0
我正在關閉通過谷歌發現的代碼。最初的代碼被設置爲創建一個新的彙總表,但是我想使用一個已經存在的代碼並將新數據粘貼到下一個空行。當我設置摘要工作表時,問題似乎就會發生。我在這行代碼得到一個 「運行時錯誤 '438'」 -VBA錯誤代碼438 - 最有可能設置活動工作表問題
Set DestSh = ActiveWorkbook.Worksheet("Tab_Upload").Activate
當我使用下面的代碼:
Sub CopyRangeFromMultiWorksheets()
Dim sh As Worksheet
Dim DestSh As Worksheet
Dim Last As Long
Dim CopyRng As Range
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
' Set Summary Worksheet.
Set DestSh = ActiveWorkbook.Worksheet("Tab_Upload").Activate
' Loop through all worksheets and copy the data to the
' summary worksheet.
For Each sh In ActiveWorkbook.Worksheets
If LCase(Left(sh.Name, 1)) = "_" Then
' Find the last row with data on the summary worksheet.
Last = DestSh.[a65536].End(xlUp).Row
' Specify the range to place the data.
Set CopyRng = sh.Rows("A23,B8:S8")
' Test to see whether there are enough rows in the summary
' worksheet to copy all the data.
If Last + CopyRng.Rows.Count > DestSh.Rows.Count Then
MsgBox "There are not enough rows in the " & _
"summary worksheet to place the data."
GoTo ExitTheSub
End If
' This statement copies values and formats from each
' worksheet.
CopyRng.Copy
With DestSh.Cells(Last + 1, "A")
.PasteSpecial xlPasteValues
.PasteSpecial xlPasteFormats
Application.CutCopyMode = False
End With
End If
Next
ExitTheSub:
Application.Goto DestSh.Cells(1)
' AutoFit the column width in the summary sheet.
DestSh.Columns.AutoFit
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
年底似乎並沒有工作,雖然這並不
s
:Application.DisplayAlerts =假 上的錯誤繼續下一步 ActiveWorkbook.Worksheets(「Tab_Upload」)激活 在錯誤轉到0 Application.DisplayAlerts =真 – TessD我已經更新了我的答案。你在'工作表'中錯過了's'。立即嘗試 –
@TessD只要你知道,你在註釋中輸入的代碼所做的事情實際上並沒有修復錯誤。它只是將其隱藏起來並跳過違規的代碼行。這不是一個解決方案,如果你使用它,你會得到意想不到的結果。你應該在On Error Resume Next和On Error GoTo 0中包裝代碼的時間很少。不要陷入陷阱,認爲你已經通過忽略它來解決它。 – sous2817