correct macro VBA code

  • Status: Closed
  • Pris: $10
  • Bidrag mottagna: 4
  • Vinnare: soery4

Tävlingssammandrag

I have an excel VBA code that takes data files with many records then splits the one large file up into multiple files with (X) amount of records each. Currently, the code makes all the files that are created when this macro is run to be in .xls format. I need the files it creates to be in .csv format.

Please update the code accordingly so when it runs it makes all the files in .csv format

First entry that uploads the working code will be awarded

code_________________________________

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 = 2000 '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 & "\file " & WorkbookCounter
wb.Close

'Increment file counter
WorkbookCounter = WorkbookCounter + 1
Next p

Application.ScreenUpdating = True
Set wb = Nothing
End Sub

Rekommenderade kompetenser

Arbetsgivares feedback

“Awesome freelancer! Good quick work solved my problem !”

Profilbild revamp365, United States.

Topp bidrag från den här tävlingen

Visa fler bidag

Klargörandetavla

Finns inga meddelanden än.

Hur du kommer igång med tävlingar

  • Lägg upp din tävling

    Lägg upp din tävling Snabbt och enkelt

  • Få massvis med bidrag

    Få massvis med bidrag Från världens alla hörn

  • Utse det bästa bidraget

    Utse det bästa bidraget Ladda ner filerna - enkelt!

Lägg upp en tävling nu eller gå med idag!