2017-08-17 109 views
1

我在其他頁面有相同的代碼,它工作正常,但在一個頁面它不會拒絕粘貼到第三行,它只能做到第一行有人可以幫忙嗎?VBA Offseting複製範圍不起作用

Set wsInfoFrom = Worksheets("STP1st") 
Set wsInfoTo = Worksheets("StowToPrime") 
lastrow = wsInfoFrom.Range("B" & wsInfoFrom.Rows.Count).End(xlUp).Row 
Set copyRange = wsInfoFrom.Range("B5:B" & lastrow) 

wsInfoTo.Range("A1:A9999" & lastrow).ClearContents 

    copyRange.SpecialCells(xlCellTypeVisible).Copy wsInfoTo.Range("A" & Rows.Count).End(xlUp).Offset(3, 0) 
+0

當我測試它在VB建設者它工作正常,但是當我運行宏它將列向上移動A列 – Mils

+1

(a)我看不到'wsInfoTo.Range(「A」&Rows.Count).End(xlUp).Offset(3,0)'可以指向高於單元格A4(b)我看不到'wsInfoTo.Range(「A1:A9999」&lastrow).ClearContents'如何工作 - 例如如果'lastrow'是123(因爲B123是'wsInfoFrom'中最後一個被佔用的單元格),您將試圖清除'A1:A9999123'的內容 - 因此,如果您在'wsInfoFrom'中有低於B99的任何內容,則懷疑崩潰。 – YowE3K

+0

道歉代碼有罰款我已經調試到另一塊代碼,這是從行中刪除重複項。我需要做這個Columns(1).RemoveDuplicates Columns:= Array(1),但是從單元格A3下來 – Mils

回答

0

嘗試下面的略微修飾的和更有組織的代碼,我覺得這是你的意思辦:

Set wsInfoFrom = Worksheets("STP1st") 
Set wsInfoTo = Worksheets("StowToPrime") 

With wsInfoFrom 
    lastrow = .Range("B" & .Rows.Count).End(xlUp).Row 
    Set copyRange = .Range("B5:B" & lastrow) 
End With 

With wsInfoTo 
    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row ' <-- different last row in this sheet 
    .Range("A1:A" & lastrow).ClearContents 

    copyRange.SpecialCells(xlCellTypeVisible).Copy .Range("A" & .Rows.Count).End(xlUp).Offset(3, 0) 
End With