Add Loop in FindDistinctSubString VBA Module - vba

I have found a VBA script which is capable of making a distinction between two strings. For example A1 = aaabbb and B1 = aaabbbccc - it will highlight the ccc section in B1.
Since the report I am running this through has many rows I would like to loop it through a[i] and b[i]. I have tried adding standard loops though unfortunately it has consistently errored out.
Does anyone have experience adding loops to such a script?
Here is the script:
Public Sub FindDistinctSubstrings()
Dim a$, b$, i&, k&, rA As Range, rB As Range
Set rA = [a1]: a = rA
Set rB = [b1]: b = rB
k = Len(a): If Len(b) > k Then k = Len(b)
Do
i = i + 1
If Mid$(a, i, 1) <> Mid$(b, i, 1) Then
Align i, a, b, rA, rB
End If
DoEvents
Loop Until i > k
k = Len(a): If Len(b) > k Then k = Len(b)
For i = 1 To k
If Mid$(a, i, 1) = "." Then rB.Characters(i, 1).Font.Color = vbRed
If Mid$(b, i, 1) = "." Then rA.Characters(i, 1).Font.Color = vbRed
Next
Do
k = InStr(rA, "."): If k Then rA.Characters(k, 1).Delete
Loop Until k = 0
Do
k = InStr(rB, "."): If k Then rB.Characters(k, 1).Delete
Loop Until k = 0
End Sub
Private Sub Align(k&, a$, b$, rA As Range, rB As Range)
Dim i&, iMax&, nI&, nMaxI&, j&, jMax&, nJ&, nMaxJ&
Const LOOK_AHEAD_BUFFER = 30
For i = 0 To LOOK_AHEAD_BUFFER
nI = CountMatches(Space$(i) & Mid$(a, k, LOOK_AHEAD_BUFFER), Mid$(b, k, LOOK_AHEAD_BUFFER))
If nI > nMaxI Then
nMaxI = nI: iMax = i
End If
Next
For j = 0 To LOOK_AHEAD_BUFFER
nJ = CountMatches(Mid$(a, k, LOOK_AHEAD_BUFFER), Space$(j) & Mid$(b, k, LOOK_AHEAD_BUFFER))
If nJ > nMaxJ Then
nMaxJ = nJ: jMax = j
End If
Next
If nMaxI > nMaxJ Then
a = Left$(a, k - 1) & String$(iMax, ".") & Mid$(a, k)
rA = a: k = k + iMax
Else
b = Left$(b, k - 1) & String$(jMax, ".") & Mid$(b, k)
rB = b: k = k + jMax
End If
End Sub
Private Function CountMatches(a$, b$) As Long
Dim i&, k&, c&
k = Len(a): If Len(b) < k Then k = Len(b)
For i = 1 To k
If Mid$(a, i, 1) = Mid$(b, i, 1) Then c = c + 1
Next
CountMatches = c
End Function
Many thanks,
Sebastian

Related

