简体   繁体   English

将带逗号的文本文件导入Excel

[英]Import text files with commas into Excel

I've got a large number of files that I need to import into Excel. 我有大量的文件需要导入到Excel中。 I found, and partly modified this code (I can't remember where, so I can't credit), that works 90&%+ of the time. 我发现并部分修改了此代码(我不记得在哪里,所以我不敢相信),这种代码在90%以上的时间内都有效。

Sub Extractions()

Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.DisplayStatusBar = False

Dim FilesToOpen
Dim x As Integer

    On Error GoTo ErrHandler

    FilesToOpen = Application.GetOpenFilename _
      (fileFilter:="Microsoft Excel Files (*.*?), *.*?", MultiSelect:=True, Title:="Files to Import")

    If TypeName(FilesToOpen) = "Boolean" Then
        MsgBox "No Files were selected"
        GoTo ExitHandler
    End If

    x = 1
    While x <= UBound(FilesToOpen)
        Workbooks.Open fileName:=FilesToOpen(x)
        Sheets().Move before:=ThisWorkbook.Sheets _
          (ThisWorkbook.Sheets.Count)
        x = x + 1
    Wend

ExitHandler:
    Application.ScreenUpdating = False
    Exit Sub

ErrHandler:
    MsgBox Err.Description
    Resume ExitHandler
    Resume

End Sub

Most of the files I import are fine, as they are either .csv, .txt. 我导入的大多数文件都可以,因为它们是.csv,.txt。 or .xlsx files. 或.xlsx文件。 However, a number of the .csv and .txt files contain commas in different rows. 但是,许多.csv和.txt文件在不同的行中包含逗号。 When I go to import them, they split the rows into two, which causes issues with later parts of the macro. 当我导入它们时,它们将行分成两部分,这会导致宏后面的部分出现问题。

When I go to import the files normally, the issue of the commas does not occur and the file imports to the first column, which is where it needs to be. 当我正常导入文件时,不会出现逗号的问题,并且文件会导入到第一列,也就是它所在的位置。

Is there any way to modify the above code to simply import the files to the first column, and ignore the commas? 有什么方法可以修改上面的代码,以将文件简单地导入第一列,而忽略逗号?

So the answer, with the help of @R3uK is to put 所以答案是在@ R3uK的帮助下

, Format:= 5

after

Workbooks.Open fileName:=FilesToOpen(x)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM