VBA:将数组值写入相应的列 - 代码未正确分配值的问题

VBA: Writing Array Values to Respective Columns - Issue with Code Not Distributing Values Correctly

提问人:Hassan Shehzad 提问时间:11/15/2023 更新时间:11/15/2023 访问量:61

问:

我有使用 VBA 宏代码生成 CSV 文件的代码。一切正常,除了它将所有值/数据写入第一列而不是实际所属的相应列。

代码如下:

Sub export_multiple_CSV()
    Dim rg As Range: Set rg = FindLastCell(shFile.Cells)
    Dim arr As New clsArray2D: arr.data = Range(shFile.Range("A1"), rg).value
    arr.RemoveRows 1
    Dim header As Variant: header = Range(shFile.Cells(1, 1), shFile.Cells(1, FindLastcolumn(shFile.Cells))).value
    
    Dim sRow As Long: sRow = 1
    Dim stopRow As Long: stopRow = arr.IndexOf("stop", 1)
    arr.data = arr.getRows(1, stopRow)
    
    Dim flag As Boolean: flag = False
    Dim counter As Long: counter = 1

    Dim i As Long, eRow As Long
    For i = 1 To stopRow
            eRow = arr.IndexOf("next", 1, sRow)
            If eRow = -1 Then
                    flag = True
                    eRow = stopRow
            End If
            
            'split the array from start to end row
            Dim temp As New clsArray2D
            temp.data = arr.getRows(sRow, eRow - sRow)
            'add header to ea
            'ch file
            temp.insertRows 1, header
            'update if adding columns to export correct file name
            Dim fName As String: fName = temp.value(2, 18)
            
            Dim ii As Long, jj As Long
            For ii = 1 To temp.rowCount
                    For jj = 1 To temp.columnCount
                            If IsError(temp.value(ii, jj)) Then temp.value(ii, jj) = Empty
                            
                            'option to replace semicolons and other delimiters, which is not a good solution
                            temp.value(ii, jj) = Replace(temp.value(ii, jj), ";", ",")
                            
                            If jj = 1 Then
                                    tempStr = temp.value(ii, jj)
                                    
                            Else
                                    tempStr = tempStr & ";" & temp.value(ii, jj)
                            End If
                    Next jj
                    temp.value(ii, 1) = tempStr
                    Call AppendLineToFile(tempStr, fName, ii)
            Next ii

            If flag = True Then Exit For
            
            'update starting row
            sRow = eRow + 1
            counter = counter + 1
    Next i

    'treure comentari per activar missatge d'exportació
    'MsgBox counter & " CSV generated!"
End Sub

Sub AppendLineToFile(textToAppend, fName As String, count As Long)
    Dim fs As Object: Set fs = CreateObject("Scripting.FileSystemObject")
    Dim filePath As String: filePath = ThisWorkbook.Path & "\" & fName & ".csv"
     If count = 1 Then
            If fs.FileExists(filePath) Then fs.DeleteFile filePath
    End If
    
    Dim fileStream As Object: Set fileStream = fs.OpenTextFile(filePath, 8, True, -1)
    
    fileStream.WriteLine textToAppend
    fileStream.Close
    
    ' Release the objects
    Set fileStream = Nothing
    Set fs = Nothing
End Sub

它还导出 unicode 符号。

导出的文件应具有 utf8 编码(它已经具有)并且数据写入其所属的所有列中。

Excel VBA CSV

评论

0赞 Tim Williams 11/15/2023
什么?仅供参考,您应该引用包含分隔符的任何数据clsArray2D;
1赞 Tim Williams 11/15/2023
我认为最好使用 ADODB。流以读取/写入 UTF-8,例如,请参阅 stackoverflow.com/questions/13851473/...
0赞 CDP1802 11/15/2023
第一列中的值/数据你是如何打开csv的?
0赞 user10186832 11/15/2023
请发布一些测试数据。用它来制作一个 Markdown 表格

答: 暂无答案