【问题标题】:Cut - Paste an auto-detected range in Excel VBA剪切 - 在 Excel VBA 中粘贴自动检测到的范围
【发布时间】: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。
  • 我建议最后一行只是大于列数...

标签: vba excel


【解决方案1】:

您需要添加CellsAddress 属性(每当您调用只有一个单元格参数的范围时都是必需的)。所以:

Range(Cells(2, LastRow2 + 1).Address).Select

虽然这将解决您的问题,但您应该在代码中进行许多更改,包括避免选择,这将提高性能并避免其他问题

【讨论】:

    【解决方案2】:

    换行:

    Range(Cells(2, LastRow2 + 1)).Select
    

    收件人:

    Range("B" & LastRow2 + 1).Select
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多