總是限定其片材被稱爲使用屬性時,如Cells
,Range
,Rows
等:。
With Sheets("Sheet1")
'This line is missing a qualification of `Rows`
'.Range("H4:I" & .Cells(Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy 'January
.Range("H4:I" & .Cells(.Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy 'January
'This line is missing qualification of Cells and Rows
'Workbooks("2017 Plan.xlsx").Sheets("Consolidated").Range("L" & Cells(Rows.Count, "F").End(xlUp).Row).Offset(1, 0).PasteSpecial Paste:=xlPasteValues 'paste to 1+ rows paste last row used
Workbooks("2017 Plan.xlsx").Sheets("Consolidated").Range("L" & Workbooks("2017 Plan.xlsx").Sheets("Consolidated").Cells(Workbooks("2017 Plan.xlsx").Sheets("Consolidated").Rows.Count, "F").End(xlUp).Row).Offset(1, 0).PasteSpecial Paste:=xlPasteValues 'paste to 1+ rows paste last row used
End With
現在,最後一個語句是有點笨重,所以我們可以通過使用With
塊簡化編碼:
With Sheets("Sheet1")
.Range("H4:I" & .Cells(.Rows.Count, "A").End(xlUp).Row).SpecialCells(xlCellTypeVisible).Copy 'January
End With
With Workbooks("2017 Plan.xlsx").Sheets("Consolidated")
.Range("L" & .Cells(.Rows.Count, "F").End(xlUp).Row).Offset(1, 0).PasteSpecial Paste:=xlPasteValues 'paste to 1+ rows paste last row used
End With
編輯:用XLMatters聊天后,我們發現,找到最後一個正常的做法列中使用的單元格不起作用,因爲目標位於表格內。這結束了正在使用的代碼是:
With Workbooks("2017 Plan.xlsx").Sheets("Consolidated")
.Range("L" & .Cells(2, "F").End(xlDown).Row + 1).PasteSpecial Paste:=xlPasteValues
End With
尋找最後一個單元格和粘貼的結果更健壯的方法是:
dim lastRow As Long
With Workbooks("2017 Plan.xlsx").Sheets("Consolidated")
lastRow = .Columns("F").Find(What:="*", _
After:=.Cells(1, "F"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
.Range("L" & lastRow + 1).PasteSpecial Paste:=xlPasteValues
End With
以下注釋的第一個版本中刪除。 –
像這樣將命令串起來不是一個好主意。創建字符串需要花費時間,如果出錯則需要更長的時間來進行調試。列「A」中最後一次使用的行是4還是更多?複製可見單元看起來是一種奇怪的方式來獲得你想要的。打破這一點。將第一條語句的結果存儲在範圍變量中。檢查範圍變量是否包含您期望的範圍。將第二條語句的結果存儲在範圍變量中。檢查範圍變量是否包含您期望的單元格。 Debug.Print Rng.Address很有幫助。使用'Rng1,複製目的地:= Rng2'複製範圍。 –