+ Reply to Thread
Results 1 to 7 of 7

need vba to split sheet into multiple sheets

Hybrid View

  1. #1
    Registered User
    Join Date
    09-19-2013
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    4

    need vba to split sheet into multiple sheets

    I need help quickly & don't have vba experience! I have attached an example of how the whole workbook needs to look. The first tab (Full Report) is the original file I have to work with. I need to take that original sheet & split it into the various individual sheets. I've looked online & tried various code listed out there but none have worked for me & I don't know enough to customize for my needs. I'm also wondering if my original sheet needs to be changed a little since the information I need to pull into separate sheets is included in fields in column A. Any help is much appreciated!
    Attached Files Attached Files

  2. #2
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: need vba to split sheet into multiple sheets

    Hi there thuff,

    now the code below is pretty crude, cause I want to go to sleep
    It assumes the following about the workbook you're going to run it on: There is only 1 sheet with data - and that one is called "full report"
    Make sure that's true

    It will mess up your "full report" page, but I really didn't have time to fix it so all stays fine and looks wonderful and stuff.
    At least this code will do what you're looking for (I think) - you can work from there to get it nicer as you want.
    I will NOT be able to big updates on this anymore, so if you need radical changes, someone else will have to do it - small changes, I can do.

    Please remember to click the * below left if this helps

    Sub test()
    Dim lr&, cell As Range, i&, x&, y&, a$, b$, c$, d$, e$, f$, g$, h$, ws As Worksheet
    
    Application.ScreenUpdating = False
    Application.EnableEvents = False
    
    On Error GoTo earlyexit
    
    If ActiveSheet.Name <> "Full Report" Then Exit Sub
    a = ActiveSheet.Range("A2").Value
    b = ActiveSheet.Range("B2").Value
    c = ActiveSheet.Range("C2").Value
    d = ActiveSheet.Range("D2").Value
    e = ActiveSheet.Range("E2").Value
    f = ActiveSheet.Range("F2").Value
    g = ActiveSheet.Range("G2").Value
    h = ActiveSheet.Range("H2").Value
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    
    Columns(1).Insert
    Range("A1").FormulaR1C1 = "=IFERROR(RIGHT(RC[1], LEN(RC[1])-3),"""")"
    Range("A1").AutoFill Destination:=Range("A1:A" & lr), Type:=xlFillDefault
    Columns(1).Copy
    Columns(1).PasteSpecial Paste:=xlPasteValues
    Application.CutCopyMode = False
    Range("A1", ActiveCell.SpecialCells(xlLastCell)).Select
    Worksheets("Full Report").Sort.SortFields.Clear
    Worksheets("Full Report").Sort.SortFields.Add Key:=Range("A3:A" & lr), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers
    With Worksheets("Full Report").Sort
            .SetRange Range("A2:H" & lr)
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
    End With
    For i = lr To 1 Step -1
    If Cells(i, 1).Value = "" Or Len(Cells(i, 1)) < 4 Or IsNumeric(Mid(Cells(i, 1), 4, 1)) = False Then Cells(i, 1).EntireRow.Delete
    Next i
    Columns(1).Delete
    
    Rows(1).Insert
    Range("A1").Value = "iloveexcelforum.com"
    
    lr = Cells(Rows.Count, 1).End(xlUp).Row
    
    For Each cell In Range("A2:A" & lr)
    i = InStr(cell.Value, "-")
    If Mid(cell.Value, i, Len(cell.Value) - i) <> Mid(cell.Offset(-1, 0).Value, i, Len(cell.Offset(-1, 0).Value) - i) Then
    If cell.Row = 2 Then GoTo nc
    On Error Resume Next
    Set ws = Worksheets(Mid(cell.Offset(-1, 0).Value, i + 1, Len(cell.Offset(-1, 0).Value) - i))
    On Error GoTo earlyexit
    
    If ws Is Nothing Then Worksheets.Add(After:=ActiveSheet).Name = Mid(cell.Offset(-1, 0).Value, i + 1, Len(cell.Offset(-1, 0).Value) - i)
    Range("A" & x & ":A" & cell.Row - 1).EntireRow.Copy
    Worksheets(Mid(cell.Offset(-1, 0).Value, i + 1, Len(cell.Offset(-1, 0).Value) - i)).Range("A1").PasteSpecial
    Application.CutCopyMode = False
    nc:
    x = cell.Row
    End If
    Next cell
    
    For i = 1 To Worksheets.Count
    If Worksheets(i).Range("A1").Value = "" Then GoTo nxi
    If Worksheets(i).Name <> "Full Report" Then
    Worksheets(i).Rows(1).Insert
    Worksheets(i).Rows(1).Insert
    Worksheets(i).Rows(1).Insert
    Worksheets(i).Range("A1").Value = Worksheets(i).Name
    Worksheets(i).Range("A2").Value = a
    Worksheets(i).Range("B2").Value = b
    Worksheets(i).Range("C2").Value = c
    Worksheets(i).Range("D2").Value = d
    Worksheets(i).Range("E2").Value = e
    Worksheets(i).Range("F2").Value = f
    Worksheets(i).Range("G2").Value = g
    Worksheets(i).Range("H2").Value = h
    Worksheets(i).Range("A3").Value = "CUP " & Left(Worksheets(i).Range("A4"), InStr(Worksheets(i).Range("A4"), "-") - 1)
    Worksheets(i).Rows("1:3").Font.Bold = True
    
    lr = Worksheets(i).Cells(Rows.Count, 1).End(xlUp).Row
    y = 4
    x = 5
    Do Until x = lr + 60
    If Left(Worksheets(i).Cells(x, 1), InStr(Worksheets(i).Cells(x, 1), "-")) <> Left(Worksheets(i).Cells(x - 1, 1), InStr(Worksheets(i).Cells(x - 1, 1), "-")) Then
    Worksheets(i).Rows(x).Insert
    Worksheets(i).Rows(x).Insert
    Worksheets(i).Rows(x).Insert
    Worksheets(i).Rows(x).Insert
    Worksheets(i).Cells(x + 1, 1).Value = "Cup " & Left(Worksheets(i).Cells(x - 1, 1), InStr(Worksheets(i).Cells(x - 1, 1), "-")) & " Total :"
    Worksheets(i).Cells(x + 1, 5).FormulaR1C1 = "=SUM(R[-" & x - y + 1 & "]C:R[-2]C)"
    Worksheets(i).Cells(x + 1, 6).FormulaR1C1 = "=SUM(R[-" & x - y + 1 & "]C:R[-2]C)"
    Worksheets(i).Cells(x + 1, 7).FormulaR1C1 = "=SUM(R[-" & x - y + 1 & "]C:R[-2]C)"
    Worksheets(i).Cells(x + 3, 1).Value = "Cup " & Left(Worksheets(i).Cells(x + 4, 1), InStr(Worksheets(i).Cells(x + 4, 1), "-"))
    Worksheets(i).Rows(x & ":" & x + 3).Font.Bold = True
    x = x + 4
    y = x
    End If
    x = x + 1
    Loop
    End If
    nxi:
    Next i
    
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Exit Sub
    
    earlyexit:
    Application.ScreenUpdating = True
    Application.EnableEvents = True
    MsgBox Err.Description
    
    End Sub
    Please remember to click the * below left if this helps
    Please click the * below if this helps

  3. #3
    Registered User
    Join Date
    09-19-2013
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: need vba to split sheet into multiple sheets

    Thanks for your response! Unfortunately it didn't work. It did create all the tabs but no data was on any of them. Again, I appreciate you trying!

  4. #4
    Forum Expert JasperD's Avatar
    Join Date
    05-07-2013
    Location
    Netherlands
    MS-Off Ver
    Excel 2016
    Posts
    1,393

    Re: need vba to split sheet into multiple sheets

    On your sample it worked fine.
    Please make sure of the following :

    - Put the code in the WORKSHEET CODE AREA of worksheet with the name 'Full Report'
    - Run the code while the 'Full Report' sheet is the active sheet

    Please try again - make sure you don't put the code in a module, but in the worksheet code area of "Full Report"
    Thanks

  5. #5
    Registered User
    Join Date
    09-19-2013
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: need vba to split sheet into multiple sheets

    You are fabulous!! I am so glad you recognized my mistake & corrected me. As I said, I have no vba experience so thank you for your patience. It appears to have worked perfectly but I will review it closer in the morning. Thank you again!!!

  6. #6
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: need vba to split sheet into multiple sheets

    Based on your last post in this thread, its apparent that you are satisfied with the solution(s) you've received and have solved your question, but you haven't marked your thread as "SOLVED". I will do it for you this time.

    In future, to mark your thread as Solved, you can do the following -
    Select Thread Tools-> Mark thread as Solved.

    Incase your issue is not solved, you can undo it as follows -
    Select Thread Tools-> Mark thread as Unsolved.

    Also, since you are relatively new to the forum, i would like to inform you that you can thank those who have helped you by clicking the small star icon located in the lower left corner of the post which helped you. This adds to the reputation of the person who has taken the time to help you.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  7. #7
    Registered User
    Join Date
    09-19-2013
    Location
    NC
    MS-Off Ver
    Excel 2007
    Posts
    4

    Re: need vba to split sheet into multiple sheets

    JasperD, you did a fantastic job on this, thanks again! I do have one issue I was hoping you could fix. It didn't create tabs for every different name. 4405BE, 4405BK, & 4405BR are listed on the 4405BR tab, 4405FR & 4405FS are listed on the 4405FS tab, 4405PE & 4405PL are listed on the 4405PL tab, 4405RA & 4405RH are listed on the 4405RH tab and 4405SH is not listed on any tab. Also, I will need to add 2 columns every month, will this code allow for this? For example, it now has Jan (Col.E), Jan $ (Col.F), and Total $ (Col. G). The next month will be Jan (Col.E), Jan $ (Col.F), Feb (Col.G), Feb $ (Col.H), and Total $ (Col. I) and so on. Being a newbie, I am hoping those are very minor issues.

+ 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. Replies: 9
    Last Post: 06-06-2013, 11:25 PM
  2. [SOLVED] Split large sheet in multiple 1000 row sheets
    By thotosch in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-14-2013, 06:29 AM
  3. Split Data in Home Sheet to Multiple Existing Sheets
    By tboyle35 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-23-2012, 04:23 PM
  4. [SOLVED] Split master sheet data to multiple sheets in same workbook
    By Apple Ling in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-26-2012, 10:39 PM
  5. i need to split excel work sheet into multiple sheets
    By samehnabil in forum Excel General
    Replies: 1
    Last Post: 08-16-2012, 07:54 PM

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