尽管分隔符设置为分号,但宏还是用逗号分隔.csv

时间:2019-02-01 23:55:29

标签: excel vba

在下面附带的代码中,尽管逗号分隔符设置为false并且分号分隔符设置为true,但宏会打开.csv,并用分号和逗号分隔值(至少看起来像这样做)。

我试图切换代码的​​顺序,但无济于事。 可以在以下线程中找到代码的历史记录和用途:VBA run-time error '1004' while looping through .csv file。请在这里告诉我我做错了什么或想念什么?

(与this问题相关的主题。)

Dim wrk As Workbook
Dim Sh As Worksheet
Dim findMatch As Range, searchInColumn As Range
Dim i As Long, j As Long, k As Long
Dim chosenFile As Integer
Dim xlFileName As String
Dim chooseFiles As Office.FileDialog

Set DictChanged = CreateObject("Scripting.Dictionary")
Set DictToBeDone = CreateObject("Scripting.Dictionary")

Application.ScreenUpdating = False


Set chooseFiles = Application.FileDialog(msoFileDialogFilePicker)

    With chooseFiles

        .AllowMultiSelect = True
        .Title = "Please select the file."
        .InitialFileName = "c:\"
        .InitialView = msoFileDialogViewList
        .Filters.Add "All", "*.*"

    End With


If chooseFiles.Show = -1 Then
    For k = 1 To chooseFiles.SelectedItems.Count
        xlFileName = chooseFiles.SelectedItems(k)

        Set wrk = Application.Workbooks.Open(xlFileName)
        Set Sh = wrk.Worksheets(1)               

        If InStr(1, wrk.Name, ".csv") Then

            Sh.Range(Range("A1"), Range("A1").End(xlDown)).TextToColumns _
            DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
            Semicolon:=True, Comma:=False, Space:=False, Other:=False

        End If
End If

所描述的问题出现在可见代码内。在要求的输出阶段以下:

输出阶段:

初始文件

+ --------- + --------- + --------- + ---------------- -+ ------------ + ----------------- + |第1列|第2列|第3列|第4列|第5列|第6列| + --------- + --------- + --------- + ------------------ + ------------ + ----------------- + | 123456 | 654321 | SomeID | ValuesWithComma | SomeValues |其他价值| + --------- + --------- + --------- + ------------------ + ------------ + ----------------- + | 123456 | 654322 | SomeID |价值观,用逗号| SomeValues |其他价值| + --------- + --------- + --------- + ------------------ + ------------ + ----------------- + | 123456 | 654323 | SomeID |价值观,用逗号| SomeValues |其他价值| + --------- + --------- + --------- + ------------------ + ------------ + ----------------- + | 123456 | 654324 | SomeID | Val,uesWithComma | SomeValues |其他价值| + --------- + --------- + --------- + ------------------ + ------------ + ----------------- + | 123456 | 654325 | SomeID | ValuesWithComma | SomeValues |其他价值| + --------- + --------- + --------- + ------------------ + ------------ + ----------------- + | 123456 | 654326 | SomeID | ValuesWithComma | SomeValues |其他价值| + --------- + --------- + --------- + ------------------ + ------------ + ----------------- + | 123456 | 654327 | SomeID | ValuesWithComma | SomeValues |其他价值| + --------- + --------- + --------- + ------------------ + ------------ + ----------------- + | 123456 | 654328 | SomeID | ValuesWithComma | SomeValues | SomeOtherValues |

2.AfterPickingFileInFileDialog

+ ---------------------------------------------- ------------------- + ------------------------------ ----------- + | column1; column2; column3; column4; column5; column6 |第2列| + ------------------------------------------------- ---------------- + --------------------------------- -------- + | 123456; 654321; SomeID; ValuesWithComma; SomeValues; SomeOtherValues | 654321 | + ------------------------------------------------- ---------------- + --------------------------------- -------- + | 123456; 654322; SomeID;值| WithComma; SomeValues; SomeOtherValues | + ------------------------------------------------- ---------------- + --------------------------------- -------- + | 123456; 654323; SomeID;值| WithComma; SomeValues; SomeOtherValues | + ------------------------------------------------- ---------------- + --------------------------------- -------- + | 123456; 654324; SomeID; Val | uesWithComma; SomeValues; SomeOtherValues | + ------------------------------------------------- ---------------- + --------------------------------- -------- + | 123456; 654325; SomeID; ValuesWithComma; SomeValues; SomeOtherValues | 654325 | + ------------------------------------------------- ---------------- + --------------------------------- -------- + | 123456; 654326; SomeID; ValuesWithComma; SomeValues; SomeOtherValues | 654326 | + ------------------------------------------------- ---------------- + --------------------------------- -------- + | 123456; 654327; SomeID; ValuesWithComma; SomeValues; SomeOtherValues | 654327 | + ------------------------------------------------- ---------------- + --------------------------------- -------- + | 123456; 654328; SomeID; ValuesWithComma; SomeValues; SomeOtherValues | 654328 | + ------------------------------------------------- ---------------- + --------------------------------- -------- +

