Using VBA/Macros to Duplicate Sheets and Inserting Individual Rows from Table

donnington

New Member
Joined
May 31, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I am trying to automate a task that I regularly have to do at work however I don't have much experience using VBA/Macros so I was hoping someone could help?

I will try my best to explain what I want to do in words but I have also got a mini sheet below which should make it a lot easier to follow. I have 2 sheets, one called "Table" and one called "Template". The "Table" sheet contains a table (obviously) which has 7 columns and up to 50 rows (changes week on week). I want to extract each individual row from the table and insert it into row 7 on the other sheet called "Template". Since I need to do this up to 50 times, it would be great if I could duplicate the "Template" sheet for each row that I insert. Preferably, I also want to name each sheet based off it's specific code.

Can anyone think of an easy code to use for this on VBA? Do let me know if I need to provide more information.

Table Sheet:

Example.xlsx
ABCDEFGHIJ
1Column 1Column 2Column 3Column 4Column 5Column 6Column 7Code
209-Dec-204,447.0017-Jan-01963400.00%39938,155.009,254.00XX 1 YY
321-Dec-054,660.0024-Feb-01948100.00%40789,308.009,494.00XX 2 YY
429-Jun-118,529.0021-Aug-24114000.00%88662,150.002,060.00XX 3 YY
509-Jan-163,970.0005-Jan-12403300.00%14493,295.008,049.00XX 4 YY
613-Aug-171,288.0030-Jul-13562200.00%22276,027.003,781.00XX 5 YY
702-Nov-052,779.0019-Jul-07351600.00%39303,613.009,505.00XX 6 YY
821-Feb-174,079.0010-Mar-11916500.00%52159,699.006,471.00XX no. YY
917-May-059,498.0029-Apr-15206100.00%27813,045.008,193.00XX no. YY
1012-May-128,368.0029-Apr-21914600.00%37303,238.007,729.00XX no. YY
1101-Jul-122,294.0029-Jul-01137200.00%86157,525.003,300.00XX no. YY
1204-Aug-215,198.0007-Aug-26396900.00%41068,234.00623.00XX no. YY
1302-Nov-056,655.0001-Apr-11387300.00%97977,264.002,098.00XX no. YY
1426-Feb-218,997.0026-May-26362100.00%3561409.002,472.00XX no. YY
1527-Jun-096,311.0020-Dec-09664000.00%19229,061.004,054.00XX no. YY
1622-Jul-256,878.0006-Dec-11615500.00%40493,039.00462.00XX no. YY
1717-Oct-10153.0012-Jan-09329700.00%4053,064.006,732.00XX no. YY
1810-Aug-258,082.0031-Aug-16958300.00%38623,950.008,791.00XX no. YY
1906-May-269,945.0010-Aug-19247300.00%9031702.007,496.00XX no. YY
2010-Feb-242,122.0013-Oct-00750000.00%72621,880.008,620.00XX no. YY
2106-Jun-078,498.0011-Dec-03535800.00%7108462.005,650.00XX no. YY
2219-Oct-241,912.0001-Feb-09775900.00%84154,351.008,035.00XX no. YY
2304-Nov-233,253.0011-Aug-085700.00%44894,720.002,082.00XX no. YY
2412-Mar-002,289.0004-Aug-21276500.00%51989,544.005,836.00XX no. YY
2528-Dec-174,859.0024-Sep-24532100.00%90869,653.001,087.00XX no. YY
2608-Dec-107,528.0029-Apr-21947800.00%86584,713.002,631.00XX no. YY
2731-Jan-005,011.0029-Jan-0829000.00%42434,344.003,601.00XX no. YY
2804-Nov-115,663.0009-Feb-01320700.00%3562511.003,823.00XX no. YY
2919-Nov-103,913.0025-Nov-03425900.00%70928,992.008,862.00XX no. YY
3004-Dec-1515.0023-Jun-23960700.00%86253,987.00317.00XX no. YY
3116-May-139,256.0024-Nov-16504500.00%68858,891.007,698.00XX no. YY
3225-Jan-101,649.0030-Dec-25683800.00%19448,941.007,737.00XX no. YY
3315-Oct-115,361.0012-Apr-10717700.00%75848,350.003,550.00XX no. YY
3419-Jan-195,978.0009-Jan-02457300.00%36521,960.003,467.00XX no. YY
3511-Oct-077,692.0012-Jun-18685100.00%17105,190.001,485.00XX no. YY
3605-Aug-115,458.0025-Apr-24636500.00%7426684.008,802.00XX no. YY
3713-Apr-148,345.0027-Apr-25648400.00%45256,610.001,137.00XX no. YY
3824-Jan-074,225.0018-Oct-0591700.00%97136,359.005,499.00XX no. YY
3920-Jun-239,111.0026-Sep-25636300.00%2164438.009,735.00XX no. YY
4027-Oct-177,278.0004-Jul-24838300.00%318,271.007,134.00XX no. YY
4125-Nov-043,298.0025-Apr-00130900.00%55885,722.003,260.00XX no. YY
4203-Feb-083,125.0026-Feb-21775800.00%86005,434.001,098.00XX no. YY
4324-Dec-011,639.0021-Jun-07354000.00%49193,165.009,039.00XX no. YY
4416-Jan-237,270.0019-Jul-23261800.00%54954,728.00421.00XX no. YY
4528-Jun-139,167.0031-Oct-181900.00%93521,517.002,333.00XX no. YY
4619-Feb-04500.0029-Jun-26201800.00%79724,366.004,107.00XX no. YY
4725-Nov-188,512.0030-Jul-10925700.00%59738,076.001,799.00XX no. YY
4801-Oct-142,607.0005-Aug-19885600.00%59242,182.004,098.00XX no. YY
4927-Aug-257,465.0023-Jun-07995700.00%53505,318.009,547.00XX no. YY
5025-Sep-16505.0018-Nov-15446500.00%65912,806.002,473.00XX no. YY
5127-Oct-267,314.0014-Nov-09297400.00%60947,109.002,173.00XX no. YY
529244398129853281343051622616XX no. YY
Table
Cell Formulas
RangeFormula
A2:G52A2=RANDARRAY(51,7,0,10000,TRUE)
Dynamic array formulas.


