简体   繁体   中英

Excel Macro Find Last Row In Column B, Select another cell A1, drag formula from A1 to last row in B

I am trying to do an AutoFill in Excel where I can make my macro:

  1. Find the last row with data in column B (lets say this is B40 ), and remember.
  2. Select A1 .
  3. Drag A1 's formula to the row that was found in step 1 (which would be A40 ) so as to perform an autofill the formula.

I am pretty noobish at VBA so would really appreciate some help.

This should do what you need.

' Get last row with data in column B...
Dim intLastRow As Long
intLastRow = Cells(Rows.Count, "B").End(xlUp).Row

' Fill column A down to this row... 
Range("A1:A" & intLastRow).FillDown

You don't even require a Range.FillDown or Range.AutoFill method in this case.

With ActiveSheet
    .Cells(1, 1).Resize(.Cells(Rows.Count, 2).End(xlUp).Row, 1) = .Cells(1, 1).Formula
End With

Just start with the top-left cell and use the Range.Resize property to expand it to the desired dimensions and transfer the formula.

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