.csv文件有行限制? (VBA)

时间:2016-09-21 18:20:11

标签: vba csv append

我写了一个代码,将日期从.xls文件传输到csv文件。但是,一旦.csv文件达到了1M的行限制标记,代码就会被删除,就像常规的excel文件一样。您将看到我将mainwb.activate放在哪里将信息粘贴到csv中。我认为这可能与我如何粘贴它有关,但我不确定。感谢。

Sub LoopThroughFilesInFolder_Click()

Dim mainwb As Workbook
Dim wb As Workbook
Dim main_i As Long
Dim wb_i As Long
Dim transfer_array(1, 14) As Variant

Set mainwb = Workbooks.Open(Filename:="C:\Users\n808037\Desktop\csv\test.csv", ReadOnly:=False)

mainwb.Activate

Set FileSystemObj = CreateObject("Scripting.FileSystemObject")
Set FolderObj = FileSystemObj.GetFolder("C:\Users\...")

main_i = 1

For Each fileObj In FolderObj.Files     'loop through the files

Set wb = Workbooks.Open(fileObj.Path)

'workbook row counter
    wb_i = 2
Do While wb.Sheets(1).Cells(wb_i, 1) <> ""

    For i = 1 To 13
        transfer_array(1, i) = wb.Sheets(1).Cells(wb_i, i).Value
    Next i

mainwb.Activate 'THIS WILL BE WHERE I OPEN AND PASTE TO THE CSV FILE

    For i = 1 To 13
    mainwb.Worksheets("test").Cells(main_i, i).Value = transfer_array(1, i) & ""
    Next i

    mainwb.Worksheets("test").Cells(main_i, 14).Value = Now & ""

    wb_i = wb_i + 1
    main_i = main_i + 1
Loop

wb.Activate
wb.Save
wb.Close

Next fileObj

mainwb.Save

End Sub

1 个答案:

答案 0 :(得分:0)

这只是一种方法:
将项目引用添加到Microsoft Scripting Runtime库。为csv文件创建一个文件对象。然后,您可以使用OpenAsTextStream方法读取,写入或附加到csv文件。只要您将该文件作为Excel工作簿处理,您将具有与Excel相同的限制。由于您现在以文本形式处理它,因此您还可以添加逗号分隔符(或类似于字符串值的引号等等)