【发布时间】:2016-07-19 14:21:09
【问题描述】:
我正在尝试在定义的位置(特别是在 A 列或 B 列中最后使用的单元格之后)剪切和粘贴具有自动检测行范围的单元格块。
Sub Cut_Range_To_Clipboard()
'Detecting number of used lines in the chunck of code I need to cut
Dim LastRow As Long
With ActiveSheet
LastRow = .Cells(.Rows.Count, "T").End(xlUp).Row
MsgBox LastRow
End With
'Detecting number of used lines in the column I need to paste the code
Dim LastRow2 As Long
With ActiveSheet
LastRow2 = .Cells(.Rows.Count, "B").End(xlUp).Row
MsgBox LastRow2
End With
'Cells(20, 3) = T3
Range(Cells(20, 3), Cells(36, LastRow)).Cut
Range(Cells(2, LastRow2 + 1)).Select
ActiveSheet.Paste
Range("T1").Cut
Range(Cells(1, LastRow2 + 1)).Select
ActiveSheet.Paste
Columns("T:AK").EntireColumn.Delete
End Sub
执行代码时,Range(Cells(2, LastRow2 + 1)).Select 行输出错误 1004,我不明白为什么。
【问题讨论】:
-
只使用
Cells(2, LastRow2 + 1)没有范围。范围 -
Cells([row], [column])...你确定Cells(2, LastRow2 + 1)???远离语法错误,您可以在这里翻转行和列;) -
@DirkReichel 你是对的。谢谢!
-
@ScottCraner 你也是对的。仅使用 Cells。
-
我建议最后一行只是大于列数...