【发布时间】:2018-03-30 00:00:53
【问题描述】:
我有一个用于 excel 的 VB 脚本,它几乎可以完美地达到我想要的目的,它将大文件分成许多更小的 50.000 行文件。 但它保存在 XLS 中,我需要它们在 CSV 上,我只是不知道如何。 我可以在这里更改任何简单的参数来实现这一点吗?
Sub Test()
Dim wb As Workbook
Dim ThisSheet As Worksheet
Dim NumOfColumns As Integer
Dim RangeToCopy As Range
Dim RangeOfHeader As Range 'data (range) of header row
Dim WorkbookCounter As Integer
Dim RowsInFile 'how many rows (incl. header) in new files?
Application.ScreenUpdating = False
'Initialize data
Set ThisSheet = ThisWorkbook.ActiveSheet
NumOfColumns = ThisSheet.UsedRange.Columns.Count
WorkbookCounter = 1
RowsInFile = 50000 'as your example, just 1000 rows per file
'Copy the data of the first row (header)
Set RangeOfHeader = ThisSheet.Range(ThisSheet.Cells(1, 1), ThisSheet.Cells(1, NumOfColumns))
For p = 2 To ThisSheet.UsedRange.Rows.Count Step RowsInFile - 1
Set wb = Workbooks.Add
'Paste the header row in new file
RangeOfHeader.Copy wb.Sheets(1).Range("A1")
'Paste the chunk of rows for this file
Set RangeToCopy = ThisSheet.Range(ThisSheet.Cells(p, 1), ThisSheet.Cells(p + RowsInFile - 2, NumOfColumns))
RangeToCopy.Copy wb.Sheets(1).Range("A2")
'Save the new workbook, and close it
wb.SaveAs ThisWorkbook.Path & "\TF_COMBAT_EASTER_GAMES_2_" & WorkbookCounter
wb.Close
'Increment file counter
WorkbookCounter = WorkbookCounter + 1
Next p
Application.ScreenUpdating = True
Set wb = Nothing
End Sub
【问题讨论】:
-
你正在使用 excel 互操作吗?
-
VB.NET、VBA 和 VBScript不一样。请正确调用事物,并使用适当的标签。 您的代码似乎在 VBA 中。
-
每次你用错标签,你就发一个kitten sad