打开文本文件并使用感叹号作为分隔符的 Split()
Open Text File and Split() with exclamation mark as delimiter
我想应该很简单,但似乎无法让它工作。尝试了两种方法将文本文件 (.txt) 打开到 excel 并按其他选项拆分:“!”
Set workbookbk = Workbooks.OpenText(fullfilepath, xlMSDOS, _
xlDelimited, xlDoubleQuote, False, False, False, False, False, True, "!")
这个语法很好,但是它说 "Compile Error: Expected Function or Variable" 我错过了什么?!
我也用过
Set workbk = Workbooks.Open filename:= fullfilepath, Delimiter:= "!"
这可以正常打开文件,但根本不会拆分文件
帮助非常感谢
如果您要跳过方法中的参数,则需要明确指定您提供哪些参数:
Workbook.OpenText语法(来自MSDN)
expression.OpenText(Filename, Origin, StartRow, DataType, TextQualifier, ConsecutiveDelimiter, Tab, Semicolon, Comma, Space, Other, OtherChar, FieldInfo, TextVisualLayout, DecimalSeparator, ThousandsSeparator, TrailingMinusNumbers, Local)
expression A variable that represents a Workbooks object.
在您的代码中,您遗漏了一些参数,但编译器会假设您已经按照该方法的预期顺序提供了参数除非您指定你传递的参数。例如:
Workbooks.OpenText Filename:=fuullfilepath, Origin:=xlMSDOS, DataType:=xlDelimited, Other:=True, OtherChar:="!"
此外,这可能不是您所希望的答案,但我不久前写了一个 UDF 来完成类似的事情而没有使用 .OpenText()
方法:
Function MM_OpenTextFile(vPath As String, delim As String) As Variant
Dim FF As Integer
Dim lineArray As Variant
Dim temp As String
Dim arrayList As Object
Set arrayList = CreateObject("System.Collections.ArrayList")
FF = FreeFile
Open vPath For Input As #FF
While Not EOF(FF)
Line Input #FF, temp
lineArray = Split(temp, delim)
arrayList.Add lineArray
Erase lineArray
Wend
Close #FF
MM_OpenTextFile = WorksheetFunction.Transpose(WorksheetFunction.Transpose(arrayList.ToArray()))
arrayList.Clear
Set arrayList = Nothing
End Function
下面是您将如何使用它的示例:
Sub Foo()
Dim ar As Variant '// Must be a Variant to work
'// Change to a file and delimiter of your choosing...
ar = MM_OpenTextFile("C:\Users\Macro Man\SomeFile.txt", "!")
With Range("A1").Resize(UBound(ar, 1), UBound(ar, 2))
.NumberFormat = "@" '// Change format to "text"
.Value = ar '// insert array values
End With
End Sub
我想应该很简单,但似乎无法让它工作。尝试了两种方法将文本文件 (.txt) 打开到 excel 并按其他选项拆分:“!”
Set workbookbk = Workbooks.OpenText(fullfilepath, xlMSDOS, _
xlDelimited, xlDoubleQuote, False, False, False, False, False, True, "!")
这个语法很好,但是它说 "Compile Error: Expected Function or Variable" 我错过了什么?!
我也用过
Set workbk = Workbooks.Open filename:= fullfilepath, Delimiter:= "!"
这可以正常打开文件,但根本不会拆分文件
帮助非常感谢
如果您要跳过方法中的参数,则需要明确指定您提供哪些参数:
Workbook.OpenText语法(来自MSDN)
expression.OpenText(Filename, Origin, StartRow, DataType, TextQualifier, ConsecutiveDelimiter, Tab, Semicolon, Comma, Space, Other, OtherChar, FieldInfo, TextVisualLayout, DecimalSeparator, ThousandsSeparator, TrailingMinusNumbers, Local)
expression A variable that represents a Workbooks object.
在您的代码中,您遗漏了一些参数,但编译器会假设您已经按照该方法的预期顺序提供了参数除非您指定你传递的参数。例如:
Workbooks.OpenText Filename:=fuullfilepath, Origin:=xlMSDOS, DataType:=xlDelimited, Other:=True, OtherChar:="!"
此外,这可能不是您所希望的答案,但我不久前写了一个 UDF 来完成类似的事情而没有使用 .OpenText()
方法:
Function MM_OpenTextFile(vPath As String, delim As String) As Variant
Dim FF As Integer
Dim lineArray As Variant
Dim temp As String
Dim arrayList As Object
Set arrayList = CreateObject("System.Collections.ArrayList")
FF = FreeFile
Open vPath For Input As #FF
While Not EOF(FF)
Line Input #FF, temp
lineArray = Split(temp, delim)
arrayList.Add lineArray
Erase lineArray
Wend
Close #FF
MM_OpenTextFile = WorksheetFunction.Transpose(WorksheetFunction.Transpose(arrayList.ToArray()))
arrayList.Clear
Set arrayList = Nothing
End Function
下面是您将如何使用它的示例:
Sub Foo()
Dim ar As Variant '// Must be a Variant to work
'// Change to a file and delimiter of your choosing...
ar = MM_OpenTextFile("C:\Users\Macro Man\SomeFile.txt", "!")
With Range("A1").Resize(UBound(ar, 1), UBound(ar, 2))
.NumberFormat = "@" '// Change format to "text"
.Value = ar '// insert array values
End With
End Sub