I'm new to VBA programming so the following code may look terrible which is why I want ways to understand how to better optimize it and other future codes.
Sub Sequence()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Dim unique_string1, unique_string2, unique_string3 As String
Dim arrj() As String
Dim arrk() As String
Dim arro() As String
Dim a, b, c As Long
'ARRAYj
jr = Sheets("Docs").Cells(Sheets("Docs").Rows.Count, 1).End(xlUp).Row
Set range1 = Sheets("Docs").Range("J2:J" & jr)
For Each cel In range1
If Not InStr(unique_string1, cel.Value) > 0 Then
unique_string1 = unique_string1 & cel.Value & ","
End If
Next
arrj = Split(unique_string1, ",")
'ARRAYk
kr = Sheets("Docs").Cells(Sheets("Docs").Rows.Count, 1).End(xlUp).Row
Set range2 = Sheets("Docs").Range("K2:K" & kr)
For Each cel In range2
If Not InStr(unique_string2, cel.Value) > 0 Then
unique_string2 = unique_string2 & cel.Value & ","
End If
Next
arrk = Split(unique_string2, ",")
'ARRAYo
pr = Sheets("Docs").Cells(Sheets("Docs").Rows.Count, 1).End(xlUp).Row
Set range3 = Sheets("Docs").Range("o2:o" & pr)
For Each cel In range3
If Not InStr(unique_string3, cel.Value) > 0 Then
unique_string3 = unique_string3 & cel.Value & ","
End If
Next
arro = Split(unique_string3, ",")
'ForLoop
For a = LBound(arrj) To UBound(arrj)
For b = LBound(arrk) To UBound(arrk)
For c = LBound(arro) To UBound(arro)
If Not (arrj(a) = "" Or arrk(b) = "" Or arro(c) = "") Then
Dim x, y, z As String
x = Trim(arrj(a))
y = Trim(arrk(b))
z = Trim(arro(c))
Dim lRow As Long, i As Long
Dim ws As Worksheet
Dim nextn As Long
Dim getAddress As String
Set ws = ActiveSheet
With ws
lRow = .Cells(.Rows.Count, 1).End(xlUp).Row
For i = 2 To lRow
If .Cells(i, 10).Value = x And .Cells(i, 11).Value = y And .Cells(i, 15).Value = z And .Cells(i, 23).Value = "" Then
getAddress = .Cells(i, 23).Address
nextn = 1 + Application.Evaluate("MAX(IF((J2:J20000=""" & x & """)*(K2:K20000=""" & y & """)*(O2:O20000=""" & z & """),INT(w2:20000)))")
Range(getAddress).Value = nextn
End If
Next i
End With
End If
Next c
Next b
Next a
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
End Sub
So the code gets the distinct value from the columns and stores it in an array. Then those 3 arrays containing the distinct value combination is used to check for the max of the sequence and assigned a plus 1 to the max sequence if found. Else a 1.
How could I make this run faster considering there are a lot of rows and combinations to check?
P.s: If you have a code for getting distinct value from a column and store it into and array it would be great.
Mock-DATA
J K O W
1jobid dep job_no Sequence
2 aaa FJ 1 1 <existing data>
3 aaa FJ 1 2
4 aaa FJ 1 5
5 aaa RJ 1 1
6 aaa RJ 1 9
7 aaa RJ 1 10 <existing data>
-----------------------------------------------------
8 aaa RJ 1 11 <after the code is run, 11 is assigned>
9 aaa FJ 1 6 <after the code is run, 6 is assigned >
columns J,K,O are strings and column W is number.
EDIT : As mentioned in the comments changed Range("J2:J20000" & jr)
to Range("J2:J" & jr)
jr
= 10 thenRange("J2:J20000" & jr)
would refer to row2,000,010
. \$\endgroup\$ActiveSheet
alsoSheets("Docs")
? \$\endgroup\$