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)
以下是我的问题:
如何访问文件夹C:\Users\karthic.rangaraj\Desktop\CSVFiles
中的一堆CSV文件
&安培;使用VBA在相同的文件名中完成该过程后,将其保存为同一文件夹中的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
这里的数组公式出了什么问题?
答案 0 :(得分:1)
VBA可以创建的arrayformula的最大长度为255个字符
答案 1 :(得分:1)
从Charles Williams的回答开始,您可以使用“定义名称”将软件列表数组放入(可选地隐藏)“命名常量”中,并在公式中使用该命名数组使其更短。
ThisWorkbook.Names.Add Name:="MyList", _
RefersTo:=Array(Array("Windows XP", "Adobe", "IBM")
...
Range("D2").FormulaArray = "=INDEX(MyList,1,...
这会使你的公式的长度与项目数无关。
答案 2 :(得分: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