2017-03-02 85 views
0

我不明白爲什麼Excel不喜歡這段代碼,我已經介紹了所有關於如何解決它的(儘管)有限的知識。Excel錯誤424需要幫助

我的代碼調用我在網上找到的ProperUnion代碼,它應該處理空範圍和重複。我只掌握了第二位代碼如何工作的基本知識。第一位是我的。

該代碼根據它們是否被標記來選擇項目列表,並將每個標誌保存爲一個範圍,然後根據需要將它們相交。在我測試的情況下,只有「Flag 3」框被選中,這可能是導致問題的原因。 (所以rngx(1)rngx(2)rngx(4)+都是空值)。

我把'xxxxxxx在正確的聯盟的線,這給我的調試錯誤。

任何和所有的幫助,非常感謝。

收集已標記的信息編碼

Sub GSFlagged(prg As String) 'prg is the Program Name 
Dim rng As Range 
Dim rngA As Range 
Dim rngx(1 To 8) As Variant 
Dim rngu As Range 
Dim r As Long 
Dim wsMaster As Worksheet 
Dim wsGenScore As Worksheet 
Dim wsScore As Worksheet 

Set wsMaster = Worksheets("Master List") 
Set wsGenScore = Worksheets("Generate Scorecard") 
Set wsScore = Worksheets("Scorecard") 

wsMaster.Activate 
'Make sure that the master list is not filtered 
    If wsMaster.AutoFilterMode = True Then 
    wsMaster.AutoFilterMode = False 
    End If 

'Select all data in the Masterlist and then remove the headers 
Set rng = wsMaster.Range("B4:E4", Range("B4:E4").End(xlDown)) 
Set rng = rng.Offset(1, 0).Resize(rng.Rows.Count - 1) 

'Filter by the program name 
    wsMaster.Range("B4").AutoFilter Field:=2, Criteria1:=prg 
    Set rngA = rng.SpecialCells(xlCellTypeVisible) 
'Filter by flags with a loop over the variable r and save each set of visible cells as rngx(r) 
    For r = 1 To 8 
     If wsGenScore.Shapes("Flag" & r).ControlFormat.Value = 1 Then 
      wsMaster.Activate 
      If wsMaster.AutoFilterMode = True Then 
       wsMaster.AutoFilterMode = False 
      End If 
     wsMaster.Range("B4").AutoFilter Field:=r + 6, Criteria1:="<>" 
     Set rngx(r) = rng.SpecialCells(xlCellTypeVisible) 
     End If 
    Next r 
'After filtering through all the SKUs we union them using Proper Union a Custom VBA that allows for null values and removes duplicates. 
    Set rngu = ProperUnion(rngx(1), rngx(2), rngx(3), rngx(4), rngx(5), rngx(6), rngx(7), rngx(8)) 
'Now that we have rngu which is the union of all flagged SKUs we want to intersect that with the SKUs that are in the chosen program. 
    Set rngi = Intersect(rngA, rngu) 
End Sub 

正確的聯盟代碼 來源:http://www.cpearson.com/Excel/BetterUnion.aspx

Function ProperUnion(ParamArray Ranges() As Variant) As Range 
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    ' ProperUnion 
    ' This provides Union functionality without duplicating 
    ' cells when ranges overlap. Requires the Union2 function. 
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
     Dim ResR As Range 
     Dim n As Long 
     Dim r As Range 

     If Not Ranges(LBound(Ranges)) Is Nothing Then 'xxxxxxxxxx 
      Set ResR = Ranges(LBound(Ranges)) 
     End If 
     For n = LBound(Ranges) + 1 To UBound(Ranges) 
      If Not Ranges(n) Is Nothing Then 
       For Each r In Ranges(n).Cells 
        If Application.Intersect(ResR, r) Is Nothing Then 
         Set ResR = Union2(ResR, r) 
        End If 
       Next r 
      End If 
     Next n 
     Set ProperUnion = ResR 
    End Function 
'Union2 is required for ProperUnion 

Function Union2(ParamArray Ranges() As Variant) As Range 
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
    ' Union2 
    ' A Union operation that accepts parameters that are Nothing. 
    '''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
     Dim n As Long 
     Dim RR As Range 
     For n = LBound(Ranges) To UBound(Ranges) 
      If IsObject(Ranges(n)) Then 
       If Not Ranges(n) Is Nothing Then 
        If TypeOf Ranges(n) Is Excel.Range Then 
         If Not RR Is Nothing Then 
          Set RR = Application.Union(RR, Ranges(n)) 
         Else 
          Set RR = Ranges(n) 
         End If 
        End If 
       End If 
      End If 
     Next n 
     Set Union2 = RR 
    End Function 
+1

您應該引用「ProperUnion」的來源來滿足該網站的要求 - [資料來源:www.cpearson.com/Excel/BetterUnion.aspx版權2013,Charles H. Pearson](http://www.cpearson.com /Excel/BetterUnion.aspx) –

+0

我很抱歉。我忘了那個規則。我已經在我的項目的頂部模塊中引用了它,但忘記了將其鏈接到此處。非常感謝你提及它。我現在將它添加到帖子中。 –

回答

1

您已經聲明rngx是一個Variant陣列,但它應該被聲明爲Range對象的數組。

所以更改聲明是:

Dim rngx(1 To 8) As Range 

上述代碼中,rngx未分配的元素被傳遞到ProperUnion與類型的Variant/Empty,這就是爲什麼它崩潰。通過將rngx更改爲Range,參數將作爲Variant/Range傳遞,未賦值的元素爲Nothing

+0

嗯....現在我得到錯誤5:無效的過程就行:'如果Application.Intersect(ResR,r)沒有那麼',但你確實解決了我最初的問題,所以我肯定會接受這個我可以回答。 –

+1

我不認爲'ProperUnion'將作爲第一個範圍使用'Nothing',因爲'Application.Intersect'不會接受'Nothing'作爲其參數之一。我認爲它將需要改變使用'如果ResR沒有那麼''設置ResR = r''否則ApplicationIntersect(ResR,r)沒有那麼設置ResR = Union2(ResR,r)''End If'。 – YowE3K

+0

現在完美!非常感謝你! –