小编use*_*987的帖子

使用VBA从文本文件写入excel时保留"列"

我有一个文本文件,格式如下:

在此输入图像描述

我在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)

regex excel vba excel-vba

6
推荐指数
1
解决办法
805
查看次数

标签 统计

excel ×1

excel-vba ×1

regex ×1

vba ×1