VBA noob here. I am trying to pull data from a table from sheet "Schedule" based on the value in column B, copying the values in column "C" and then pasting the results to the associated table on another sheet "Report1". The "Report1" has 5 Tables with headers (header rows can change) and I am trying to paste the results from "Schedule" to "Report1" in the specific tables.
Sub CopyDataToReport()
Dim r As Integer
Dim s As Integer
Dim t As Integer
Dim u As Integer
Dim LRow As Long
Dim InfoSecCounter As Long
Dim HeaderRow_InfoSec As Long
Dim HeaderRow_BCDR As Long
Dim HeaderRow_PhysicalandDC As Long
Dim HeaderRow_SDLC As Long
Dim HeaderRow_ITOps As Long
Worksheets("Schedule").Activate
LRow = Worksheets("Schedule").range("B" & rows.Count).End(XLUP).Row
Worksheets("Report1").Activate
HeaderRow_InfoSec = Cells.Find(what:="InfoSec - 2019", _
After:=range("B1"), _
lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Worksheets("Schedule").Activate
For r = 2 To LRow
If Worksheets("Schedule").range("B" & r).Value = "InfoSec" Then
Worksheets("Schedule").range("C" & r).Copy
Worksheets("Report1").Activate
LRowReport = Worksheets("Report1").range("B" & r + HederRow_InfoSec).End(XLUP).Row
Worksheets("Report1").range("B" & LRowReport + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next r
'Populate BCDR Controls in BCDR Table
Worksheets("Report1").Activate
HeaderRow_BCDR = Cells.Find(what:="BCDR - 2019", _
After:=range("B1"), _
lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Worksheets("Schedule").Activate
For s = 2 To LRow
If Worksheets("Schedule").range("B" & s).Value = "BCDR" Then
Worksheets("Schedule").range("C" & s).Copy
Worksheets("Report1").Activate
LRowReport = Worksheets("Report1").range("B" & s + HeaderRow_BCDR).End(XLUP).Row
Worksheets("Report1").range("B" & LRowReport + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next s
'Populate SDLC Controls in SDLC Table
Worksheets("Report1").Activate
HeaderRow_SDLC = Cells.Find(what:="SDLC - 2019", _
After:=range("G1"), _
lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=True).Row
Worksheets("Schedule").Activate
For u = 2 To LRow
If Worksheets("Schedule").range("B" & u).Value = "SDLC" Then
Worksheets("Schedule").range("C" & u).Copy
Worksheets("Report1").Activate
LRowReport = Worksheets("Report1").range("G" & u + HeaderRow_SDLC).End(XLUP).Row
Worksheets("Report1").range("G" & LRowReport + 1).Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
End If
Next u
End Sub
Code is working fine for the first two tables, but as soon as move to the right, column "G" (where the 3rd table is) the code is pasting the values in column G but starting from row 37 when it should be row 4. Any Ideas why the "SDLC" code is pasting the rows in the wrong location?
Also, I am sure there is a more elegant why to write this code, but this is my first attempt.
Thank you!