数据从 Excel 导出到 CSV

Data export from Excel to CSV

当我将 excel sheet 中分隔的列和行的数据传输到 .csv 格式时,它不会以 .csv 格式分隔数据。我把我的代码放在下面。

Private Sub CommandButton1_Click()
    Dim lastrow As Long, wb As Workbook, ws As Worksheet, wbDest As Workbook, _
        wsDest As Worksheet, path As String

    If ComboBox1.Value = "DENİZBANK - ALTUNİZADE ŞUBE" Then
        'Combobox isimli bir dosya aç
        Set wb = ThisWorkbook
        Set ws = wb.Sheets("TL")
        path = "C:\Users\emir.DEMTA\Desktop\Vahit Çağlayan Dosyalar\p. CSV\DENİZBANK - ALTUNİZADE ŞUBE\Deniz_Giden_Banka_HGS.csv"
        Set wbDest = Workbooks.Open(path)
        Set wsDest = wbDest.Worksheets("Deniz_Giden_Banka_HGS")
        n = 1

        For i = 13 To 50 'lastrow
            'Aşağıdaki verileri combobox isimli dosyaya yazdır.
            If ComboBox1.Value = "DENİZBANK - ALTUNİZADE ŞUBE" And _
               ComboBox5.Value = "1073196-389" And _
               TextBox5.Value <> "" And _
               ws.Cells(i, "A").Value = "DENİZBANK - ALTUNİZADE ŞUBE" And _
               ws.Cells(i, "I").Value = "Banka" Then
                wsDest.Cells(n + 1, 1) = ws.Cells(i, 9).Value
                wsDest.Cells(n + 1, 2) = ws.Cells(i, 4).Value
                wsDest.Cells(n + 1, 3) = ws.Cells(i, 6).Value
                wsDest.Cells(n + 1, 5) = "1"
                wsDest.Cells(n + 1, 6) = "'01"
                wsDest.Cells(n + 1, 7) = ws.Cells(i, 7).Value
                wsDest.Cells(n + 1, 8) = ws.Cells(i, 8).Value

                n = n + 1
            End If
        Next i

        wbDest.SaveAs Filename:="C:\Users\emir.DEMTA\Desktop\Vahit Çağlayan Dosyalar\p. CSV\DENİZBANK - ALTUNİZADE ŞUBE\" _
           & " D - HGS - " & Date & ".csv"
        wbDest.Close
        Set wbDest = Nothing

        Unload Me
    End If
End Sub

传输到 .csv 的 Excel 中的所有信息都出现在一个单元格中

MasrafHGS100

我想要的是在我的代码中传输所有相同的数据

Masraf    HGS    100

如果您 .SaveAs 您还需要根据您的情况 XlFileFormat enumeration 指定 FileFormat:= 可能 FileFormat:=xlCSV。然后在文本编辑器中打开 CSV 文件进行检查!不在 Excel.

如果您在 Excel 中打开它,它会执行 CSV 以 Excel 导入(如果它有错误的属性,它不会将其正确导入到不同的列中)。请注意,双击 .CSV 文件(在 Excel 中打开它)是幸运的,没有正确的导入!它可能会按预期显示或不显示。

wbDest.SaveAs FileFormat:=xlCSV, local:=True, Filename:="C:\Users\emir.DEMTA\Desktop\Vahit Çağlayan Dosyalar\p. CSV\DENİZBANK - ALTUNİZADE ŞUBE\" _ & " D - HGS - " & Date & ".csv" 

在代码中,您只是指定文件以 .csv 结尾。但是,这不适用于 CSV 文件的“格式”。

为此,您需要始终指定何时执行 .SaveAs 名称 - 文件格式 你可以做 FileFormat:=xlCSVFileFormat:=6

或者,您始终可以执行 FileFormat:=xlTextWindows 以获得 .txt 文件,然后使用替代方法将其转换为 .csv