![](/img/trans.png)
[英]Arrayformula shows error: Unable to set the formulaarray property of the range class
[英]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)
这是我的问题:
使用VBA以相同的文件名完成该过程后,如何访问文件夹C:\\Users\\karthic.rangaraj\\Desktop\\CSVFiles
的一堆CSV文件C:\\Users\\karthic.rangaraj\\Desktop\\CSVFiles
另存为同一文件夹中的XLS文件?
我在数组中有问题:
码:
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.