macros导入CSV文件到一个Excel非活动工作表

我有一个macros启用Excel工作簿,其中包含几个命名的工作表。 其中一个工作表被命名为“面板”,另一个工作表命名为“数据”。 名为“面板”的工作表有一个macros指定的button。 我想select名为“面板”的工作表上的button,并出现浏览文件窗口。 一旦用户在他们的硬盘上select了csv文件,我希望将csv文件的内容导入从单元格A1开始的名为“data”的工作表中。

问题1:已分配给button的vba导致csv文件的内容与button(“面板”工作表)放在同一工作表上。 我想把csv文件的内容放在“数据”表上。

问题2:另外,还有一串代码引用了我的硬盘驱动器和一个名为“capture.csv”的文件。 所以当macros启用Excel文件在另一台计算机上时,该文件崩溃。 任何方式来删除通路string,以便任何计算机可以使用该文件?

任何帮助解决这个问题将不胜感激。 分配给该button的macros如下所示:

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 

这是你正在尝试?

 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 

对于Mac上的Excel,似乎QueryTable对象不支持属性“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