Transform Sub to UDF getting (#VALUE!)

I have a program Sub which works well. I want to convert it into a custom Function, but when I use this function in Excel an error (#VALUE!) occurs
Function ТридцатьТРи(Diapozon As Integer)
'для п/пр
Dim k, n As Integer
Dim parRange As Range
Set parRange = Range("Diapozon")
k = 0
n = 0
For Each Cell In parRange.Rows
If Cell.Offset(0, 1).Value = 1 And k = -1 Then
n = n - 1
End If
If Cell.Value = 1 And k = -1 Then
n = n + 1
End If
If Cell.Value = 1 Then
k = k + 1
If k = 2 Then
k = -1
End If
End If
If Cell.Value = 2 Or Cell.Value = 3 Then
k = 0
End If
Next Cell
ТридцатьТРи = n
End Function
Try the UDF code below (not sure about what you are trying to achieve with your logics inside the UDF), but it works (not getting #VALUE!).
Since you want to pass a Range object to the UDF (according to your screen-shot), you need to define it also in your Function code.
Code
Function cyrilic(Diapozon As Range) As Long
Dim k As Long, n As Long
Dim C As Range
k = 0
n = 0
For Each C In Diapozon.Rows
If C.Offset(0, 1).Value = 1 And k = -1 Then
n = n - 1
End If
If C.Value = 1 And k = -1 Then
n = n + 1
End If
If C.Value = 1 Then
k = k + 1
If k = 2 Then
k = -1
End If
End If
If C.Value = 2 Or C.Value = 3 Then
k = 0
End If
Next C
cyrilic = n
End Function
This will work as a worksheet function apperently, you need to input the range that you previousely defined with a named range "diapozon" as an input range.
Function cyrillic(rng As Range)
Dim k, n As Integer
Dim parRange As Range
Set parRange = rng
k = 0
n = 0
For Each Cell In parRange.Rows
If Cell.Offset(0, 1).Value = 1 And k = -1 Then
n = n - 1
End If
If Cell.Value = 1 And k = -1 Then
n = n + 1
End If
If Cell.Value = 1 Then
k = k + 1
If k = 2 Then
k = -1
End If
End If
If Cell.Value = 2 Or Cell.Value = 3 Then
k = 0
End If
Next Cell
cyrillic = n
End Function
Just type : =cyrillic("R1:RX") and it should work.

Defining worksheets and variables for mulitple worksheets for one textbox in VBA for cases

I am having a problem with the block of code below. I am scanning a barcode which places the text in an ActiveX textbox. Upon seeing the text in the ActiveX text box, it runs through a series of cases. It then performs the necessary movements. My guess is that the textbox is in Worksheet(1)? Or that I do not know how to Dim a worksheeet or Set Variables properly!
I also know I will need to change the 0's to an actual possible cell range value.
Private Sub TextBox1_Change()
Dim bws As Worksheet, v, a, b, c, d, e, f, g, h, i, j, k, l
Set bws = Worksheets("PACKAGING-BOXES")
Dim cws As Worksheet, v, a, b, c, d, e, f, g, h, i, j, k, l
Set cws = Worksheets("PACKAGING-CARTONS")
Dim rws As Worksheet, v, a, b, c, d, e, f, g, h, i, j, k, l
Set rws = Worksheets("Cut ROlls")
v = TextBox1.Value
a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
i = 0
j = 0
k = 0
l = 0
'a = Packing Units in one case
'b = Clip strip, row reference, PACKAGING-CARTONS
'c = Clip strip hooks, row reference, PACKAGING-CARTONS
'd = Number of clip strips or hooks used, PACKAGING-CARTONS
'e = row reference, PACKAGING-CARTONS
'f = Number of polybags used
'g = Carton Number, row reference, PACKAGING-CARTONS-WAYNE
'h = Box Number 1, row reference, PACKAGING-BOXES
'i = Box Number 2, row reference, PACKAGING-BOXES
'j = Box Number 3, row reference, PACKAGING-BOXES
'k = row ID for cut rolls
'l = number for case selection
'_____________________________________________________________________________________________
Select Case v
Case 1: l = 10
a = 72
b = 0
c = 0
d = 0
e = 0
f = 0
g = 97
h = 35
i = 36
j = 0
k = 1000
'other cases here....
'_____________________________________________________________________________________________
End Select
'_____________________________________________________________________________________________
If l = 1 Then
'Packed Items (Packing Report)
cws.Cells(b, 8) = cws.Cells(b, 8).Value - d
cws.Cells(b, 8) = cws.Cells(b, 9).Value + d
cws.Cells(c, 8) = cws.Cells(c, 8).Value - d
cws.Cells(c, 9) = cws.Cells(c, 9).Value + d
cws.Cells(e, 8) = cws.Cells(e, 8).Value - f
cws.Cells(e, 9) = cws.Cells(e, 9).Value + f
cws.Cells(g, 8) = cws.Cells(g, 8).Value - (a * cws.Cells(1, 1))
cws.Cells(g, 9) = cws.Cells(g, 9).Value + (a * cws.Cells(1, 1))
bws.Cells(h, 8) = bws.Cells(h, 8).Value - a
bws.Cells(h, 9) = bws.Cells(h, 9).Value + a
bws.Cells(i, 8) = bws.Cells(i, 8).Value - a
bws.Cells(i, 9) = bws.Cells(i, 9).Value + a
bws.Cells(j, 8) = bws.Cells(j, 8).Value - a
bws.Cells(j, 9) = bws.Cells(j, 9).Value + a
rws.Cells(k, 1) = cws.Cells(k, 1).Value + (a * cws.Cells(1, 1))
TextBox1.Activate
TextBox1.Value = ""
End If
End Sub
Thank you for the help!
Figured it out, this needs to be the start of the code. Needs to have the Wb defined as variables instead of each sheet :)
Private Sub TextBox1_Change()
Dim wb As Workbook, v, a, b, c, d, e, f, g, h, i, j, k, l
Set wb = Workbooks("Inventory Tracking.xlsm")
Dim bws As Worksheet
Set bws = wb.Worksheets("PACKAGING-BOXES")
Dim cws As Worksheet
Set cws = wb.Worksheets("PACKAGING-CARTONS")
Dim rws As Worksheet
Set rws = wb.Worksheets("Cut Rolls")
Dim fws As Worksheet
Set fws = wb.Worksheets("Sheet1")
v = TextBox1.Value
a = 0
b = 0
c = 0
d = 0
e = 0
f = 0
g = 0
h = 0
i = 0
j = 0
k = 0
l = 0

VLookup Loop not working

I'm trying VLookup until the cells in Column A are empty and it is not working and is returning Run-time error '1004' Unable to get the VLookup property of the WorksheetFunction class. Any help? Or is there a better loop that I can use.
Sub FindOldValue()
Dim oldvalue As String
Dim result As String
i = 2
j = 1
K = 2
l = 3
Do
oldvalue = Worksheets("Products").Cells(i, j) & Worksheets("Products").Cells(i, K) & "delete"
result = Application.WorksheetFunction.VLookup(oldvalue, Worksheets("Raw Delta").Range("A:H"), 7, 0)
Worksheets("Products").Cells(i, l) = result
i = i + 1
j = j + 1
K = K + 1
Loop Until Worksheets("Products").Cells(i, 1) = ""
End Sub
Have you tried using only Application.Vlookup instead of Application.Worksheetfunction.vlookup in the line
result = Application.WorksheetFunction.VLookup(oldvalue, Worksheets("Raw Delta").Range("A:H"), 7, 0)
?
How about changing the loop to something like this?
Do
oldvalue = Worksheets("Products").Cells(i, j) & Worksheets("Products").Cells(i, K) & "delete"
resultrow = Application.Match(oldvalue, Worksheets("Raw Delta").Columns(1), 0)
If Not IsError(resultrow) Then
Worksheets("Products").Cells(i, l) = Worksheets("Raw Delta").Cells(resultrow,7).Value
End If
i = i + 1
j = j + 1
K = K + 1
Loop Until Worksheets("Products").Cells(i, 1) = ""

Sum cone to surface with variable size

I am trying to develop develop a model to calculate the sum of a cone to row 1 given an array of variable size only if the value of the cell is > 0.
If the sum is then >=1 I wish to color the range of the cone to display this. If the cone hits the A row boundary I need it not to error and for it to extend in the cone shape the other boundary. Here is what I have at the moment:
Public Sub MC()
Worksheets("SC").Cells.Clear
Dim i&, j&
For j = 1 To Worksheets("Data").Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To Worksheets("Data").Cells(Rows.Count, "A").End(xlUp).Row
If Worksheets("Data").Cells(i, j) > 0 Then
Worksheets("SC").Cells(i, j).Address , SumAndColorCone(Cells(i, j))
Else: If Worksheets("Data").Cells(i, j) <= 0 Then Worksheets("SC").Cells(i, j) = "0"
End If
Next
Next
End Sub
Public Function SumAndColorCone(r As Range) As Double
Dim i&, k&, c As Range
Set c = r
For i = r.Row - 1 To 1 Step -1
If r.Column - k < 2 Then Exit For
Set c = Union(c, r(-k, -k).Resize(, (k + 1) * 2 + 1))
k = k + 1
Next
c.Interior.Color = vbRed
SumAndColorCone = Application.Sum(c)
End Function
Try this:
Public Sub MC()
Dim c&, i&, j&
For j = 1 To Cells(1, Columns.Count).End(xlToLeft).Column
For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
If Val(Cells(i, j)) > 0 Then
c = RGB(Int(Rnd * 255) + 1, Int(Rnd * 255) + 1, Int(Rnd * 255) + 1)
Debug.Print "Testing value at: " & Cells(i, j).Address & vbLf & "Cone sum: " & SumAndColorCone(Cells(i, j), c) & vbLf
End If
Next
Next
End Sub
Private Function SumAndColorCone(r As Range, color&) As Double
Dim i&, k&, c As Range
Set c = r
For i = r.Row - 1 To 1 Step -1
If r.Column - k < 2 Then
Set c = Union(c, r(-k, -r.Column + 2).Resize(, r.Column + k + 1))
Else
Set c = Union(c, r(-k, -k).Resize(, (k + 1) * 2 + 1))
End If
k = k + 1
Next
SumAndColorCone = Application.Sum(c)
If SumAndColorCone > 1 Then c.Interior.color = color
End Function

I am getting out of script error, why?

Here is the code
Sub logic()
'Declaring variables
Dim r As Long, i As Long, k As Long, l As Long
Dim num() As Long
Dim del() As Long
'Assigning Values
r = 2
i = 0
k = 0
l = 0
ReDim num(0)
ReDim del(0)
Do Until Cells(r, 11).Value = ""
num(i) = Cells(r, 15).Value
'I m getting error over here
del(l) = k - num(i)
k = num(i)
i = i + 1
ReDim Preserve num(i)
'Preserve the value
r = r + 1
l = l + 1
Loop
r = 2
For l = 0 To UBound(num) - 1
Cells(r, 25).Value = del(l)
r = r + 1
Next l
End Sub
You are growing the num array but you forget to do the same with the del array. I believe you need to modify the code like this:
i = i + 1
ReDim Preserve num(i)
'Preserve the value
r = r + 1
l = l + 1
ReDim Preserve del(l) ' Add this line