我需要編寫一些代碼以運行特定工作簿的每個工作表,並將特定單元複製到單獨的工作簿。我無法指定要複製到的目標工作表。我有什麼至今:從多個工作表複製到單獨的工作簿
Private Sub CommandButton1_Click()
Dim wb As Workbook, wbhold As Workbook
Dim ws As Worksheet, wshold As Worksheet
Dim holdCount As Integer
Dim cellColour As Long
Dim cell As Range, rng As Range
Set wb = Workbooks.Open("blahblah.xls")
Set wbhold = Workbooks.Open("blahblah2.xlsm")
holdCount = 0
cellColour = RGB(255, 153, 0)
rownumber = 0
For Each ws In wb.Worksheets
With ws
Set rng = ws.Range("A1:A20")
For Each cell In rng
rownumber = rownumber + 1
If cell.Interior.Color = cellColour Then
Range("A" & rownumber & ":B" & rownumber).Select
Selection.Copy
wbhold.Activate
Sheets("Hold Data").Activate
Cells.Offset(1, 0).PasteSpecial
Application.CutCopyMode = False
With Selection.Font
.Name = "Arial"
.Size = 10
wb.Activate
End With
holdCount = holdCount + 1
End If
Next cell
End With
Next ws
Application.DisplayAlerts = False
wb.Close
MsgBox "found " & holdCount
End Sub
但行:Sheets("Hold Data").Activate
不斷拋出了一個「下標越界」的錯誤。我一直在玩代碼大約2個小時,試圖讓它起作用,但無濟於事。有任何想法嗎?
你在哪裏實際上是試圖粘貼到 - 哪些細胞? – Rory
它只需要開始粘貼到A1和B1 [它只是複製每行的兩個數據單元],然後在循環的每次迭代中向下移動一行。我是VBA的總新手[因爲你可以說],所以它需要的時間比它應該。 – WaltVinegar