2016-10-27 5 views
0

私は基本的に、csvファイルを手動で取り込み、TESTという名前のシートに配置します。ワークブック内に自動的に作成される新しいシートにインポートする必要があります。ここでは、コード.... greatfully私はそれが十分に簡単であると確信していますが、私がここにいる理由それ故にオンライン解答や参考書を見つけることができません受け取っマクロを使用してファイルをワークブック内の新しいシートにインポートする方法

Sub load_csv() 
    Dim fStr As String 

    With Application.FileDialog(msoFileDialogFilePicker) 
     .Show 
     If .SelectedItems.Count = 0 Then 
      MsgBox "Cancel Selected" 
      Exit Sub 
     End If 
     'fStr is the file path and name of the file you selected. 
     fStr = .SelectedItems(1) 
    End With 

    With ThisWorkbook.Sheets("TEST").QueryTables.Add(Connection:= _ 
    "TEXT;" & fStr, Destination:=ThisWorkbook.Sheets("TEST").Range("$A$1")) 
     .Name = "CAPTURE" 
     .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 = True 
     .TextFileSpaceDelimiter = False 
     .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
     .TextFileTrailingMinusNumbers = True 
     .Refresh BackgroundQuery:=False 

    End With 
End Sub 

答えがあります。

答えて

1

あなたのワークブックから新しいワークシートを作成し、CSVファイルをコピーするためにそれを使用します。YowE3K @

Dim NewSheet As Worksheet 
Set NewSheet = ThisWorkbook.Worksheets.Add 
NewSheet.Name = "NewSheet" 

With NewSheet.QueryTables.Add(Connection:= _ 
"TEXT;" & fStr, Destination:=NewSheet.Range("$A$1")) 
    .Name = "CAPTURE" 
    .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 = True 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 

End With 
+0

をアドバイスしてくれてありがとうは、ただ与えられた答えを編集しました。.. – AugustoQ

関連する問題