2017-05-21 28 views
0

私の目標は、フォルダ内のtxtファイルをエクスポートして1つのExcelシートに統合することです。ファイル名とtxtファイルの保存日をそれぞれ別々の列。Excelでtxtファイルを統合しようとしているときに実行時エラー1004

以下のコードでExcelのフォルダ内のtxtファイルを統合しようとしていますが、でランタイムエラー1004が発生します.Refresh BackgroundQuery:= Trueなぜエラーが表示されますか?

Sub test() 

Dim wbk As Workbook, wksht As Worksheet 
    Dim xStrPath As String 
    Dim xFileDialog As FileDialog 
    Dim xFile As String 
    Dim xFiles As New Collection 
    Dim I As Long 

    Set xFileDialog = Application.FileDialog(msoFileDialogFolderPicker) 
    xFileDialog.AllowMultiSelect = False 
    xFileDialog.Title = "Select a folder" 
    If xFileDialog.Show = -1 Then 
     xStrPath = xFileDialog.SelectedItems(1) 
    End If 
    If xStrPath = "" Then Exit Sub 

    If Right(xStrPath, 1) <> "\" Then xStrPath = xStrPath & "\" 
    xFile = Dir(xStrPath & "*.txt") 
    If xFile = "" Then 
     MsgBox "No files found", vbInformation, "eBay" 
     Exit Sub 
    End If 

Workbooks.Add 
Set wbk = ActiveWorkbook 
Set wksht = ActiveSheet 

Do While xFile <> "" 
     xFiles.Add xFile, xFile 
     xFile = Dir() 
Loop 

If xFiles.Count > 0 Then 

For I = 1 To xFiles.Count 
'On Error Resume Next 
    With wksht.QueryTables.Add(Connection:="TEXT;" & xFile, Destination:=Range("$A$1")) 
    .Name = xFile 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 65001 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierNone 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = True 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = False 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=True 

    End With 

Next 
End If 

End Sub 

error

他の簡単な方法はありますか?ここで

+0

何行では? –

+0

@DavidGこの行で。リフレッシュBackgroundQuery:= True – Linga

+0

はい私はwksht.QueryTables.Add(接続:= "TEXT;"&xStrPath&xFiles.Item(I)、宛先:=範囲( "$ A $ 1 ")) と.Name = xFiles.Item(I) – Linga

答えて

2

は修正され、ヘルプありがとうございました:)あなたはエラーが出るん

With wksht.QueryTables.Add(Connection:="TEXT;" & xStrPath & xFiles.Item(I), Destination:=Range("$A$1")) 
.Name = xFiles.Item(I) 
関連する問題