0

外部データの取得オプションを使用してテキスト ファイルをインポートするマクロがあります。このコードでユーザーにファイルを選択するように求める方法はありますか、それとも別のアプローチを取る必要がありますか? インポート中に一部の列を除外できるため、このアプローチが気に入っていますが、他のオプションも受け入れています。

With Sheets(1).QueryTables.Add(Connection:= _
        "TEXT;C:\Program Files\SubDirectory\ThisIsMyFile.txt" _
        , Destination:=Range("$A$1"))
        .Name = "ThisIsMyFile"
        .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 = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(9, 9, 9, 2, 9, 2, 9, 9, 9, 2, 9, 9, 9, 2, 2, 9, 2, 9, 2, 9, 2, _
        9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 9, 9, 2, 9, 2, 9, 2, 9, 2 _
        , 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 9, 9, 9, 9, 2, 9, 9, 9, 2, 9, 2, 9, 9, 9, _
        2, 9, 9, 2, 2, 2)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With

前もって感謝します!

助けてくれてありがとう。私はこのコードを動作させました:

  Dim FileFilter As String
  Dim Filename As String
  Dim SrcWkb As Workbook
  MsgBox "Select file"
  FileFilter = "Text Files (*.txt), *.txt"
  Filename = Application.GetOpenFilename(FileFilter, 1)

    If Filename = "False" Then Exit Sub
With Sheets(1).QueryTables.Add(Connection:= _
        "TEXT;" & Filename, Destination:=Range("$A$1"))
        .Name = Filename
        .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 = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(9, 9, 9, 2, 9, 2, 9, 9, 9, 2, 9, 9, 9, 2, 2, 9, 2, 9, 2, 9, 2, _
        9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 9, 9, 2, 9, 2, 9, 2, 9, 2 _
        , 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 9, 9, 9, 9, 2, 9, 9, 9, 2, 9, 2, 9, 9, 9, _
        2, 9, 9, 2, 2, 2)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
    End Sub
4

1 に答える 1

1

このようなものが動作するはずです:

Sub FileName()

fileToOpen = Application.GetOpenFilename("Text Files (*.txt), *.txt")

If fileToOpen <> False Then QueryTable fileToOpen

End Sub

Sub QueryTable(file As String)

With Sheets(1).QueryTables.Add(Connection:= _
        "TEXT;" & file, Destination:=Range("$A$1"))
        .Name = "ThisIsMyFile"
        .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 = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = True
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = False
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(9, 9, 9, 2, 9, 2, 9, 9, 9, 2, 9, 9, 9, 2, 2, 9, 2, 9, 2, 9, 2, _
        9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 9, 9, 2, 9, 2, 9, 2, 9, 2 _
        , 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 2, 9, 9, 9, 9, 9, 2, 9, 9, 9, 2, 9, 2, 9, 9, 9, _
        2, 9, 9, 2, 2, 2)
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub
于 2012-09-20T15:38:11.373 に答える