无法在VBA中设置类范围FormulaArray的属性

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. 如何在一个文件夹C:\Users\karthic.rangaraj\Desktop\CSVFiles访问一堆CSV文件并在使用VBA在相同文件名中完成该过程之后将其另存为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) 

如果input超过10个项目(比方说14)我收到此错误信息:

 Unable to set the property of the class range FormulaArray 

这里的数组公式出了什么问题?

那么这个简单的公式怎么了?

 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 

VBA可以创build的最大数组长度是255个字符

从Charles Williams的答案开始,可以使用Define Names将软件列表数组置于(可选隐藏)“命名常量”中,并在公式中使用该命名数组来缩短它。

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

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

这将使你的公式的长度与项目的数量无关。