我有一个文本文件,格式如下:
我在VBA中使用以下代码将文本文件写入excel:
Sub Test()
Dim Fn As String, WS As Worksheet, st As String
Fn = "Path.txt" ' the file path and name
Set WS = Sheets("Sheet1")
'Read text file to st string
With CreateObject("Scripting.FileSystemObject")
If Not .FileExists(Fn) Then
MsgBox Fn & " : is missing."
Exit Sub
Else
If FileLen(Fn) = 0 Then
MsgBox Fn & " : is empty"
Exit Sub
Else
With .OpenTextFile(Fn, 1)
st = .ReadAll
.Close
End With
End If
End If
End With
'Replace …Run Code Online (Sandbox Code Playgroud)