简体   繁体   English

Excel宏查找列B中的最后一行,选择另一个单元格A1,将公式从A1拖动到B中的最后一行

[英]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: 我正在尝试在Excel中进行自动填充,然后在其中创建宏:

  1. Find the last row with data in column B (lets say this is B40 ), and remember. 在B列中找到包含数据的最后一行(假设这是B40 ),并记住。
  2. Select A1 . 选择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. A1的公式A40步骤1中找到的行(应为A40 )中,以执行自动填充公式。

I am pretty noobish at VBA so would really appreciate some help. 我对VBA不太满意,因此非常感谢您的帮助。

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. 在这种情况下,您甚至不需要Range.FillDownRange.AutoFill方法。

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. 只需从左上​​角的单元格开始,然后使用Range.Resize属性将其扩展到所需的尺寸并传输公式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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