Macro

Copper Contributor

Trying to get a VBA code that column B autofills to the last row that has data in column A, but I keep getting an issue with the range and I'm not sure what's wrong with this:

 

Sub CombinedApply()
Dim lastRowA As Long
Dim lastRowB As Long
Dim fillRange As Range
Dim i As Long

' Find the last row with data in column A
lastRowA = Cells(Rows.Count, "A").End(xlUp).Row

' Find the corresponding cell in column B
lastRowB = Cells(lastRowA, "B").Row

' Define the range to be filled
Set fillRange = Range("B" & lastRowB & ":B" & lastRowA)

' Loop through each cell in the range and fill it
For i = lastRowB To lastRowA
Cells(i, "B").Value = Cells(lastRowB, "B").Value
Next i
End Sub

2 Replies

@heyxhar 

Try this:

Sub CombinedApply()
    Dim lastRowA As Long
    Dim lastRowB As Long
    Dim fillRange As Range

    ' Find the last row with data in column A
    lastRowA = Cells(Rows.Count, "A").End(xlUp).Row

    ' Find the last row with data in column B
    lastRowB = Cells(Rows.Count, "B").End(xlUp).Row

    If lastRowA > lastRowB Then
        ' Define the range to be filled
        Set fillRange = Range("B" & lastRowB + 1 & ":B" & lastRowA)
        ' Fill range with last used value of column B
        fillRange.Value = Cells(lastRowB, "B").Value
    End If
End Sub
thank you so much!!