Showing posts with label vb. Show all posts
Showing posts with label vb. Show all posts

Wednesday, March 7, 2012

Compare the simlarity of two strings in Visual Basic

I have adapted this code from some C code I found on the Internet years ago.


Function WordCompare(word1 As String, word2 As String, Optional weight_towards_front As Boolean = False) As Integer
'Compares two strings and returns a value between 0 and 100 for similarness
If word1 = "" And word2 = "" Then
WordCompare = 0
Exit Function
End If

If word1 = word2 Then
WordCompare = 100
Exit Function
End If

Dim word1_len, word2_len As Integer

word1_len = Len(word1)
word2_len = Len(word2)

Dim n, i, j, k, x, y, eb As Integer
n = 0
i = 0
j = 0
k = 0
x = 0
y = 0

For i = 1 To word1_len
x = i

For j = 1 To word2_len
y = j
k = 0

Do While x <= word1_len And y <= word2_len And Mid(word1, x, 1) = Mid(word2, y, 1)
eb = 1
If weight_towards_front Then
If x < 2 And y < 2 Then
eb = eb + 3
End If
If x < 3 And y < 3 Then
eb = eb + 2
End If
If x < 4 And y < 4 Then
eb = eb + 1
End If
End If

k = k + 1
n = n + (k * k * eb)
x = x + 1
y = y + 1
Loop

Next j
Next i

n = (n * 20) / (word1_len * word2_len)

If n > 100 Then
n = 100
End If

WordCompare = n

End Function

Wednesday, February 18, 2009

VBA Excel - Formatting a column of numbers to a given precision, stripping out the decimal place, and adding leading zeros to satisfy a minimum length


'Return the number dNumber increased to the power iExponent
Function Pow(ByVal dNumber As Double, ByVal iExponent As Integer) As Double
Pow = 1

Dim iCounter As Integer

For iCounter = 1 To iExponent
Pow = Pow * dNumber
Next iCounter
End Function

'Convert the values in the active column to the given precision, with no decimal place, filled with leading zeros to satisfy a minimum length
Sub ConvertColToTextNoDecimal(ByVal iPrecision As Integer, ByVal iMinimumLength As Integer)
Dim row As Long
Dim col As Long

col = Selection.Column 'We are going to process whatever column is currently selected

Dim val As Variant

For row = 2 To ActiveSheet.UsedRange.Rows.Count 'Assume we are processing from below a header row to the last used row in the selected sheet
val = ActiveSheet.Cells(row, col).Value 'Get the current value

On Error GoTo NextRow
val = CStr(Round(CDbl(val) * Pow(10, iPrecision), 0)) 'Round to the given precision, then shift off the decimal place

While Len(val) < iMinimumLength 'If the value we have isn't long enough:
val = "0" & val 'Prepend a zero
Wend

ActiveSheet.Cells(row, col).FormulaR1C1 = "'" & val 'Mark this value as text, so we don't lose any leading zeros
NextRow:
On Error GoTo 0
Next row

End Sub


Example usage:



'Round to 3 decimal places, remove the decimal point
Sub ConvertColTo3Dec()
ConvertColToTextNoDecimal 3, 0
End

'Round to 2 decimal places, remove the decimal point
Sub ConvertColTo2Dec()
ConvertColToTextNoDecimal 2, 0
End Sub

'Round to 1 decimal place, remove the decimal point
Sub ConvertColTo1Dec()
ConvertColToTextNoDecimal 1, 0
End Sub

'Round to 0 decimal places
Sub ConvertColTo0Dec()
ConvertColToTextNoDecimal 0, 0
End Sub

'No decimal place changes, but force enough leading zeros for 3 digits
Sub ConvertColTo0Filled3Long()
ConvertColToTextNoDecimal 0, 3
End Sub