将CSV文件导入非活动的Excel工作表的宏

8
我有一个启用宏的Excel工作簿,其中包含多个命名工作表。其中一个工作表名为“panel”,第二个工作表名为“data”。名为“panel”的工作表上有一个按钮,该按钮分配了一个宏。我想选择名为“panel”的工作表上的按钮,并出现一个浏览文件窗口。一旦用户在硬盘上选择了CSV文件,我希望将CSV文件的内容导入到名为“data”的工作表中,从单元格A1开始。
问题1:我分配给该按钮的vba会导致CSV文件的内容被放置在与按钮相同的工作表上(即“panel”工作表)。我希望将CSV文件的内容放置在“data”工作表上。
问题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
2个回答

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

感谢您抽出时间来帮助我。我已经将您编写的代码分配给了按钮。当我加载CSV文件时,会出现一个消息窗口:“运行时错误'-2147024809(80070057)目标范围不在创建查询表的同一工作表上。” - George
1
将代码中的 Destination:=Range("$A$1")) 改为 Destination:=ThisWorkbook.Sheets("Data").Range("$A$1")) - Siddharth Rout
4
针对 UTF-8 编码,将 .TextFilePlatform 改为 -535。 - Lnu
3
وˆ‘ه°†RefreshStyle = xlInsertDeleteCellsو›؟وچ¢ن¸؛.RefreshStyle = xlOverwriteCellsهگژ,و‰¾هˆ°ن؛†وˆ‘çڑ„é—®é¢کçڑ„ç­”و،ˆم€‚ - Alg_D
有什么想法如何将文件放置在工作簿中的新工作表中吗? - user3504751

0

对于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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接