Hello,
I'm new to Excel VBA and have run into a complex problem for my current level. Let me explain:
The formula in column S is =(A2&B2)=(N2&K2) and fills down to the second last row i.e. =(A4&B4)=(N4&K4). In the original worksheet, there are over 5000 rows of such data that include the same formula in column S. I want to write code that will do the following:
Here is what the data looks like after all the macro(s) is/are run:
The code I am using as a basis for my macro is below. I found it here on the forum but haven't been able to tweak it to my needs:
I've spent a lot of time trying to learn more VBA off the internet and to think of different ways to do this but to no avail. Your help and input will therefore be greatly appreciated
I'm new to Excel VBA and have run into a complex problem for my current level. Let me explain:
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S |
XYZ | 10 | 2022 | 2nd Quarter | 5 | May | 10 | Earth | Earth | XYZ | 10 | 20 | 30 | TRUE | |||||
ABC | 11 | 2022 | 2nd Quarter | 5 | May | 12 | Earth | Earth | LMN | 6 | 34 | 5 | FALSE | |||||
LMN | 12 | 2022 | 2nd Quarter | 5 | May | 13 | PQR | 45 | 8 | 12 | FALSE | |||||||
PQR | 13 |
The formula in column S is =(A2&B2)=(N2&K2) and fills down to the second last row i.e. =(A4&B4)=(N4&K4). In the original worksheet, there are over 5000 rows of such data that include the same formula in column S. I want to write code that will do the following:
- Run through column S from top to bottom and identify the first FALSE value. Then add new cells from columns G to S in the row above it.
For example, in this case, code will identify FALSE in row 3 and add new cells G3: S3, pushing the existing data down to G4:S4.
- Copy the values in new cells G:J and L:M from the cells in the corresponding column and the row above. That is, the values in the new G3:J3 and L3:M3 will be copied from G2:J2 and L2:M2. (in blue in the table below)
- Values in new cells in K and N copied from B and A in the same row, i.e., K3 and N3 will copy from B3 and A3. (in green in the table below)
- New cells from O:Q will have the number 0. (in red in the table below)
- Finally, the column S formula will be filled down to the last row and this entire code will loop until there are no more FALSE values in column S.
Here is what the data looks like after all the macro(s) is/are run:
XYZ | 10 | 2022 | 2nd Quarter | 5 | May | 10 | Earth | Earth | XYZ | 10 | 20 | 30 | TRUE | |||||
ABC | 11 | 2022 | 2nd Quarter | 5 | May | 11 | Earth | Earth | ABC | 0 | 0 | 0 | TRUE | |||||
LMN | 12 | 2022 | 2nd Quarter | 5 | May | 12 | Earth | Earth | LMN | 6 | 34 | 5 | TRUE | |||||
PQR | 13 | 2022 | 2nd Quarter | 5 | May | 13 | PQR | 45 | 8 | 12 | TRUE |
The code I am using as a basis for my macro is below. I found it here on the forum but haven't been able to tweak it to my needs:
VBA Code:
Sub InsertOnFalse()
Dim n As Long
Dim ws As Worksheet
Set ws = ActiveWorkbook.Sheets("Audit Volume")
Application.ScreenUpdating = False
n = 1625
Do While Not ws.Range("A" & n) = ""
If ws.Range("S" & n) = False Then
ws.Range("R" & n, "S" & n).Insert Shift:=xlShiftDown
ws.Range("S" & n).Offset(-1, 0).Copy
ws.Range("S" & n).PasteSpecial (xlPasteFormulas)
End If
ws.Range("S" & n).Copy
ws.Range("S" & n).Offset(1, 0).PasteSpecial (xlPasteFormulas)
n = n + 1
Loop
End Sub
I've spent a lot of time trying to learn more VBA off the internet and to think of different ways to do this but to no avail. Your help and input will therefore be greatly appreciated