2013-01-31 18 views
8

名前付きワークシートが複数あるマクロ対応Excelブックがあります。ワークシートの1つは「パネル」と呼ばれ、2番目のワークシートは「データ」と名付けられます。 「パネル」という名前のシートには、マクロが割り当てられたボタンがあります。 "panel"という名前のワークシート上のボタンを選択し、ファイルウィンドウのブラウズを表示したいと思います。ユーザーがハードドライブ上のcsvファイルを選択すると、csvファイルの内容をセルA1から始まる "data"という名前のワークシートにインポートします。マクロを使用してcsvファイルをExcelワークシートにインポートする

問題1:私がボタンに割り当てたvbaは、csvファイルの内容をボタン(「パネル」ワークシート)と同じワークシートに配置します。私はcsvファイルの内容を "データ"シートに載せたいと思います。

問題2:また、ハードドライブと「capture.csv」というファイルを参照するコード列があります。したがって、マクロ対応のExcelファイルが別のコンピュータにある場合、ファイルがクラッシュします。任意のコンピュータがファイルを使用できるように、パスウェイ文字列を削除する方法はありますか?

この問題を解決するための支援をいただければ幸いです。ボタンに割り当てられたマクロは次のとおりです。

Sub load_csv() 
Dim fStr As String 
With Application.FileDialog(msoFileDialogFilePicker) 
.Show 
If .SelectedItems.Count = 0 Then 
MsgBox "Cancel Selected" 
End 
End If 
'fStr is the file path and name of the file you selected. 
fStr = .SelectedItems(1) 
End With 
Range("A1").Select 
With ActiveSheet.QueryTables.Add(Connection:= _ 
"TEXT;C:\Users\laptop\Desktop\CAPTURE.csv", Destination:=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 
MsgBox fStr 
End With 
End Sub 

答えて

12

これはあなたが試みていることですか?

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("Data").QueryTables.Add(Connection:= _ 
    "TEXT;" & fStr, Destination:=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 
+0

。これは動作するはずです – Rick

+0

私を助けるために時間を割いていただきありがとうございます。私はあなたが書いたコードをボタンに割り当てました。 csvファイルを読み込むと、メッセージウィンドウが表示されます。 "実行時エラー '-2147024809(80070057)宛先範囲は、クエリテーブルが作成されている同じワークシートにありません。 – George

+1

'Destination:= Range(" $ A $ 1 "))'を 'Destination:= ThisWorkbook.Sheets(" Data ")に変更してください。 –

0

は、Mac上のExcelの場合は、クエリテーブルオブジェクトが「PreserveFormatting」と「RefreshPeriod」プロパティをサポートしていないと、あなたがしようとすると、それらを設定している場合、あなたにランタイムエラーを与えるようです。

また、Application.FileDialogはMacでも動作しませんが、それは他の記事にも記載されています。 Macの場合

:私は言うつもりだったまさに

Sub load_csv() 
Dim fStr As String 

fStr = "Macintosh HD:Users:anthony:Documents:example.csv" 'Keeping file String simple for example. 

With ThisWorkbook.Sheets("Data").QueryTables.Add(Connection:= _ 
"TEXT;" & fStr, Destination:=Range("$A$1")) 
    .Name = "CAPTURE" 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    '.PreserveFormatting = True **commented out for Mac 
    .RefreshOnFileOpen = False 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    '.RefreshPeriod = 0 **commented out for Mac 
    .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 
関連する問題