簡體   English   中英

剪切-在Excel VBA中粘貼自動檢測到的范圍

[英]Cut - Paste an auto-detected range in Excel VBA

我正在嘗試在定義的位置(特別是緊接在列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,我不明白為什么。

更改您的行:

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

至:

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

您需要添加“ Cells的“ Address屬性(每次僅使用一個“單元格”參數調用“范圍”時都必須添加)。 所以:

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

雖然這可以解決您的問題,但是您應該在代碼中進行許多更改,包括避免選擇,這將提高性能並避免其他問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM