+ Reply to Thread
Results 1 to 2 of 2

Macro to break master sheet into sheets of 101 rows - save as csv

Hybrid View

phbryan Macro to break master sheet... 09-30-2021, 02:10 PM
WideBoyDixon Re: Macro to break master... 09-30-2021, 04:57 PM
  1. #1
    Forum Contributor
    Join Date
    12-18-2011
    Location
    long island
    MS-Off Ver
    365
    Posts
    236

    Macro to break master sheet into sheets of 101 rows - save as csv

    I have a master sheet that contains a header and varying amount of rows. I need to export this sheet as a csv but cannot go over 101 rows (100 rows including the header).
    What I am looking to for is a macro that takes the master sheet and breaks it into separate sheets and saves as csv, or only grabs 100 rows at a time and saves as csv.
    For example, if my master sheet has 251 rows (250 rows and 1 header), I need to make 3 csv's. Two with 101 rows each (100 rows and 1 header) and one with 51 rows (50 rows and one header).
    The header row will remain the same throughout.

  2. #2
    Forum Expert WideBoyDixon's Avatar
    Join Date
    10-03-2016
    Location
    Sheffield, UK
    MS-Off Ver
    365
    Posts
    2,182

    Re: Macro to break master sheet into sheets of 101 rows - save as csv

    Something like this might work:

    Public Sub SplitInto100s()
    
    Dim lastRow As Long
    Dim thisRow As Long
    Dim sourceSheet As Worksheet
    Dim targetBook As Workbook
    Dim targetSheet As Worksheet
    Dim targetFile As String
    
    targetFile = ActiveWorkbook.FullName & ".###.csv"
    Set sourceSheet = ActiveSheet
    Set targetBook = Workbooks.Add
    Set targetSheet = targetBook.Sheets(1)
    targetSheet.Name = "CSV"
    targetSheet.Activate
    targetSheet.Range("1:1").Value = sourceSheet.Range("1:1").Value
    
    lastRow = sourceSheet.Cells(sourceSheet.Rows.Count, "A").End(xlUp).Row
    For thisRow = 2 To lastRow Step 100
        targetSheet.Range("2:101").Value = sourceSheet.Range(CStr(thisRow) & ":" & CStr(thisRow + 99)).Value
        targetBook.SaveAs Filename:=Replace(targetFile, "###", CStr((thisRow + 98) / 100)), FileFormat:=xlCSVUTF8, CreateBackup:=False
    Next thisRow
    
    Application.DisplayAlerts = False
    targetBook.Close False
    Application.DisplayAlerts = True
    
    End Sub
    WBD
    Office 365 on Windows 11, looking for rep!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to bring back rows from multiple sheets to one master sheet based on criteria
    By Fraser1605 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-02-2021, 01:18 PM
  2. Replies: 2
    Last Post: 05-22-2019, 10:46 AM
  3. [SOLVED] Adding the rows of sheets to a master sheet.
    By mohit999 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 09-20-2016, 10:11 AM
  4. Need the master sheet to filter rows to other sheets
    By chrisdantonelli in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-01-2014, 08:03 PM
  5. [SOLVED] Adding rows while merging sheets into a master sheet
    By sbidgol in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-11-2014, 10:00 AM
  6. Macro to Transfer data from multiple sheets into rows on a master sheet
    By serrone in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-30-2013, 06:35 AM
  7. inserting rows from different sheets into master sheet
    By Alicita in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-12-2011, 08:25 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1