VBA for duplicating slide and changing name based on table

Legil

New Member
Joined
Jan 9, 2023
Messages
7
Office Version
  1. 2021
Platform
  1. Windows
Hi Everyone!

This is my first post and I just have one question

Let's say that we have two separate sheets, the first one, witha table of information for each teammate, like this one

IDNameLast Name
1AD
2BE
3CE

And another sheet (let's call it "List" the one we want to copy N times for each of the teammates)

How can we use a VBA formula so that when I press a button, it copies the "List" tab and it renames it to the ID that each teammate has?

For example: the result for this scenario would be generating three new sheets, all of them copies of the "List" one, but these three new ones would have the names "1", "2" and "3" respectively

Thanks a lot in advance for this
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hey, please try the following code:

VBA Code:
Sub CreateSheetsFromTable()
    Dim ws As Worksheet
    Dim newWs As Worksheet
    Dim row As Long
    Dim lastRow As Long
    Dim id As Long
    Dim name As String

    Set ws = ThisWorkbook.Sheets("Sheet1") ' change to the name of your first sheet
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' get the last row with data in column A

    For row = 2 To lastRow ' loop through the rows, starting from the second row (assuming the first row is the header)
        id = ws.Cells(row, "A").Value ' get the ID
        name = ws.Cells(row, "B").Value & " " & ws.Cells(row, "C").Value ' get the name and last name

        Set newWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) ' add a new sheet at the end of the workbook
        newWs.Name = CStr(id) ' rename the new sheet with the ID
        ThisWorkbook.Sheets("List").Copy Before:=newWs ' copy the "List" sheet before the new sheet
    Next row
End Sub
 
Upvote 0
Hey, please try the following code:

VBA Code:
Sub CreateSheetsFromTable()
    Dim ws As Worksheet
    Dim newWs As Worksheet
    Dim row As Long
    Dim lastRow As Long
    Dim id As Long
    Dim name As String

    Set ws = ThisWorkbook.Sheets("Sheet1") ' change to the name of your first sheet
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row ' get the last row with data in column A

    For row = 2 To lastRow ' loop through the rows, starting from the second row (assuming the first row is the header)
        id = ws.Cells(row, "A").Value ' get the ID
        name = ws.Cells(row, "B").Value & " " & ws.Cells(row, "C").Value ' get the name and last name

        Set newWs = ThisWorkbook.Sheets.Add(After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)) ' add a new sheet at the end of the workbook
        newWs.Name = CStr(id) ' rename the new sheet with the ID
        ThisWorkbook.Sheets("List").Copy Before:=newWs ' copy the "List" sheet before the new sheet
    Next row
End Sub

Hey! Thank you very much for this input! I just tried it and it works, the only thing is that for example, the original file has this structure

IDList

And the desired result would be this one

IDList1 (First copy of "List tab")2 (Second copy of "List tab")3 (Third copy of "List tab")

With the code you replied with, I am getting this

IDListList (1)1List (2)2List (3)3

How can this be corrected?
 
Upvote 0
I am sorry, can you try this new version?

VBA Code:
Sub CreateSheetsFromTable()
    Dim ws As Worksheet
    Dim newWs As Worksheet
    Dim row As Long
    Dim lastRow As Long
    Dim id As Long
    Dim name As String

    Set ws = ThisWorkbook.Sheets("List") ' change to the name of your first sheet
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).row ' get the last row with data in column A

    For row = 2 To lastRow ' loop through the rows, starting from the second row (assuming the first row is the header)
        id = ws.Cells(row, "A").Value ' get the ID
        name = ws.Cells(row, "B").Value & " " & ws.Cells(row, "C").Value ' get the name and last name
        With ThisWorkbook
        .Sheets("List").Copy After:=.Sheets(.Sheets.Count)
        End With
        Set newWs = ThisWorkbook.ActiveSheet
        newWs.name = CStr(id) ' rename the new sheet with the ID
    Next row
End Sub
 
Upvote 0
Solution
VBA Code:
Sub op()
            Dim wk1, wk2 As Worksheet
            Set wk1 = Sheets("ID") 'change sheet
            Set wk2 = Sheets("List") 'change sheet
            Dim k As Integer
            Dim lr As Long
          
            lr = wk1.Range("A" & Rows.Count).End(xlUp).Row
          
            For k = 2 To lr
                    wk2.Copy after:=Sheets(Sheets.Count)
                    ActiveSheet.Name = k - 1
            Next k
          
          

End Sub
 

Attachments

  • 1673276755360.png
    1673276755360.png
    20.4 KB · Views: 18
Upvote 0
Thank you so much for both of you! your answers helped me structure the code and now its working with the functionality I was looking for

Thanks a lot for the input1
 
Upvote 0

Forum statistics

Threads
1,224,820
Messages
6,181,159
Members
453,021
Latest member
Justyna P

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