Template sheet:

Example.xlsx
ABCDEFGHI
1
2
3Title
4
5
6Column 1Column 2Column 3Column 4Column 5Column 6Column 7
7Where I want to insert the individual rows
8
9
10
11
12Random information
13
14
15
16
17
18
19
20
21
22
23
24
Template


Any help would be greatly appreciated!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Hope this helps .
This code does not consider the error. If there are duplicates in column I, or if there are characters that cannot be used in the sheet name, an error will occur.
VBA Code:
Sub sample()
Dim Dws As Worksheet, Tmpws As Worksheet
Dim LR As Long, i As Long

Set Dws = Sheets("Table")
Set Tmpws = Sheets("Template")
Application.ScreenUpdating = False
With Dws
    LR = .Cells(Rows.count, 1).End(xlUp).Row
    For i = 2 To LR
        Tmpws.Copy After:=Sheets(Worksheets.count)
        ActiveSheet.Name = .Cells(i, 9).Value
        .Range(.Cells(i, 1), .Cells(i, 7)).Copy ActiveSheet.Range("A7")
    Next
End With
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hope this helps .
This code does not consider the error. If there are duplicates in column I, or if there are characters that cannot be used in the sheet name, an error will occur.
VBA Code:
Sub sample()
Dim Dws As Worksheet, Tmpws As Worksheet
Dim LR As Long, i As Long

Set Dws = Sheets("Table")
Set Tmpws = Sheets("Template")
Application.ScreenUpdating = False
With Dws
    LR = .Cells(Rows.count, 1).End(xlUp).Row
    For i = 2 To LR
        Tmpws.Copy After:=Sheets(Worksheets.count)
        ActiveSheet.Name = .Cells(i, 9).Value
        .Range(.Cells(i, 1), .Cells(i, 7)).Copy ActiveSheet.Range("A7")
    Next
End With
Application.ScreenUpdating = True
End Sub
This is incredible, I made a few adjustments to avoid errors and it works a dream. Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,099
Messages
6,170,108
Members
452,302
Latest member
TaMere

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