同时将 2 个选定的相邻单元格的内容复制到 table 的最后一行
Simultaneously copying down the contents of 2 selected, adjacent cells to the last row of a table
我看过几篇文章展示了如何使用指定的单元格范围执行此操作,但我需要能够使用可变范围(无论选择什么范围)执行此操作。我在我的宏中有两个单元格彼此相邻,需要根据 A 列的距离将公式复制到 table 的末尾(活动单元格在B 和 C 列)。我正在尝试的代码如下:
ActiveCell.Resize(1, 2).Select <--Selects the two cells containing a formula in columns B and C.
Set rng = Selection
Selection.AutoFill Destination:=Range("rng" & Range("A" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
这给出了一个错误。我认为问题在于“rng”,但我无法指定类似“3B:50C”的内容,因为每次运行时 B 列和 C 列中的起点单元格可能是不同的行。任何帮助,将不胜感激。非常感谢!
像这样:
Dim lr As Long, ws As Worksheet, c As Range
If TypeName(Selection) <> "Range" Then Exit Sub 'make sure a range is selected
Set c = Selection.Cells(1) 'get the top-left cell
Set ws = c.Worksheet 'parent worksheet
lr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'last occupied row in ColA
c.Resize(1, 2).AutoFill Destination:=ws.Range(ActiveCell, ws.Cells(lr, c.Column + 1))
我看过几篇文章展示了如何使用指定的单元格范围执行此操作,但我需要能够使用可变范围(无论选择什么范围)执行此操作。我在我的宏中有两个单元格彼此相邻,需要根据 A 列的距离将公式复制到 table 的末尾(活动单元格在B 和 C 列)。我正在尝试的代码如下:
ActiveCell.Resize(1, 2).Select <--Selects the two cells containing a formula in columns B and C.
Set rng = Selection
Selection.AutoFill Destination:=Range("rng" & Range("A" & Rows.Count).End(xlUp).Row)
Range(Selection, Selection.End(xlDown)).Select
这给出了一个错误。我认为问题在于“rng”,但我无法指定类似“3B:50C”的内容,因为每次运行时 B 列和 C 列中的起点单元格可能是不同的行。任何帮助,将不胜感激。非常感谢!
像这样:
Dim lr As Long, ws As Worksheet, c As Range
If TypeName(Selection) <> "Range" Then Exit Sub 'make sure a range is selected
Set c = Selection.Cells(1) 'get the top-left cell
Set ws = c.Worksheet 'parent worksheet
lr = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'last occupied row in ColA
c.Resize(1, 2).AutoFill Destination:=ws.Range(ActiveCell, ws.Cells(lr, c.Column + 1))