+ Reply to Thread
Results 1 to 7 of 7

How can I Split this onto multiple Worksheets using a Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    09-17-2013
    Location
    Wales
    MS-Off Ver
    Excel 2007
    Posts
    10

    How can I Split this onto multiple Worksheets using a Macro

    Afternoon All,

    I have a report Generated from an external source, I Need to copy each customers information into a separate worksheet preferably names as the customer accounts number located in C4, and ever 27th line after. each sheet would then contain all the information for the customer including headings. the problem i have is that the customer list will change as new customers are added onto the list.

    Any help you guys could give would be appreciated i'm slowly picking up some VBA, but at the moment I'm just feeling around in the dark so any help or a nudge in the right direction would be appreciated.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: How can I Split this onto multiple Worksheets using a Macro

    Maybe:

    Sub kalvas()
    Dim rcell As Range
    Dim ws1 As Worksheet
    Application.Calculation = xlCalculationManual
    Application.ScreenUpdating = False
    Set ws1 = Sheets("Sheet1")
    ws1.Activate
    For Each rcell In Range("C4:C" & Range("C" & Rows.count).End(3)(1).Row)
        If Len(rcell) > 2 Then
            Sheets.Add.Name = rcell.Value
            Sheets(rcell.Value).Columns(1).ColumnWidth = 5.14
            Sheets(rcell.Value).Columns(2).ColumnWidth = 11.29
            Sheets(rcell.Value).Columns(3).ColumnWidth = 7.29
            Sheets(rcell.Value).Columns(4).ColumnWidth = 27.14
            Sheets(rcell.Value).Columns(5).ColumnWidth = 8.57
            ActiveSheet.Rows("1:2").Value = ws1.Rows("2:3").Value
            ws1.Activate
            Range(Cells(rcell.Row, "A"), Cells(rcell.Row, "E")).Resize(26).Copy Sheets(rcell.Value).Range("A" & Rows.count).End(3)(2)
        End If
    ws1.Activate
    Next rcell
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
    End Sub
    Last edited by JOHN H. DAVIS; 06-10-2014 at 08:23 AM.

  3. #3
    Valued Forum Contributor Miroslav R.'s Avatar
    Join Date
    05-16-2013
    Location
    NMnV, Slovakia
    MS-Off Ver
    Excel 2007
    Posts
    479

    Re: How can I Split this onto multiple Worksheets using a Macro

    Hi there,
    Option Explicit
    Sub Split()
    Dim shtSrc As Worksheet
    Dim xRow As Integer, xBreak As Integer: xBreak = 4
    
    Application.ScreenUpdating = False
    Set shtSrc = Sheets("Sheet1")
    
    For xRow = 5 To shtSrc.Cells(Rows.Count, 1).End(xlUp).Row
      If shtSrc.Cells(xRow, 2) = "Company" Or shtSrc.Cells(xRow, 1) = "Totals" Then
        Worksheets.Add After:=ActiveSheet
        With ActiveSheet
          .Name = shtSrc.Cells(xBreak, 3)
          shtSrc.Range("A2:E3").Copy .Range("A2:E3")
          shtSrc.Cells(xBreak, 1).Resize(xRow - xBreak, 5).Copy .Range("A4")
          .Range("A:E").EntireColumn.AutoFit
        End With
      xBreak = xRow
      End If
    Next xRow
    
    Application.ScreenUpdating = True
    End Sub
    Hope it helps
    Regards
    Miroslav R.

    (If You like my solutions, feel free to add reputation.)

  4. #4
    Valued Forum Contributor Miroslav R.'s Avatar
    Join Date
    05-16-2013
    Location
    NMnV, Slovakia
    MS-Off Ver
    Excel 2007
    Posts
    479

    Re: How can I Split this onto multiple Worksheets using a Macro

    double post
    Last edited by Miroslav R.; 06-10-2014 at 08:28 AM. Reason: deleted

  5. #5
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: How can I Split this onto multiple Worksheets using a Macro

    Hi Kalvas,

    Must say I'm not a fan of individual tabs unless absoluately needed as you could just filter the data in Sheet1 and navigation around a 53 tab workbook is cumbersome.

    That said, this will do the job:

    Option Explicit
    
    Sub Macro1()
    
        Const lngStartRow As Long = 4 'Consolidated data row start. Change to suit.
    
        Dim lngMyRow As Long, _
            lngEndRow As Long
        Dim wstMySheet As Worksheet
        Dim strMySheetName As String
        
        Application.ScreenUpdating = False
        
        lngEndRow = Sheets("Sheet1").Range("A:E").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        For lngMyRow = lngStartRow To lngEndRow Step 27
            'Create a tab only if there's value in lngMyRow of Col. C of 'Sheet1' and only if it doesn't already exist in the workbook
            If Len(Sheets("Sheet1").Range("C" & lngMyRow)) > 0 Then
                On Error Resume Next
                    strMySheetName = CStr(Sheets("Sheet1").Range("C" & lngMyRow))
                    Set wstMySheet = Sheets(strMySheetName)
                    If Err.Number <> 0 Then
                        Worksheets.Add after:=Worksheets(Worksheets.Count)
                        Worksheets(Worksheets.Count).Name = strMySheetName
                        Sheets("Sheet1").Range("A2:E3").Copy Destination:=Sheets(strMySheetName).Range("A1")
                        Sheets("Sheet1").Range("A" & lngMyRow & ":E" & lngMyRow + 26).Copy Destination:=Sheets(strMySheetName).Range("A3")
                        Sheets(strMySheetName).Range("A:E").EntireColumn.AutoFit
                        Err.Clear
                    End If
                On Error GoTo 0
            End If
        Next lngMyRow
        
        Application.ScreenUpdating = True
        
        MsgBox "Tabs have now been created.", vbInformation
    
    End Sub
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  6. #6
    Registered User
    Join Date
    09-17-2013
    Location
    Wales
    MS-Off Ver
    Excel 2007
    Posts
    10

    Re: How can I Split this onto multiple Worksheets using a Macro

    Thanks Guys, that works brilliantly. But you are off course correct having it create a new work book for every account isn't the ideal situation. but how else would you suggest handling the information given that it will need to be printed?

  7. #7
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,565

    Re: How can I Split this onto multiple Worksheets using a Macro

    how else would you suggest handling the information given that it will need to be printed?
    Untested, but this should do the job:

    Option Explicit
    
    Sub Macro1()
    
        Const lngStartRow As Long = 4 'Consolidated data row start. Change to suit.
    
        Dim lngMyRow As Long, _
            lngEndRow As Long
        
        Application.ScreenUpdating = False
        
        lngEndRow = Sheets("Sheet1").Range("A:E").Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
        
        For lngMyRow = lngStartRow To lngEndRow Step 27
            With Sheets("Sheet1")
                .PageSetup.PrintArea = "$A$" & lngMyRow & ":$E$" & lngMyRow + 26
                .PrintOut
            End With
        Next lngMyRow
        
        Application.ScreenUpdating = True
        
        MsgBox "Accounts have now been printed.", vbInformation
    
    End Sub
    Robert

+ 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] Macro - Split into Multiple Worksheets
    By jonno in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 07-19-2013, 01:46 PM
  2. [SOLVED] Macro to split worksheet to multiple worksheets
    By daillest319 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 03-30-2013, 04:58 AM
  3. [SOLVED] Split data in one worksheets into multiple sheets?
    By cahillct in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-14-2012, 07:51 AM
  4. Split worksheet into multiple worksheets
    By ashtanga in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-12-2009, 04:43 PM
  5. split results to multiple worksheets
    By Rich in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-11-2005, 03:06 PM

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