【问题标题】:Macro to prompt user to select CSV files for import into existing sheet in workbook提示用户选择 CSV 文件以导入工作簿中现有工作表的宏
【发布时间】:2012-04-02 11:38:16
【问题描述】:

我正在运行一个宏,它会自动获取 csv 文件并将它们导入到我的工作簿中的特定工作表中。但是,我希望通过让用户选择要导入的文件而不是让宏自动抓取 csv 文件来增加更大的灵活性,因为命名和目录一样可能会发生变化。我是 VBA 新手,一直在尝试更好地理解 MsoFileDialogType 和 GetOpenFilename,但很难将概念/实现掌握到我的代码中。

我最终想要的是用户单击工作簿前端的按钮。系统会提示您选择要导入的第一个 csv 文件。此 csv 文件将被导入到工作簿 temp1 中的预先命名的工作表中。但是,由于数据文件是成对出现的,我希望用户能够在第一个 csv 文件之后选择下一个 csv 文件进入 temp2。

我目前拥有的是:

Worksheets.Add
ActiveSheet.Name = "temp1"
With ActiveSheet.QueryTables.Add(Connection:= _
        "TEXT;MAC Directory path here" _
        , Destination:=Range("A1"))
        .Name = "temp 1 03.02.12"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = xlMacintosh
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
        1, 1, 1)
        .Refresh BackgroundQuery:=False
        .UseListObject = False
End With
ActiveSheet.Move after:=Worksheets(Worksheets.Count)

谢谢。

【问题讨论】:

    标签: excel vba macos


    【解决方案1】:

    也许在这些线上有些东西。

    Sub GetCSVList()
    Dim dlgOpen As FileDialog
    Set dlgOpen = Application.FileDialog(msoFileDialogFilePicker)
    With dlgOpen
        .AllowMultiSelect = True
        ''Start in
        .InitialFileName = "Z:\docs\"
        .Show
    End With
    
    For Each fname In dlgOpen.SelectedItems
        ImportCSV fname
    Next
    End Sub
    
    Sub ImportCSV(fname)
    Set ws = Worksheets.Add(after:=Worksheets(Worksheets.Count))
    ws.Name = "temp" & Worksheets.Count + 1
    
    With ws.QueryTables.Add( _
            Connection:="TEXT;" & fname, _
            Destination:=Range("A1"))
        .Name = "Temp" & Worksheets.Count + 1
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .BackgroundQuery = True
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = xlMacintosh
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileConsecutiveDelimiter = False
        .TextFileTabDelimiter = False
        .TextFileSemicolonDelimiter = False
        .TextFileCommaDelimiter = True
        .TextFileSpaceDelimiter = False
        .Refresh BackgroundQuery:=False
        '.UseListObject = False
    End With
    End Sub
    

    【讨论】:

    • 嗨 Remou,我尝试实现代码但遇到此错误:“未定义用户定义类型”并突出显示这一行 -> Dim dlgOpen As FileDialog
    • 您使用的是哪个版本的 Excel?您可以添加对 Microsoft Office x.x 对象库的引用吗?
    • 嗨 Remou,我使用的是 Mac Office 2011。我进入了工具 -> 参考,并确保检查了 Visual Basic for Applications、Microsoft Excel 14.0 对象库和 Microsoft Office 14.0 对象库。
    • Mac版可能会有很大的不同,你不妨阅读answers.microsoft.com/en-us/mac/forum/macoffice2011-macexcel/…。但是,即使在 MAC 上,将名称输入 CSV 导入也应该非常相似。
    猜你喜欢
    • 2019-10-10
    • 2013-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-27
    • 1970-01-01
    • 2016-07-28
    相关资源
    最近更新 更多