Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 如何修改此代码以通过FileDialog导入文本文件?_Excel_Vba - Fatal编程技术网

Excel 如何修改此代码以通过FileDialog导入文本文件?

Excel 如何修改此代码以通过FileDialog导入文本文件?,excel,vba,Excel,Vba,您好,我正在尝试从设置的路径导入基于FileDialog的文件夹中的文本文件?我有一个导入文本文件的代码,但它只打开一个通用的C:\\路径,我应该如何修改下面的代码才能打开指定路径的文件夹 Sub ImportTextFile() Dim fName As String, LastRow As Long fName = Application.GetOpenFilename("Text Files (*.txt), *.txt") If fName = "False" Then Exit Su

您好,我正在尝试从设置的路径导入基于
FileDialog
的文件夹中的文本文件?我有一个导入文本文件的代码,但它只打开一个通用的
C:\\
路径,我应该如何修改下面的代码才能打开指定路径的文件夹

Sub ImportTextFile()
Dim fName As String, LastRow As Long

fName = Application.GetOpenFilename("Text Files (*.txt), *.txt")
If fName = "False" Then Exit Sub

LastRow = Range("A" & Rows.Count).End(xlUp).Row + 1

    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" & fName, _
        Destination:=Range("A" & LastRow))
            .Name = "sample"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierNone
            .TextFileConsecutiveDelimiter = True
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileOtherDelimiter = "" & Chr(10) & ""
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, _
               1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
               1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
               1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
    End With
End Sub
任何帮助都将不胜感激

我对代码的开头做了如下修改,但现在得到了“1004应用程序定义错误”

在打开文件之前使用可能会有所帮助。我会发表评论,但没有足够的声誉,所以张贴在这里

e、 g


如何将ChDir应用为导入源?这可能有助于:
Sub ImportTextFile()
Dim fName As FileDialog, LastRow As Long

Set fName = Application.FileDialog(msoFileDialogOpen)
fName.InitialFileName = "\\olscmesf003\gcm_emea\TCU_REPORTS\APPS\Reports\Regional\Pointsec for PC  Web RH\2017\"
If fName = "False" Then Exit Sub
Sub ImportTextFile()
ChDir "C:/yourpath/folder/etc"
Dim fName As String, LastRow As Long
....