简体   繁体   中英

VBA AutoFill - Cell references variable

I am trying to autofill the last cell in column A down to the last cell in column B.

However, the last cell in each column is variable.

Below is my current code:

Sub Import()

'Select first empty cell in column B
    Sheets("Sheet1").Select
    Range("B1").End(xlDown).Offset(1, 0).Select

'Paste  Data
    ActiveSheet.Paste
    
'Allocate source_file to pasted data
    Range("A1").End(xlDown).Offset(1, 0).Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "TEMP"

End Sub

I have located the first empty cell in column A and entered "TEMP". I now want to fill this down to the last row in column B.

Any help would be appreciated.

Thanks

This should do it:

    Dim lastrowa As Long
    Dim lastrowb As Long
    
    With Sheet2
        lastrowa = .Cells(.Rows.Count, 1).End(xlUp).Row
        lastrowb = .Cells(.Rows.Count, 2).End(xlUp).Row
        
        .Cells(lastrowa + 1, 1).Value = "TEMP"
        .Cells(lastrowa + 1, 1).AutoFill .Range(.Cells(lastrowa + 1, 1), .Cells(lastrowb, 1))
    End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM