VBA code to copy template worksheet according to list, list has duplicates

100_cruiser

New Member
Joined
Jul 19, 2021
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
Hi All

new member here!

I have a situation where i need to copy a template tab multiple times, and name these new tabs as per a pre-defined list. I found the below code on this forum

Sub makeSheets()
Dim sh1 As Worksheet, sh2 As Worksheet, c As Range
Set sh1 = Sheets("Template")
Set sh2 = Sheets("Points")
For Each c In sh2.Range("B5", sh2.Cells(Rows.Count, 2).End(xlUp))
sh1.Copy After:=Sheets(Sheets.Count)
ActiveSheet.Name = c.Value
Next
End Sub


This works, except for when i get to a point in my reference list where there are duplicate values. What i am looking for is how to make it ignore the duplicates (create the first value) and then move on to the next unique reference in the list

An example of the reference list i am using

1626730097795.png


any help appreciated
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi and welcome to MrExcel.

Try this:

VBA Code:
Sub makeSheets()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim c As Range
  Dim dic As Object, ky As Variant
  
  Set sh1 = Sheets("Template")
  Set sh2 = Sheets("Points")
  Set dic = CreateObject("Scripting.Dictionary")
  
  For Each c In sh2.Range("B5", sh2.Cells(Rows.Count, 2).End(xlUp))
    dic(c.Value) = Empty
  Next
  For Each ky In dic.keys
    sh1.Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = ky
  Next
End Sub
 
Upvote 0
Solution
Hi and welcome to MrExcel.

Try this:

VBA Code:
Sub makeSheets()
  Dim sh1 As Worksheet, sh2 As Worksheet
  Dim c As Range
  Dim dic As Object, ky As Variant
 
  Set sh1 = Sheets("Template")
  Set sh2 = Sheets("Points")
  Set dic = CreateObject("Scripting.Dictionary")
 
  For Each c In sh2.Range("B5", sh2.Cells(Rows.Count, 2).End(xlUp))
    dic(c.Value) = Empty
  Next
  For Each ky In dic.keys
    sh1.Copy After:=Sheets(Sheets.Count)
    ActiveSheet.Name = ky
  Next
End Sub
thankyou so much! that worked great......very much appreciated
 
Upvote 0

Forum statistics

Threads
1,214,397
Messages
6,119,271
Members
448,882
Latest member
Lorie1693

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