如何识别VB excel脚本上的文件输出格式

时间:2018-03-30 00:00:54

标签: excel excel-vba vba

我有一个用于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

1 个答案:

答案 0 :(得分:1)

您可以这样做:

    wb.SaveAs("C:\Temp\output.csv", Excel.XlFileFormat.xlCSVWindows)
    wb.Close(false)
    'wb is the WORKBOOK