Relative novice here in VBA, in need of some assistance modifying code to fit a specific use case. I've searched high and low in addition with tinkering with code but to date have been unsuccessful in locating a similar use case // executing the necessary changes on my own.
Use Case: Have an exported report that generates multiple reports within a single worksheet all separated by a single blank space after a row that contains totals. The names of each report are static but the amount of data contained within each report is dynamic (how many rows it can contain).
I need code that searches Column "A" in Sheet1 for a specific value (example in the attached image it would be "Extra Header A" for a report title). Then copies (preferably) from the next row under "Extra Header A" down to the blank space under the row with "Data 9" and from the columns with "Header B" to "Header E" into Sheet2("A1").
Use Case Image:
The code listed below is what I have found moderate success with (sorry source is unavailable as I've Frankensteined this together). The current issue with this code is it appears to only be static in nature (by modifying the if statement range method) and does not account for the number of rows within each report being dynamic.
Sub Cells_Loop()
Dim c As Range, lastrow As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
Application.ScreenUpdating = False
For Each c In Range("A1:A500" & lastrow)
If c.Value = "Extra Header A" Then Range("A" & c.Row & ":D" & c.Row).Copy Worksheets("Sheet2").Range("A" & 1)
Next c
Worksheets("Sheet2").Rows(1).Delete Shift:=xlUp
Application.ScreenUpdating = True
End Sub
Any help offered would be greatly appreciated! Thanks in advance.
edit added another image for additional context. Red would be data I'm looking to avoid, while blue is the targeted data. Image 2
For Each c In Range("A1:A500" & lastrow)
makes no sense. Change it toFor Each c In Range("A1:A" & lastrow)
to make it dynamic. Also, you have anIf
statement with noEnd If
. ALSO, what's the point of the delete line outside of your loop??? – dwirony