将CSV导入Excel – 自动“文本到列”和“插入表”

我想在Excel 2010上打开我的CSV(逗号分隔)文件,并自动将文本转换为列,然后选择所有活动单元格并插入带有标题的表格.

是否可以在我的色带上添加一个按钮来为我做这一切?

我经常使用不同大小的CSV文件,我发现每次手动执行此操作都有点痛苦.

最佳答案 这有点晚了,但我只是碰到了这个问题……

这是为了从选择器中选择特定文件:

Sub OpenCSV()
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFilePicker)
    fd.AllowMultiSelect = True
    fd.Show
    For Each fileItem In fd.SelectedItems
        Workbooks.OpenText Filename:= _
            fileItem _
            , Origin:=65001, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
            xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
            Comma:=False, Space:=False, Other:=False, TrailingMinusNumbers:=True
    Next
End Sub

这将打开所选文件夹中的所有CSV文件:

Sub OpenCSVFolder()
    Dim fd As FileDialog
    Set fd = Application.FileDialog(msoFileDialogFolderPicker)
    fd.AllowMultiSelect = True
    fd.Show
    For Each folderItem In fd.SelectedItems
        fileItem = Dir(folderItem & "\" & "*.csv")
        While fileItem <> ""
            Workbooks.OpenText Filename:= _
                folderItem & "\" & fileItem _
                , Origin:=65001, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
                xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, Semicolon:=False, _
                Comma:=False, Space:=False, Other:=False, TrailingMinusNumbers:=True
            fileItem = Dir
        Wend
    Next
End Sub

请注意,这些文件设置为Tab Delimited – 通过更新Tab:= True或Comma:= False参数来更改分隔符.

点赞