我不明白爲什麼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
您應該引用「ProperUnion」的來源來滿足該網站的要求 - [資料來源:www.cpearson.com/Excel/BetterUnion.aspx版權2013,Charles H. Pearson](http://www.cpearson.com /Excel/BetterUnion.aspx) –
我很抱歉。我忘了那個規則。我已經在我的項目的頂部模塊中引用了它,但忘記了將其鏈接到此處。非常感謝你提及它。我現在將它添加到帖子中。 –