3.AfterLastCondition

+ --------- + --------- + --------- + ---------------- -+ ------------ + ----------------- + |第1列|第2列|第3列|第4列|第5列|第6列| + --------- + --------- + --------- + ----------------- +- ----------- + ----------------- + | 123456 | 654321 | SomeID | ValuesWithComma | SomeValues |其他价值| + --------- + --------- + --------- + ----------------- +- ----------- + ----------------- + | 123456 | 654322 | SomeID |价值观| | | + --------- + --------- + --------- + ----------------- +- ----------- + ----------------- + | 123456 | 654323 | SomeID |价值观| | | + --------- + --------- + --------- + ----------------- +- ----------- + ----------------- + | 123456 | 654324 | SomeID | Val | | | + --------- + --------- + --------- + ----------------- +- ----------- + ----------------- + | 123456 | 654325 | SomeID | ValuesWithComma | SomeValues |其他价值| + --------- + --------- + --------- + ----------------- +- ----------- + ----------------- + | 123456 | 654326 | SomeID | ValuesWithComma | SomeValues |其他价值| + --------- + --------- + --------- + ----------------- +- ----------- + ----------------- + | 123456 | 654327 | SomeID | ValuesWithComma | SomeValues |其他价值| + --------- + --------- + --------- + ----------------- +- ----------- + ----------------- + | 123456 | 654328 | SomeID | ValuesWithComma | SomeValues |其他价值| + --------- + --------- + --------- + ----------------- +- ----------- + ----------------- +

2 个答案:

答案 0 :(得分:1)

@Tim Williams和@ Nick.McDermaid评论到您的代码存在问题。尚不清楚您要实现什么。但是,关于打开分号分隔的文本文件,您可以将文件重命名为.txt并像

一样打开
xlFileName = chooseFiles.SelectedItems(k)
        TmpFlName = Path & "TmpCsv.txt"
        If Dir(TmpFlName) <> "" Then Kill TmpFlName
        FileCopy xlFileName, TmpFlName
        Workbooks.OpenText Filename:=TmpFlName, Origin:= _
        1250, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
        ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False _
        , Space:=False, Other:=False, TrailingMinusNumbers:=True, Local:=False

issue was delimiter选项仅在使用.txt文件而不使用.csv时有效。

答案 1 :(得分:0)

由于上面@Ahmed AU的回答和评论,我编辑了代码,现在看来可以了。应该可以使用.csv和.xlmx文件。下面是代码的比特更长的版本:

Sub FixCSV()

Dim wrk As Workbook
Dim Sh As Worksheet
Dim findMatch As Range, searchInColumn As Range
Dim i As Long, j As Long, k As Long, lastRow As Long, lastColumn As Long
Dim chosenFile As Integer
Dim xlFileName As String
Dim chooseFiles As Office.FileDialog

Set chooseFiles = Application.FileDialog(msoFileDialogFilePicker)

    With chooseFiles       
        .AllowMultiSelect = True
        .Title = "Please select the file."
        .InitialFileName = "c:\"
        .InitialView = msoFileDialogViewList
        .Filters.Add "All", "*.*"            
    End With           

If chooseFiles.Show = -1 Then
    For k = 1 To chooseFiles.SelectedItems.Count
        xlFileName = chooseFiles.SelectedItems(k)

            If InStr(1, xlFileName, ".csv") Then
                TmpFlName = path & "TmpCsv.txt"
                If Dir(TmpFlName) <> "" Then Kill TmpFlName
                FileCopy xlFileName, TmpFlName
                Workbooks.OpenText FileName:=TmpFlName, origin:= _
                1250, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
                ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False _
                , Space:=False, Other:=False, TrailingMinusNumbers:=True, Local:=False

                Set wrk = Application.Workbooks("TmpCsv.txt")
                Set Sh = wrk.Worksheets(1)
            Else
                Set wrk = Application.Workbooks.Open(xlFileName)
                Set Sh = wrk.Worksheets(1)
            End If

        lastRow = Sh.Cells(Sh.Rows.Count, "A").End(xlUp).Row
        lastColumn = Sh.Cells(1, Sh.Columns.Count).End(xlToLeft).Column           

       i = 2           
       Do Until i = lastRow                          
          'do something          
       Loop

    If InStr(1, wrk.Name, "TmpCsv.txt") Then             
        wrk.SaveAs FileName:=xlFileName, FileFormat:=xlCSV, Local:=True
        wrk.Close False
        Kill TmpFlName
    Else
        wrk.Close 'savechanges:=true
    End If

    Next k
End If

End Sub

另存为.csv时,逗号作为分隔符弹出,再次破坏了文件-user2726096的回答:https://stackoverflow.com/a/18492514/10348607帮助我解决了这个问题。