+ Reply to Thread
Results 1 to 16 of 16

Split the worksheet

Hybrid View

Mhabashy Split the worksheet 08-27-2024, 09:52 AM
jolivanes Re: Split the worksheet 08-27-2024, 06:04 PM
Mhabashy Re: Split the worksheet 08-28-2024, 02:38 AM
AliGW Re: Split the worksheet 08-28-2024, 03:03 AM
Mhabashy Re: Split the worksheet 08-28-2024, 09:35 AM
jolivanes Re: Split the worksheet 08-29-2024, 01:02 AM
Mhabashy Re: Split the worksheet 08-29-2024, 02:19 AM
jolivanes Re: Split the worksheet 08-30-2024, 04:05 PM
Mhabashy Re: Split the worksheet 08-31-2024, 02:14 AM
jolivanes Re: Split the worksheet 09-02-2024, 12:30 AM
Mhabashy Re: Split the worksheet 09-02-2024, 02:49 AM
jolivanes Re: Split the worksheet 09-02-2024, 12:16 PM
Mhabashy Re: Split the worksheet 09-04-2024, 11:06 AM
jolivanes Re: Split the worksheet 09-04-2024, 09:32 PM
Mhabashy Re: Split the worksheet 09-07-2024, 01:44 AM
jolivanes Re: Split the worksheet 09-07-2024, 06:01 PM
  1. #1
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    54

    Split the worksheet

    Hello,

    I need to split the Active worksheet to a new workbook but split the sheet to multiple sheets as following:

    1- Create a new workbook.
    2- Copy the active sheet.
    3- Paste the active sheet to the new workbook after splitting each page to a different sheets using page breaks to split. (paste all formula as text)
    4- Keep the print title for each sheet (Row 3 to 9).
    5- Save the new workbook at the desktop name is "New AAA".

    thanks for usual support,
    Attached Files Attached Files
    Last edited by Mhabashy; 08-28-2024 at 09:31 AM.

  2. #2
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: Split the worksheet

    What is in the first 2 Rows?
    Line 3 does not make sense to me. After splitting the sheet, you will have multiple sheets but you ask for the active sheet to copy/paste.
    If you split the sheet by pages, the values of the print titles (rows 3 to 9) are going to be different for each sheet. Is that what is needed?
    The inherent weakness of the liberal society: a too rosy view of humanity.

  3. #3
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    54

    Re: Split the worksheet

    Sorry if it was not clear

    The original sheet has a title from (row 3 to 9) so when it is split to two or 3 pages as per the length of the sheet (page break) i need the same title to be there in each page.

    conclusion i have a long sheet i need to divide it to different sheets in a new workbook.

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2505 (Windows 11 Home 24H2 64-bit)
    Posts
    91,137

    Re: Split the worksheet

    There are instructions at the top of the page explaining how to attach your sample workbook (yellow banner: HOW TO ATTACH YOUR SAMPLE WORKBOOK). Screenshots are of little practical use as we cannot manipulate them.

    A good sample workbook has just 10-20 rows of representative data that has been desensitised. The sample layout accurately matches that of your real data. It also has expected results mocked up, worked examples where required, relevant cells highlighted and a few explanatory notes.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  5. #5
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    54

    Re: Split the worksheet

    Attachment added

    thanks for your clarifying and patient

  6. #6
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: Split the worksheet

    Re: "using page breaks to split"
    How were these set? Manually or automatically?

  7. #7
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    54

    Re: Split the worksheet

    I am using the following code for the page breaks
    to keep the break always above "P.TAG" in column A and not cutting in the middle.

    Sub PAGEBREAK()
    
    
    
     Dim Search As String
     Search = "P.TAG"
     Dim x As Long
     Dim R As Range
     Dim LastRow As Long
     
     ActiveSheet.ResetAllPageBreaks
    
    With ActiveSheet
         Do While x < .HPageBreaks.Count
               For Each R In Range("A" & (LastRow + 1) & ":A" & .HPageBreaks(x + 1).Location.Row)
                  If R = Search Then
                      LastRow = R.Row
                  End If
               Next R
               If LastRow = 0 Then Exit Sub
               .HPageBreaks.Add before:=Range("A" & LastRow)
    
               x = x + 1
          Loop
     End With
    
    End Sub
    Last edited by Mhabashy; 08-29-2024 at 02:25 AM.

  8. #8
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: Split the worksheet

    Any reason that the Row Heights below Row 9 are either 15, 12 or 12.75?
    If they are different for no particular reason, it is difficult, for me at least, to calculate amount of rows per page.

    Your page break settings needs to be changed because of the rows to be repeated at the top.

  9. #9
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    54

    Re: Split the worksheet

    Yes I know , that's why I am trying to find a code dividing the sheet as per the page break and not a certain number of rows for each sheet.

  10. #10
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: Split the worksheet

    The Page Breaks are redone. If that's not what you want, take that part out of the code.
    Copy all three into a regular Module and run the "Save_As_New_Book" macro.
    I don't really know if this is what you want but try it.

    Sub Save_As_New_Book()
    Dim sh1 As Worksheet, tr As Long, br As Range, lr As Long
    Set sh1 = Worksheets("Sheet1")
    lr = sh1.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
    tr = 10
    
    Do
        
        ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = "From_Row_" & tr
            
        sh1.Range("A1:CP9").Copy ThisWorkbook.Sheets(Sheets.Count).Range("A1")
            Set br = sh1.Range(sh1.Cells(10, 1), sh1.Cells(tr + 80, 1)).Find("P.TAG", , , , , xlPrevious)
        sh1.Range(sh1.Cells(tr, 1), sh1.Cells(br.Row - 1, "CP")).Copy Sheets(Sheets.Count).Cells(10, 1)
        Sheets(Sheets.Count).UsedRange.Value = Sheets(Sheets.Count).UsedRange.Value
            
        tr = br.Row
            
        Set_P_Range
            
        If br.Row + 80 >= lr Then
            ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)).Name = "From_Row_" & tr
                sh1.Range("A1:CP9").Copy ThisWorkbook.Sheets(Sheets.Count).Range("A1")
                    sh1.Range(sh1.Cells(tr, 1), sh1.Cells(lr, "CP")).Copy Sheets(Sheets.Count).Cells(10, 1)
                Sheets(Sheets.Count).UsedRange.Value = Sheets(Sheets.Count).UsedRange.Value
            Set_P_Range
            GoTo Finish_It_Off
            Exit Sub
        End If
            
    Loop
    Finish_It_Off: Save_To_Desktop
    End Sub
    Sub Set_P_Range()
    Dim lr As Long, ws As Worksheet
    Set ws = ActiveSheet
    lr = ws.Cells.Find("*", , xlValues, , xlByRows, xlPrevious).Row
        With ws.PageSetup
            .PrintArea = Range("A1:CP" & lr).Address
            .Zoom = False
            .FitToPagesWide = 1
            .FitToPagesTall = False
            .Orientation = xlLandscape
        End With
    End Sub
    Sub Save_To_Desktop()
    Dim sh As Worksheet, shArr
    
        For Each sh In ThisWorkbook.Worksheets
            If Left(sh.Name, 9) = "From_Row_" Then shArr = shArr & "|" & sh.Name
        Next sh
        
        shArr = Split(Mid(shArr, 2), "|")
        Sheets(shArr).Copy
        
        With ActiveWorkbook
            .SaveAs Filename:=CreateObject("WScript.Shell").Specialfolders("Desktop") & "\New_AAA.xlsx"
            .Close
        End With
        
    End Sub

  11. #11
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    54

    Re: Split the worksheet

    1st of all thank you Mr. Jolivanes for your helping
    its work and that is what I really want
    iam only asking if you can help me with the following:
    1- the column width its coming different than the original.
    2-its working only if there is only sheet1 in the workbook can it modified to be done for the active sheet regardless the name and even if there is many sheets in the workbook.

    regards,

  12. #12
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: Split the worksheet

    Change this
    sh1.Range("A1:CP9").Copy ThisWorkbook.Sheets(Sheets.Count).Range("A1")
    to this
    sh1.Range("A1:CP9").Copy
    With ThisWorkbook.Sheets(Sheets.Count).Range("A1")
    .Paste    'or .PasteSpecial xlPasteValues
    .Resize(, 94).PasteSpecial Paste:=xlPasteColumnWidths
    End With
    Change this
    Set sh1 = Worksheets("Sheet1")
    to so
    Set sh1 = ActiveSheet

  13. #13
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    54

    Re: Split the worksheet

    Thank you man for your hard trying, I am appreciating your efforts
    but actually its not working well
    I attached the original file and the result file
    Attached Files Attached Files

  14. #14
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: Split the worksheet

    In the code there are 2 instances of
    sh1.Range("A1:CP9").Copy ThisWorkbook.Sheets(Sheets.Count).Range("A1")
    Did you change them both?
    See Post #12 on what to change that line to.

  15. #15
    Registered User
    Join Date
    06-28-2022
    Location
    KSA
    MS-Off Ver
    Microsoft Office Professional Plus 2019
    Posts
    54

    Re: Split the worksheet

    thanks man I am appreciating your help
    its working perfectly

    last thing please, the new sheets created are still in the original file any way to delete it or it will be difficult

    i know it is too much but actually i have more than 600 workbooks need to be modified

  16. #16
    Forum Expert
    Join Date
    10-06-2008
    Location
    Canada
    MS-Off Ver
    2007 / 2013
    Posts
    5,700

    Re: Split the worksheet

    Change the "Save_To_Desktop" macro to
    Sub Save_To_Desktop()
    Dim sh As Worksheet, shArr, i As Long
    Application.ScreenUpdating = False
        For Each sh In ThisWorkbook.Worksheets
            If Left(sh.Name, 9) = "From_Row_" Then shArr = shArr & "|" & sh.Name
        Next sh
        
        shArr = Split(Mid(shArr, 2), "|")
        Sheets(shArr).Copy
        
        With ActiveWorkbook
            .SaveAs Filename:=CreateObject("WScript.Shell").Specialfolders("Desktop") & "\New_AAA.xlsx"
            .Close
        End With
        For i = LBound(shArr) To UBound(shArr)
            Application.DisplayAlerts = False
                Sheets(shArr(i)).Delete
            Application.DisplayAlerts = True
        Next i
    Application.ScreenUpdating = True
    End Sub

+ 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. [SOLVED] VBA to split worksheet
    By GregThePeg in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-22-2014, 12:15 PM
  2. [SOLVED] Split worksheet
    By Luphai in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 01-22-2013, 06:13 AM
  3. [SOLVED] Split one worksheet into multiple worksheet by column
    By vdivgi in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-19-2012, 11:05 AM
  4. Split one worksheet to many?
    By Nessin in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 09-06-2006, 03:44 AM
  5. How to split a worksheet
    By JP3 in forum Excel General
    Replies: 0
    Last Post: 02-06-2006, 05:40 PM
  6. [SOLVED] RE: How to split a worksheet
    By Ron Coderre in forum Excel General
    Replies: 0
    Last Post: 02-06-2006, 05:35 PM
  7. [SOLVED] Split worksheet?
    By Richard in forum Excel General
    Replies: 4
    Last Post: 05-19-2005, 10:06 AM

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