2017-06-26 175 views
0

我正在運行以下宏將多個工作表的列數據合併到同一工作簿中的一個工作表(一列)中。 Workbook- ThisWorkbooksVBA excell運行時錯誤424對象未找到

Public Sub Test() 

Dim lRow As Long 
Dim sh As Worksheet 
Dim shArc As Worksheet 

Set shArc = ThisWorkbooks.Worksheets("Archive") 
For Each sh In ThisWorkbooks.Worksheets 
    Select Case sh.Name 
     Case Is <> "Archive" 
      lRow = shArc.Range("A" & Rows.Count).End(xlUp).Row 
      sh.Range("A1:A1000").Copy _ 
       Destination:=shArc.Range("A" & lRow + 1) 
    End Select 
Next 

Set shArc = Nothing 
Set sh = Nothing 

End Sub 

的 Nmae你能告訴我什麼是必須是錯在這裏。

回答

1

它是ThisWorkbook而不是ThisWorkbooks

最後放棄s,你應該很好去。

編輯:我看到你重命名你的ThisWorkbook模塊被稱爲ThisWorkbooks? 如果是這樣的話 - 上面應該可以正常運行,除非您沒有工作表中的選項卡名稱爲「存檔」。

0

嘗試下面的代碼,您的模塊的頂部添加Option Explicit會幫助你檢測拼寫錯誤)

Option Explicit 

' rest of your code, bla bla bla 

' your loop (I like using the `With` inside) 
For Each sh In ThisWorkbook.Worksheets 
    With sh 
     Select Case .Name 
      Case Is <> "Archive" 
       lRow = shArc.Range("A" & shArc.Rows.Count).End(xlUp).Row 
       .Range("A1:A1000").Copy Destination:=shArc.Range("A" & lRow + 1) 
     End Select 
    End With 
Next 
0

下面的代碼工作。我將我的Excel文件重命名爲「Workbook」並應用下面的邏輯。

Public Sub Test() 
    Dim lRow As Long 
    Dim sh As Worksheet 
    Dim shArc As Worksheet 
    Set shArc = ThisWorkbook.Worksheets("Archive") 
    For Each sh In ThisWorkbook.Worksheets 
     Select Case sh.Name 
      Case Is <> "Archive" 
       lRow = shArc.Range("A" & Rows.Count).End(xlUp).Row 
       sh.Range("A1:A2000").Copy _ 
        Destination:=shArc.Range("A" & lRow + 1) 
     End Select 
    Next 
    Set shArc = Nothing 
    Set sh = Nothing 
End Sub 

如果你看,連我試圖找到答案,一個問題就如何做這項工作?