The following is a small snippet from a pretty large program written to analyze raw data and find specific trade structures. I am new to VBA and this is my first large scale program I have written in the language.
I definitely did things unconventionally and I wanted to ask for some review and pointers how to improve my code. I tried to do things I have done in other languages (at basic levels) and encountered some trouble so I did it in a way that achieved the goal. I'm sure you all agree when I say writing code that works is nice, but beautiful, succinct code is most important.
One thing specifically I had interest in doing was creating an object to manage the location of the program in the spreadsheet. I used the variable row_number to track the current row, but when I tried to set a range variable say Set r = Range("H" & row_number)
, row_number
wouldn't update as it was incremented. Rather than having to write out all that code, writing just r
would have been much more succinct, clean, and safe should I ever need to go back and make a change.
Also, the indenting might be a little off because I had to add spaces here and there so that it showed up as code rather than text, but I'm confident in my indenting practices.
'Deletes voided trades
Do
DoEvents
row_number = row_number + 1
tradeStatus = ActiveSheet.Range("M" & row_number)
If InStr(tradeStatus, "Void") >= 1 Then
ActiveSheet.Rows(row_number & ":" & row_number).Delete
row_number = row_number - 1
End If
Loop Until row_number = lastRow
'Resets row number in preparation for removing voided trades and finds the new last row
row_number = 2
lastRow = ThisWorkbook.Sheets("Trade_Data_Insert").Cells(Rows.Count, 1).End(xlUp).Row
colorImp = 23
'Highlights trade structures via time
Do
DoEvents
'Row color tester is a proxy for testing trade time "structure principle"
row_number = row_number + 1
row_color_tester = row_number
tradeTime = ActiveSheet.Range("B" & row_number).Text
If tradeTime = Range("B" & (row_color_tester + 1)).Text Or tradeTime = Range("B" & (row_color_tester - 1)).Text Then
Range("A" & row_number, "O" & row_number).Interior.ColorIndex = colorImp
End If
Loop Until row_number = lastRow
'Analyze trade data for structures
row_number = 3
tradeMonth = Range("F" & row_number).Text
Do
DoEvents
tradeTime = ActiveSheet.Range("B" & row_number).Text
legCounter = 0
'Counts legs to narrow structure type
Do
DoEvents
row_color_tester = row_number
If tradeTime = Range("B" & (row_color_tester + 1)).Text Or tradeTime = Range("B" & (row_color_tester - 1)).Text Then
legCounter = legCounter + 1
End If
row_number = row_number + 1
Loop Until tradeTime <> Range("B" & (row_color_tester + 1)).Text
' Following large IF Statements analyze individual legs to determine specific structure types
If legCounter = 2 Then
Range("M" & (row_number - legCounter)) = legCounter
'Declaring variables for string interpolation
Dim fLeg As String
Dim sLeg As String
Dim struct As String
Dim mulLot As Boolean
mulLot = False
'Defining what the first leg value is
fLeg = monthCode(Range("F" & (row_number - 1)).Text)
'Defining second leg value
sLeg = monthCode(Range("F" & (row_number - 2)).Text)
If fLeg = sLeg And Range("J" & (row_number - 1)) = Range("J" & (row_number - 2)) Then
mulLot = True
End If
'If statement determining strip or spread
If Range("J" & (row_number - 1)) <> Range("J" & (row_number - 2)) Then
struct = "Spread"
ElseIf Range("J" & (row_number - 1)) = Range("J" & (row_number - 2)) Then
struct = "Strip"
End If
If mulLot = False Then
Range("O" & (row_number - legCounter)) = fLeg & "/" & sLeg & " " & struct
Range("A" & (row_number - legCounter), "O" & (row_number - legCounter)).Interior.ColorIndex = 4
ElseIf mulLot = True Then
Range("O" & (row_number - legCounter)) = fLeg & " Multiple clips"
Range("A" & (row_number - legCounter), "O" & (row_number - legCounter)).Interior.ColorIndex = 3
End If
row_number
value? \$\endgroup\$