简体   繁体   中英

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

I can save this single file as .xls file using this code

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

Here are my questions:

  1. How can i access a bunch of CSV files in a folder C:\\Users\\karthic.rangaraj\\Desktop\\CSVFiles & Save it as a XLS file in the same folder after the process is done in the same file names using VBA?

  2. I have a problem in the array:

Code:

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)

if enter more than 10 items (let's say 14) i'm getting this error message:

Unable to set the property of the class range FormulaArray

What's wrong with the array formula here?

VBA可以创建的arrayformula的最大长度为255个字符

Starting from Charles Williams answer, you could put the software list array in a (optionally hidden) "named constant" using Define Names, and use that named array in your formula to make it shorter.

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

...

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

That would make the lenght of your formula independent from the number of items.

So whats wrong with this simpler formula?

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)

EDIT:

OK Now I understand what you are trying to do: try this code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM