如何导入.txt文件,以及如何使用VBA将导入的.txt文件名添加到Excel工作表中的单独单元格中

如何导入.txt文件,以及如何使用VBA将导入的.txt文件名添加到Excel工作表中的单独单元格中,excel,vba,import,Excel,Vba,Import,我正在寻找一种方法,将.txt文件导入Excel工作表,并在同一工作表中的单独单元格中添加文件名(比如P06_113.txt) 我寻找一个函数,该函数在导入时提取文件名,然后将文件名复制并粘贴到给定的单元格中 Sub Import() Dim myFile As Variant myFile = Application.GetOpenFilename(FileFilter:="TXT Files, *.txt", Title:="Select File To Be Opened") Do Wh

我正在寻找一种方法,将.txt文件导入Excel工作表,并在同一工作表中的单独单元格中添加文件名(比如P06_113.txt)

我寻找一个函数,该函数在导入时提取文件名,然后将文件名复制并粘贴到给定的单元格中

Sub Import()
Dim myFile As Variant
myFile = Application.GetOpenFilename(FileFilter:="TXT Files, *.txt", 
Title:="Select File To Be Opened")
Do While myFile <> vbNullString
If myFile = False Then Exit Sub

With ActiveSheet.QueryTables.Add(Connection:= _
    "TEXT;" & myFile _
    , Destination:=Range("$A$1"))
    .Name = myFile
    .FieldNames = True
    .RowNumbers = False
    .FillAdjacentFormulas = False
    .PreserveFormatting = True
    .RefreshOnFileOpen = False
    .RefreshStyle = xlInsertDeleteCells
    .SavePassword = False
    .SaveData = True
    .AdjustColumnWidth = True
    .RefreshPeriod = 0
    .TextFilePromptOnRefresh = False
    .TextFilePlatform = 850
    .TextFileStartRow = 1
    .TextFileParseType = xlFixedWidth
    .TextFileTextQualifier = xlTextQualifierDoubleQuote
    .TextFileConsecutiveDelimiter = False
    .TextFileTabDelimiter = True
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = False
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1, 1, 1)
    .TextFileFixedColumnWidths = Array(8, 4, 6)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
End With   
End Sub
子导入()
Dim myFile作为变量
myFile=Application.GetOpenFilename(文件过滤器:=“TXT文件,*.TXT”,
标题:=“选择要打开的文件”)
在myFile vbNullString时执行此操作
如果myFile=False,则退出Sub
使用ActiveSheet.QueryTables.Add(连接:=_
“TEXT;”&myFile_
,目的地:=范围(“$A$1”))
.Name=myFile
.FieldNames=True
.rowNumber=False
.FillAdjacentFormulas=False
.PreserveFormatting=True
.refreshinfoleopen=False
.RefreshStyle=xlInsertDeleteCells
.SavePassword=False
.SaveData=True
.AdjustColumnWidth=True
.RefreshPeriod=0
.TextFilePromptOnRefresh=False
.TextFilePlatform=850
.TextFileStartRow=1
.TextFileParseType=xlFixedWidth
.TextFileTextQualifier=xlTextQualifierDoubleQuote
.textfileconsutivedelimiter=False
.TextFileTabDelimiter=True
.TextFileSemicolonDelimiter=False
.textfilecommadelimitor=False
.TextFileSpaceDelimiter=False
.TextFileColumnDataTypes=数组(1,1,1,1)
.TextFileFixedColumnWidths=数组(8,4,6)
.TextFileTrailingMinusNumbers=True
.Refresh BackgroundQuery:=False
以
端接头
预期结果将是:

范例

一年的数据

数据b年

数据c年


“Filename.txt”

这是我通过
文件对话框
对象导入文件的方法,也是一种“傻瓜式”方法,允许以独占方式导入
.txt
文件,以及是否保留以前的数据

Public Function get_file(ByVal format as String) As String
    'File Dialogue picker by Rawrplus

    Dim dia As FileDialog
    Dim res As String

prompt:
    Set dia = Application.FileDialog(msoFileDialogFilePicker)
    With dia
        .Title = "CHoose a file"
        .AllowMultiSelect = False
        .InitialFileName = Application.DefaultFilePath
        If .Show <> -1 Then
            GoTo esc_bridge
        End If

        res = .SelectedItems(1)
    End With

esc_bridge:
    If Not Right(res, Len(format)) = format Then
        MsgBox "Please select a ." & format & " file!"
        GoTo prompt
    End If

    get_file = res
    Set dia = Nothing

End Function
所以调用看起来像这样:

Private Sub import_file()
  Dim ws as Worksheet: Set ws = Sheets("Paste data to this sheet") 'change me
  Dim path as String: path = get_file("txt")
  read_file path, ws
End Sub

您可以从
myFile
获取文件名。这里有一些关于堆栈溢出的示例,可以从完整路径中提取文件名。
Private Sub import_file()
  Dim ws as Worksheet: Set ws = Sheets("Paste data to this sheet") 'change me
  Dim path as String: path = get_file("txt")
  read_file path, ws
End Sub