Create Multiple Sheets from Data in Range

sdh31873

New Member
Joined
Jan 29, 2007
Messages
36
I have an Excel file with Acct #'s, Item #'s, and Sales columns. I have multiple Acct #'s with Multiple Item #'s and I need to break these up by acct # into new worksheets.

Somewhat like a Subtotal (at each change in Acct #) but I want each new acct # to be on a new worksheet. ASAP has something of the sort but I can't see the code. In ASAP utilities it is called " 2. Quickly add multiple sheets (with the names &defined in selected cells)..."

I use this but I need to incorporate this into some other coding I am doing and it is password protected. Any help is appreciated.

Thanks.
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Try this on a copy of your sheet

Code:
Sub CityToSheet()
Dim lastrow As Long, LastCol As Integer, i As Long, iStart As Long, iEnd As Long
Dim ws As Worksheet
Application.ScreenUpdating = False
With Sheets("Master")
    lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
    LastCol = .Cells(1, Columns.Count).End(xlToLeft).Column
    iStart = 2
    For i = 2 To lastrow
        If .Range("A" & i).Value <> .Range("A" & i + 1).Value Then
            iEnd = i
            Sheets.Add after:=Sheets(Sheets.Count)
            Set ws = ActiveSheet
            On Error Resume Next
            ws.Name = .Range("A" & iStart).Value
            On Error GoTo 0
            ws.Range(Cells(1, 1), Cells(1, LastCol)).Value = .Range(.Cells(1, 1), .Cells(1, LastCol)).Value
            .Range(.Cells(iStart, 1), .Cells(iEnd, LastCol)).Copy Destination:=ws.Range("A2")
            iStart = iEnd + 1
        End If
    Next i
End With
Application.CutCopyMode = False
Application.ScreenUpdating = True
End Sub

Change worksheet reference etc. to suit. This assumes that the data are sorted.
 
Upvote 0
since I already worked on it...

Assuming that your list of accts starts in cell A1 and you run this macro from the page that contains the accounts.

Code:
Sub test()

Dim lastrw As Long
lastrw = Cells(Rows.Count, 1).End(xlUp).Row

Dim wb As Workbook
Dim ws As Worksheet


cs = ActiveSheet.Name
j = ThisWorkbook.Sheets.Count
 
Set wb = ActiveWorkbook

For Each cell In Range(Cells(1, 1), Cells(lastrw, 1))
    x = cell.Value
    For Each ws In wb.Worksheets
        If ws.Name = x Then
        j = 0
            Sheets(cs).Activate
            cell.EntireRow.Copy
            ws.Activate
            cpy_rw = Cells(Rows.Count, 1).End(xlUp).Row
            Cells(cpy_rw + 1, 1).Select
            ActiveSheet.Paste
            Sheets(cs).Activate
        End If
    Next
    If j = 0 Then
        j = ThisWorkbook.Sheets.Count 'reset the counter
    Else
        'This section adds and moves the new sheet
        Sheets.Add
        ActiveSheet.Name = cell.Value
        j = ThisWorkbook.Sheets.Count 'reset the counter
        ActiveSheet.Move After:=Sheets(j)
        
        Sheets(cs).Activate
        cell.EntireRow.Copy
        Sheets(cell.Value).Activate
        cpy_rw = Cells(Rows.Count, 1).End(xlUp).Row
        Cells(cpy_rw + 1, 1).Select
        ActiveSheet.Paste
        Sheets(cs).Activate

    End If
    
Next


End Sub/[code]
 
Upvote 0

Forum statistics

Threads
1,214,661
Messages
6,120,796
Members
448,994
Latest member
rohitsomani

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top