Hi
I have an excel worksheet with 22 columns. Columns A and B hold user identification and column C through V hold user data for different states. eg. Col C is for AZ, col D for MD and so on. I need to split the state columns into separate worksheets such that every state has it's own worksheet, with the user identification data (col A and B) repeated in each sheet.
So basically, my sheets need to look like this:
Sheet 1 - Col A, B, C
Sheet 2 - Col A, B, D
Sheet 3 - Col A, B, E
...
Sheet 20 - Col A, B, V
Any help will be greatly appreciated.
Thanks!
Try this on a copy of your workbook.
=======Code:Sub SplitStates() 'JBeaucaire (8/10/2009) Dim sh As Worksheet Dim LR As Long, LC As Long, i As Long, v As Long Set sh = ActiveSheet LR = Range("A" & Rows.Count).End(xlUp).Row LC = Cells(1, Columns.Count).End(xlToLeft).Column For i = 3 To LC v = Evaluate("ISREF(" & Cells(1, i).Text & "!A1)") If v Then 'clear existing sheet Sheets(Cells(1, i).Text).Cells.Clear Else 'create new sheet Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = Cells(1, i) sh.Activate End If 'Copy Data Range("A:B").Copy Sheets(Cells(1, i).Text).Range("A1") Columns(i).Copy Sheets(Cells(1, i).Text).Range("C1") Next i End Sub
How to use the macro:
1. Open up your workbook
2. Get into VB Editor (Press Alt+F11)
3. Insert a new module (Insert > Module)
4. Copy and Paste in your code (given above)
5. Get out of VBA (Press Alt+Q)
6. Save your sheet
The macro is installed and ready to use. Press Alt-F8 and select it from the macro list.
Last edited by JBeaucaire; 08-10-2009 at 06:50 PM.
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
Thanks
It works perfectly!
Last edited by Fawkes; 08-11-2009 at 02:40 PM. Reason: I had bad data in one of columns which caused the error. The solution was perfect
If that takes care of your need, be sure to EDIT your original post, click Go Advanced and mark the PREFIX box [SOLVED].
(Also, use the blue "scales" icon in our posts to leave Reputation Feedback, it is appreciated)
_________________
Microsoft MVP 2010 - Excel
Visit: Jerry Beaucaire's Excel Files & Macros
If you've been given good help, use theicon below to give reputation feedback, it is appreciated.
Always put your code between code tags. [CODE] your code here [/CODE]
“None of us is as good as all of us” - Ray Kroc
“Actually, I *am* a rocket scientist.” - JB (little ones count!)
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks