I have a macro that adds 1 to a cell in column 53 (column BA) if the row below has a cell containing the number “(2)” enclosed in brackets and another cell containing the word “Adult”.
This happens as follows:
Sub BUBFindAdults2()
lastRow = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
For x = 3 To lastRow
If InStr(1, Sheets("Sheet1").Cells(x, 3), "(2)") <> 0 _
And InStr(1, Sheets("Sheet1").Cells(x, 31), "Adult") <> 0 Then
Sheets("Sheet1").Cells(x - 1, 53).Value = _
Sheets("Sheet1").Cells(x - 1, 53).Value + 1
End If
Next x
End Sub
However, I also need to add 1 to the same cell if the two lines contain "(3)" and "Adult". And if the lines three below contain "(4)" and "Adult". Etc. You see the pattern!
So far I have circumvented this by simply repeating the same code as follows:
Sub BUBFindAdults2()
lastRow = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
For x = 3 To lastRow
If InStr(1, Sheets("Sheet1").Cells(x, 3), "(2)") <> 0 _
And InStr(1, Sheets("Sheet1").Cells(x, 31), "Adult") <> 0 Then
Sheets("Sheet1").Cells(x - 1, 53).Value = _
Sheets("Sheet1").Cells(x - 1, 53).Value + 1
End If
If InStr(1, Sheets("Sheet1").Cells(x, 3), "(3)") <> 0 _
And InStr(1, Sheets("Sheet1").Cells(x, 31), "Adult") <> 0 Then
Sheets("Sheet1").Cells(x - 2, 53).Value = _
Sheets("Sheet1").Cells(x - 2, 53).Value + 1
End If
If InStr(1, Sheets("Sheet1").Cells(x, 3), "(4)") <> 0 _
And InStr(1, Sheets("Sheet1").Cells(x, 31), "Adult") <> 0 Then
Sheets("Sheet1").Cells(x - 3, 53).Value = _
Sheets("Sheet1").Cells(x - 3, 53).Value + 1
End If
Next x
End Sub
, , , 10+! , VBA . , , .
.