簡體   English   中英

無法在VBA中設置類范圍FormulaArray的屬性

[英]Unable to set the property of the class range FormulaArray in VBA

    Sub Parse()


        Workbooks.OpenText Filename:="C:\Users\karthic.rangaraj\Desktop\4401.csv"

         ' Parse it using comma and semicolon as delimiters
        Range(Range("A1"), Range("A1").End(xlDown)).TextToColumns _
        DataType:=xlDelimited, _
        TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
        Semicolon:=True, Comma:=True, Space:=False, Other:=False, _
        FieldInfo:= _
        Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 1), Array(5, 2))


        Range("B:B,D:D").Delete
         'Range("D1").FormulaR1C1 = "Application_ID"

         Dim lLR As Long
    Dim vArray As Variant
    Dim sString As String
     '*****************************************************************************************
     'Add as many items you like to this array
     '*****************************************************************************************
    vArray = Array("Windows XP", "Adobe", "IBM", "VLC", "PDFCreator", "Sonic", "Office", "Sigamtel", "Printer")
    For i = LBound(vArray) To UBound(vArray)
        sString = sString & Chr(34) & vArray(i) & Chr(34) & ","
    Next i
     '*****************************************************************************************
     'This is the final array string that we pass to array formula
     '*****************************************************************************************
    sString = "{" & Left(sString, Len(sString) - 1) & "}"
    lLR = Range("A" & Rows.Count).End(xlUp).Row
    Range("D2").FormulaArray = "=INDEX(" & sString & ",1,MATCH(1,--ISNUMBER(SEARCH(" & sString & ",$C2,1)),0))"


    Range("D2").AutoFill Destination:=Range("D2:D" & lLR)
ActiveWorkbook.SaveAs "C:\Users\karthic.rangaraj\Desktop\4401.xls", FileFormat:=56
' 52 = xlOpenXMLWorkbookMacroEnabled = xlsm (workbook with macro's in 2007)

    End Sub

我可以使用此代碼將單個文件另存為.xls文件

  ActiveWorkbook.SaveAs "C:\Users\karthic.rangaraj\Desktop\4401.xls", FileFormat:=56
    ' 52 = xlOpenXMLWorkbookMacroEnabled = xlsm (workbook with macro's in 2007)

這是我的問題:

  1. 使用VBA以相同的文件名完成該過程后,如何訪問文件夾C:\\Users\\karthic.rangaraj\\Desktop\\CSVFiles的一堆CSV文件C:\\Users\\karthic.rangaraj\\Desktop\\CSVFiles另存為同一文件夾中的XLS文件?

  2. 我在數組中有問題:

碼:

vArray = Array("Windows XP", "Adobe", "IBM", "VLC", ".Net Framework", "Office", "Java", "Windows Media", "J2SE", "MSXML")
        For i = LBound(vArray) To UBound(vArray)
            sString = sString & Chr(34) & vArray(i) & Chr(34) & ","
        Next i

sString = "{" & Left(sString, Len(sString) - 1) & "}"
lLR = Range("A" & Rows.Count).End(xlUp).Row
Range("D2").FormulaArray = "=INDEX(" & sString & ",1,MATCH(1,--ISNUMBER(SEARCH(" & sString & ",$C2,1)),0))"
Range("D2").AutoFill Destination:=Range("D2:D" & lLR)

如果輸入10個以上的項目(例如14個),我將收到此錯誤消息:

Unable to set the property of the class range FormulaArray

這里的數組公式有什么問題?

VBA可以創建的arrayformula的最大長度為255個字符

從Charles Williams的答案開始,您可以使用“定義名稱”將軟件列表數組放在(可選隱藏的)“命名常量”中,並在公式中使用該命名數組使其更短。

 ThisWorkbook.Names.Add Name:="MyList", _
                     RefersTo:=Array(Array("Windows XP", "Adobe", "IBM")

...

Range("D2").FormulaArray = "=INDEX(MyList,1,...

這將使您的公式的長度不受項目數量的影響。

那么,這個簡單的公式怎么了?

vArray = Array("Windows XP", "Adobe", "IBM", "VLC", ".Net Framework", "Office", "Java", "Windows Media", "J2SE", "MSXML")
        For i = LBound(vArray) To UBound(vArray)
            sString = sString & Chr(34) & vArray(i) & Chr(34) & ","
        Next i

sString = "{" & Left(sString, Len(sString) - 1) & "}"
lLR = Range("A" & Rows.Count).End(xlUp).Row
Range("D2").Formula = "=HLOOKUP($C2," & sString & ",1,FALSE)"
Range("D2").AutoFill Destination:=Range("D2:D" & lLR)

編輯:

好的,現在我了解您要執行的操作:嘗試此代碼

Sub Joe()
vArray = Array("Windows XP", "Adobe", "IBM", "VLC", ".Net Framework", "Office", "Java", "Windows Media", "J2SE", "MSXML")
        For i = LBound(vArray) To UBound(vArray)
            sstring = sstring & Chr(34) & vArray(i) & Chr(34) & ","
        Next i
sstring = "{" & Left(sstring, Len(sstring) - 1) & "}"
lLR = Range("A" & Rows.Count).End(xlUp).Row
Range("E2").Formula = "=InList($C2," & sstring & ")"
Range("E2").AutoFill Destination:=Range("E2:E" & lLR)
End Sub
Function InList(theCell As Range, theList As Variant)
    Dim j As Long
    Dim str As String
    InList = CVErr(xlErrNA)
    str = UCase(Trim(CStr(theCell)))
    For j = LBound(theList) To UBound(theList)
        If str Like UCase("*" & theList(j) & "*") Then
            InList = theList(j)
            Exit For
        End If
    Next j
End Function

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM