【问题标题】:macro to Import csv file into an excel non active worksheet将 csv 文件导入 excel 非活动工作表的宏
【发布时间】:2013-01-16 00:30:32
【问题描述】:

我有一个启用宏的 Excel 工作簿,其中包含多个命名工作表。其中一个工作表名为“面板”,第二个工作表名为“数据”。名为“面板”的工作表有一个分配了宏的按钮。我想选择工作表上名为“面板”的按钮,然后出现浏览文件窗口。一旦用户在他们的硬盘驱动器上选择了 csv 文件,我希望将 csv 文件的内容导入到名为“data”的工作表中,该工作表从单元格 A1 开始。

问题 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

【问题讨论】:

    标签: excel vba csv import


    【解决方案1】:

    这是你正在尝试的吗?

    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) 目标范围不在创建查询表的同一个工作表上。”
    • 将 Destination:=Range("$A$1")) 更改为 Destination:=ThisWorkbook.Sheets("Data").Range("$A$1"))
    • 对于utf-8编码,改为.TextFilePlatform = -535
    • 我找到了我的问题的答案,将RefreshStyle = xlInsertDeleteCells 替换为.RefreshStyle = xlOverwriteCells
    • 知道如何将文件放入工作簿中的新工作表中吗?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 2019-10-10
      • 2016-08-30
      • 2016-04-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多