2017-09-01 141 views
0

我得到的標題中提到,每當我從另一個引用特定子錯誤對象「_Worksheet」的方法「範圍」失敗。每當這個Sub被隔離運行時,它就能正常工作。Excel VBA。運行時錯誤「1004」:當子被稱爲

下面是代碼:

Sub UnitExtraction() 

    'On Sheet2 exists a table. This table has a Unit column. 
    'The Unit column holds either of the values of: 1, 
    '2, 3, 4, or 5. The purpose of this Sub routine is to 
    'extract the rows corresponding to each Unit and store 
    'them into a respective range, and then paste these Ranges 
    'to the right of the original table in numerical order. 

    'LastRow is necessary because the size of the original table can 
    'fluctuate. 
    Dim LastRow As Long 
    Dim ChartRange1 As Range 
    Dim ChartRange2 As Range 
    Dim ChartRange3 As Range 
    Dim ChartRange4 As Range 
    Dim ChartRange5 As Range 

    LastRow = Sheet2.UsedRange.Rows.Count 

    'The original table has 6 columns starting at Y1. 
    'Each Unit range is initialized with the original 
    'table's headers. 
    Set ChartRange1 = Sheet2.Range("Y1:AD1") 
    Set ChartRange2 = Sheet2.Range("Y1:AD1") 
    Set ChartRange3 = Sheet2.Range("Y1:AD1") 
    Set ChartRange4 = Sheet2.Range("Y1:AD1") 
    Set ChartRange5 = Sheet2.Range("Y1:AD1") 

    'For some reason this doesn't work unless run in isolation. 
    'This is the loop that fills each range with its rows. 
    'We start at Row 2 (below the headers) and continue to 
    'the last row of the table. The Z column corresponds 
    'to the Unit column in the table, hence the number 26. 
    'As you can see we are simply looping through each row 
    'checking to see which number is held in the Unit 
    'column and then storing that row in the correct 
    'range object. 
    For i = 2 To LastRow 
     If Sheet2.Cells(i, 26).Value = 1 Then 
      Set ChartRange1 = Union(ChartRange1, _ 
       Sheet2.Range(Cells(i, 25), Cells(i, 30))) 
     ElseIf Sheet2.Cells(i, 26).Value = 2 Then 
      Set ChartRange2 = Union(ChartRange2, _ 
       Sheet2.Range(Cells(i, 25), Cells(i, 30))) 
     ElseIf Sheet2.Cells(i, 26).Value = 3 Then 
      Set ChartRange3 = Union(ChartRange3, _ 
       Sheet2.Range(Cells(i, 25), Cells(i, 30))) 
     ElseIf Sheet2.Cells(i, 26).Value = 4 Then 
      Set ChartRange4 = Union(ChartRange4, _ 
       Sheet2.Range(Cells(i, 25), Cells(i, 30))) 
     ElseIf Sheet2.Cells(i, 26).Value = 5 Then 
      Set ChartRange5 = Union(ChartRange5, _ 
       Sheet2.Range(Cells(i, 25), Cells(i, 30))) 
     End If 
    Next 

End Sub 

因此,正如我說的,如果我只是獨自從VB編輯器運行此子,它似乎很好地工作。但是每當我從前面的Sub中調用這個Sub時,我會遇到上述錯誤。

更具體地,在第一ElseIf發生錯誤(這是高亮的線)。這是因爲2的值當前是原始表中的第一個條目。所以第一個If是錯誤的。

我希望能夠限制這個問題的複雜性,並且有人可以單獨從這些信息中辨別出問題,但是如果有人需要我發佈導致這個問題的代碼,我可以這樣做。

UnitExtraction子從「查詢」子叫(如果你願意),查詢外部數據庫,並在Sheet2上創建原始表。

目前有4種不同的「查詢」潛艇將在UnitExtraction調用。運行的Query Sub基於Sheet1上的User selected選項按鈕,決定從數據庫中檢索哪些統計信息。但是無論運行的特定查詢如何,這個小組仍然會運行從所有單元的原始表中提取每個單元。

感謝您對這個問題的任何幫助。

+0

我沒有看到'Sheet2'任何地方定義,它是全球可能? –

+0

工作表#是Excel工作表的代碼名稱。如果你願意,這是一個「硬名稱」,即使用戶更改工作表名稱時也是如此。我通常更喜歡使用代碼名稱,這樣用戶可以隨意更改電子表格的「軟名稱」。 http://www.techrepublic.com/blog/10-things/10-ways-to-reference-excel-workbooks-and-sheets-using-vba/ – ReducingDirt

+0

非常有趣!每天學些新東西。感謝您的鏈接! :) –

回答

1
Sheet2.Range(Cells(i, 25), Cells(i, 30))) 

應該是

Sheet2.Range(Sheet2.Cells(i, 25), Sheet2.Cells(i, 30))) 

時Sheet 2中不是活動片,因爲範圍()或細胞()不具有特定的工作表限定符你的代碼無法總是指向活動片(除了在一個工作表的代碼模塊,在那裏他們默認爲該表)

What is the default scope of worksheets and cells and range?

+1

哇,有趣..這解決了它。我認爲嵌套在Sheet2.Range中的單元格就足夠作爲Sheet限定符了。感謝您提供快速且有用的答案Tim! – ReducingDirt

+0

我認爲通常假定外部'Range()'表限定符調用「通過」調用內部'Cells()'調用:不幸的是,情況並非如此。 –