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:

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Dazzawm,

I have uploaded a file to show the final result.

That does make a difference - thanks.


Sample raw data:


Excel 2007
ABCDEFGHIJKLMNOPQRS
1PartDescription12345678910111213141516TOTAL
2J4825024Test 1120222020200000013
3J4965013Test 2200122220200000013
4J4828004Test 3020122120100000011
5
Sheet1


After the macro in just 3 of the 16 new worksheets:


Excel 2007
ABC
1PartDescription1
2J4825024Test 11
3J4965013Test 22
4J4828004Test 30
5
1



Excel 2007
ABC
1PartDescription10
2J4825024Test 12
3J4965013Test 22
4J4828004Test 31
5
10



Excel 2007
ABC
1PartDescription16
2J4825024Test 10
3J4965013Test 20
4J4828004Test 30
5
16


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()
' hiker95, 07/14/2014, ME791312
Dim w1 As Worksheet
Dim lr As Long, lc As Long
Dim a As Variant, i 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
  a = .Range(.Cells(1, 1), .Cells(lr, lc))
End With
For c = 3 To lc - 1
  w = a(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(, 2).Value = Array("Part", "Description")
    .Range(.Cells(2, 1), .Cells(lr, 2)).Value = w1.Range(w1.Cells(2, 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
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 macro.
 
Upvote 0
Thanks Hiker that works great. Could I ask for just one little thing to be changed please. As I will be using this on many different files rather than it looking for sheet 1 could it look at the active sheet, because they won't always be called sheet 1. Will this be a problem?
 
Upvote 0
I think I did it. I changed Set w1 = Sheets("Sheet1") to Set w1 = ActiveSheet

Please correct me if I'm wrong but it seemed to work!
 
Upvote 0
Dazzawm,

I think I did it. I changed Set w1 = Sheets("Sheet1") to Set w1 = ActiveSheet

Please correct me if I'm wrong but it seemed to work!

As long as the ActiveSheet is setup the same as Sheet1 in my reply #3, and, it could have more rows, and, columns, it should work correctly.
 
Upvote 0
Thanks again Hiker. I thought it better to remain on this thread than start another one as I need another code that sort of has something to do with this. Once all the tabs are created and the data moved I need another code that will delete any row that does not have a number greater than zero in column C (0 or a minus number) on each created sheet. I don't need it incorporated in the code you have already written as I won't always need to use it, which is why I have asked for a separate one.
 
Upvote 0
Dazzawm,

I would think that you should start a new thread, with screenshots before, and, after.

And, in your new thread, include a link to this thread.
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,495
Members
449,088
Latest member
Melvetica

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