Hello - can someone help me with a macro that would separate the data in "All" into separate sheets by Supervisor? I have attached a test with an example. Thank you so much!!
Hello - can someone help me with a macro that would separate the data in "All" into separate sheets by Supervisor? I have attached a test with an example. Thank you so much!!
Try this code
Copy the Excel VBA code![]()
Option Explicit Sub split_data() Dim i As Long, lrow As Long Dim sname As String Application.ScreenUpdating = False For i = 1 To Worksheets.Count If Worksheets(i).Name <> "All" Then lrow = Worksheets(i).Range("A" & Rows.Count).End(xlUp).Row If lrow > 1 Then Worksheets(i).Range("A2:O" & lrow).Delete End If Next i With Worksheets("All") lrow = .Range("B" & .Rows.Count).End(xlUp).Row For i = 2 To lrow sname = .Range("C" & i).Value .Range("B" & i & ":P" & i).Copy Worksheets(sname).Range("A" & Rows.Count).End(xlUp).Offset(1, 0) Next i End With Application.ScreenUpdating = True End Sub
Select the workbook in which you want to store the Excel VBA code
Hold the Alt key, and press the F11 key, to open the Visual Basic Editor
Choose Insert | Module
Where the cursor is flashing, choose Edit | Paste
To run the Excel VBA code:
Choose View | Macros
Select a macro in the list, and click the Run button
Let me know if you need a small addition to the code which checks if the sheetname exists and adds one if it doesnt.
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]
Thanks, that would be great! Also, I noticed that when I deleted someone from "all" it wasn't deleted on the Sup sheet. I guess I would just rerun the macro when I delete someone?
Yes, if you delete / add someone, you need to rerun the code. It will clear all the sup sheets and re-populate.
The updated code is here -
![]()
Option Explicit Sub split_data() Dim i As Long, lrow As Long Dim sname As String Application.ScreenUpdating = False For i = 1 To Worksheets.Count If Worksheets(i).Name <> "All" Then lrow = Worksheets(i).Range("A" & Rows.Count).End(xlUp).Row If lrow > 1 Then Worksheets(i).Range("A2:O" & lrow).Delete End If Next i With Worksheets("All") lrow = .Range("B" & .Rows.Count).End(xlUp).Row For i = 2 To lrow sname = .Range("C" & i).Value If Not Evaluate("ISREF('" & sname & "'!A1)") Then Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sname .Range("B1:P1").Copy Worksheets(sname).Range("A1") End If .Range("B" & i & ":P" & i).Copy Worksheets(sname).Range("A" & Rows.Count).End(xlUp).Offset(1, 0) worksheets(sname).cells.entirecolumn.autofit Next i End With Application.ScreenUpdating = True End Sub
Last edited by arlu1201; 08-14-2012 at 07:51 AM.
When I try in the real workbook (which has actual supervisor names rather than "Sup 1"), I am getting "variable not defined" - am I missing a step?
I have edited the code above. I had some extra text in there by mistake.
Please try and let me know.
Now I'm getting subscript out of range...
Which line is highlighted in yellow when you get the error?
With Worksheets("All")
Is your main worksheet named "All" as it was in the summary file? When you get a subscript out of range error, its all got to do with the sheetname.
Now I feel dumb- I should have figured that out! It works beautifully now! Thank you for your patience and assistance!!
When I rerun the macro it's not replacing the sheets - I have to delete the sheets and then rerun the macro. Fixable?
---------- Post added at 10:58 AM ---------- Previous post was at 10:53 AM ----------
And while I'm asking, is there a way for the individual sheets to maintain the formatting (column width etc) of the "all" sheet? Thanks!
The macro will clear the contents of the existing sheets and re-populate with data.
If the sheet name does not exist, it will create it for you.
I have added the code line to autofit the columns of each sheet.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks