Create Sheets And Copy data To Each Sheet

Dazzawm

Well-known Member
Joined
Jan 24, 2011
Messages
3,748
Office Version
  1. 365
Platform
  1. Windows
I have sheet 1 as laid out below I need a macro that will create sheets and name them the headings that is in row 1 starting in column C and copy their respective data. The amount of columns may vary on every file I use the code on.

Excel 2010
ABCDEFGHIJKLMNOPQRS
1PartDescription12345678910111213141516TOTAL
2J4825024Test 1120222020200000013
3J4965013Test 2200122220200000013
4J4828004Test 3020122120100000011

<tbody>
</tbody>
Sheet1



This is how it would look with just a couple of the sheets as an example.

Excel 2010
ABC
1PartDescription
2J4825024Test 11
3J4965013Test 22
4J4828004Test 30

<tbody>
</tbody>
1



Excel 2010
ABC
1PartDescription
2J4825024Test 12
3J4965013Test 20
4J4828004Test 32

<tbody>
</tbody>
2



Thanks, any further clarification please let me know.
 
Last edited:
The first sheet below is the raw data with a 'total' column in K and the code works perfect and splits it in to sheets named 1, 3, 4, 5, 6, 7, 8 & 10 just as I need it to.


Excel 2010
ABCDEFGHIJK
1PartDescription134567810Totals
2N4892046test1101220129
3J4962019test2010201004
4J4962026test30012-10103
Sheet1


The sample below does not have a 'totals' column so when the code is run I only get sheets named 1, 3, 4, 5, 6, 7 & 8. No sheet 10


Excel 2010
ABCDEFGHIJ
1PartDescription134567810
2N4892046test110122012
3J4962019test201020100
4J4962026test30012-1010
Sheet1


I hope this is what you need as I don't know how to post a shot of the file after its been split into sheets. Thanks.
 
Upvote 0

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Dazzawm,

You did not show what at least one of the resulting worksheets would look like. I hope that the following is correct.


Sample raw data in worksheet Sheet1:


Excel 2007
ABCDEFGHIJ
1PartDescription134567810
2N4892046test110122012
3J4962019test201020100
4J4962026test30012-1010
5
Sheet1


After the macro in just 3 of the 8 new worksheets (for brevity sake):


Excel 2007
ABC
1PartDescription1
2N4892046test11
3J4962019test20
4J4962026test30
5
1



Excel 2007
ABC
1PartDescription6
2N4892046test12
3J4962019test20
4J4962026test3-1
5
6



Excel 2007
ABC
1PartDescription10
2N4892046test12
3J4962019test20
4J4962026test30
5
10


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Sub CreateSheets_V3()
' hiker95, 08/20/2014, ME791312
Dim w1 As Worksheet
Dim lr As Long, lc As Long, c As Long, w As String
Application.ScreenUpdating = False
Set w1 = Sheets("Sheet1")
With w1
  lr = .Cells(.Rows.Count, 1).End(xlUp).Row
  lc = .Cells(1, .Columns.Count).End(xlToLeft).Column
  For c = 3 To lc
    w = .Cells(1, c)
    If Not Evaluate("ISREF('" & w & "'!A1)") Then Worksheets.Add(After:=Sheets(Sheets.Count)).Name = w
    With Sheets(w)
      .UsedRange.Clear
      .Cells(1, 1).Resize(lr, 2).Value = w1.Range(w1.Cells(1, 1), w1.Cells(lr, 2)).Value
      .Range(.Cells(1, 3), .Cells(lr, 3)).Value = w1.Range(w1.Cells(1, c), w1.Cells(lr, c)).Value
      .Columns.AutoFit
    End With
  Next c
End With
w1.Activate
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the CreateSheets_V3 macro.
 
Upvote 0
Works perfect, it does add a totals sheet at the end but that's no problem I can just delete that. Thanks for your time.
 
Upvote 0
Dazzawm,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0

Forum statistics

Threads
1,215,471
Messages
6,124,999
Members
449,201
Latest member
Lunzwe73

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