वीबीए एक्सेल में रन-टाइम त्रुटि ‘1004’ एप्लिकेशन-परिभाषित या ऑब्जेक्ट-परिभाषित त्रुटि


मैंने एक कोड लिखा है जो बिना किसी रिक्त स्थान के गतिशील ड्रॉप-डाउन सूची बनाता है
और यह बहुत अच्छा काम करता है, लेकिन एकमात्र समस्या यह है कि जब मैं किसी ऑब्जेक्ट (चित्र, बटन इत्यादि) पर क्लिक करते समय मार्को चलाता हूं तो यह रन-टाइम त्रुटि ‘1004’ एप्लिकेशन-परिभाषित या ऑब्जेक्ट-परिभाषित त्रुटि दिखाता है

मैंने वीबीए में डिबगर का उपयोग किया है और यह इन दोनों में त्रुटि दिखाता है

.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
     xlBetween, Formula1:=Join(Application.Transpose(valuesArray), ",")

और यह पूरा कोड है, कृपया मदद करें

Sub DynamicDropDown()
    Dim sourceSheet As Worksheet
    Dim destinationSheet As Worksheet
    Dim sourceRange As Range
    Dim destinationRange As Range
    Dim cell As Range
    Dim validationFormula As String
    Dim nonBlankValues As Collection
    Dim dropDownCell As Range
    
    ' Set source and destination sheets
    Set sourceSheet = ThisWorkbook.Sheets("Sheet1")
    Set destinationSheet = ThisWorkbook.Sheets("Sheet2")
    
    ' Define the source range (adjust the range as needed)
    Set sourceRange = sourceSheet.Range("A1:A" & sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row)
    
    ' Define the destination range where you want the drop-down list
    Set destinationRange = destinationSheet.Range("N10")
    
    
    ' Create a collection to store non-blank values
    Set nonBlankValues = New Collection
    
    ' Collect non-blank values from the source range
    On Error Resume Next
    For Each cell In sourceRange
        If cell.Value <> "" Then
            nonBlankValues.Add cell.Value, CStr(cell.Value)
        End If
    Next cell
    On Error GoTo 0
    
    ' Convert the collection to an array
    Dim valuesArray() As Variant
    ReDim valuesArray(1 To nonBlankValues.Count, 1 To 1)
    
    Dim i As Integer
    For i = 1 To nonBlankValues.Count
        valuesArray(i, 1) = nonBlankValues(i)
    Next i
    
    ' Set the validation formula
    
    destinationRange.Validation.Delete
    With destinationRange.Validation
        .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
             xlBetween, Formula1:=Join(Application.Transpose(valuesArray), ",")
        .IgnoreBlank = True
        .InCellDropdown = True
        .ShowInput = True
        .ShowError = True
    End With
    
End Sub

मैंने क्या प्रयास किया है:

मैंने वह सब कुछ आज़माया है जो मैं जानता हूँ लेकिन कुछ भी काम नहीं आया

समाधान 1

इस पंक्ति को विभाजित करने का प्रयास करें:

वीबीए
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
     xlBetween, Formula1:=Join(Application.Transpose(valuesArray), ",")

में:

वीबीए
Dim result As String
result  = Join(Application.Transpose(valuesArray), ",")
.Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
     xlBetween, Formula1:=result 

コメント

タイトルとURLをコピーしました