Here is some code which loops through an area in a spreadsheet and executes the code based on the condition that the source cells do not contain the value "(blank)". The code works, but its very inefficient to run nested if statements in this manner. I've tried to have a go at making it more efficient in the long run but I'm out of ideas.
Any suggestions?
Sub NestedIfStatement()
Dim lastrow1 As Long
Dim I As Integer, J As Integer, N As Integer, MaxPriority as Integer
Dim Maxnumber as Range
Dim WS1 As Worksheet, WS3 as Worksheet
Dim WB As Workbook
Set WB = ThisWorkbook
Set WS1 = WB.Worksheets("Config")
Set WS2 = WB.Worksheets("Data")
Set WS3 = WB.Worksheets("Status Report")
lastrow1 = WS1.Cells(Rows.Count, 1).End(xlUp).Row
I = 1
J = 1
N = 3
Set Maxnumber = WS1.Range("A" & I & ":A" & lastrow1)
MaxPriority = Application.Max(Maxnumber)
For J = 1 To lastrow1
If WS1.Cells(J, 1) <= MaxPriority Then
If WS1.Cells(J, 6).Value <> "(blank)" Then
WS3.Cells(N, 7).Value = WS1.Cells(J, 6).Value
End If
If WS1.Cells(J, 5).Value <> "(blank)" Then
WS3.Cells(N, 6).Value = WS1.Cells(J, 5).Value
End If
If WS1.Cells(J, 4).Value <> "(blank)" Then
WS3.Cells(N, 4).Value = WS1.Cells(J, 4).Value
End If
If WS1.Cells(J, 3).Value <> "(blank)" Then
WS3.Cells(N, 3).Value = WS1.Cells(J, 3).Value
End If
If WS1.Cells(J, 2).Value <> "(blank)" Then
WS3.Cells(N, 2).Value = WS1.Cells(J, 2).Value
End If
N = N + 1
End If
Next J
End Sub
MaxPriority = Application.Max(Maxnumber)whereSet Maxnumber = WS1.Range("A" & I & ":A" & lastrow1), how canWS1.Cells(J, 1) <= MaxPriorityever be false?