VBA错误代码438 - 最有可能设置活动工作表问题

时间:2014-01-10 18:49:18

标签: vba excel-vba excel

我正在处理通过谷歌找到的代码。最初代码设置为创建一个新的摘要表,但是我想使用已经存在的代码并将新数据粘贴到下一个空行中。当我设置摘要工作表时,似乎会发生这个问题。我在这行代码中得到了“运行时错误'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

1 个答案:

答案 0 :(得分:0)

尝试改变

Set DestSh = ActiveWorkbook.Worksheet("Tab_Upload").Activate

Set DestSh = ActiveWorkbook.Worksheets("Tab_Upload")

您在s

的末尾错过了Worksheets