I have a macro that check the value of the last filled row in Column 2 of a table and populates all the previous rows (blank or otherwise) of the Column with the same value.
This code works fine:
Sub Test1()
Dim cel As Range
Dim JulyData2 As Range
Dim ws As Worksheet
Dim LRR2 As Range
Dim LRC2 As Long
Set ws = ThisWorkbook.Worksheets("Tracker")
Set JulyData2 = ws.ListObjects("Tb_July2021").ListColumns(2).DataBodyRange
Set LRR2 = JulyData2.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
LRC2 = JulyData2.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Application.ScreenUpdating = False
For Each cel In JulyData2.Cells
If cel.Row < LRC2 Then cel.Value = LRR2.Value
Next cel
Application.ScreenUpdating = True
End Sub
Now what I want is for this code to loop through all the columns in the table starting from Column 2 i.e., check for the value of the last filled row in each Column of the table from Column 2 to the last Column and populate all the previous rows of the respective Column with the same value.
I tried this code:
Sub Test2()
Dim tblJuly As ListObject
Dim x As Long
Set tblJuly = ActiveSheet.ListObjects("Tb_July2021")
Application.ScreenUpdating = False
For x = 2 To tblJuly.ListColumns.Count
Dim LRR As Range
Dim cel As Range
Set LRR = tblJuly.ListColumns(x).DataBodyRange.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
For Each cel In tblJuly.ListColumns(x).DataBodyRange.Cells
If cel.Row < LRR.Row Then cel.Value = LRR.Value
Next cel
Next x
Application.ScreenUpdating = True
End Sub
But this code gives me a Run-time error '91' Object variable or With block variable not set
It shows an error on this line:
If cel.Row < LRR.Row Then
Where am I going wrong? And how do I correct it?