Copy sheets and remame based on range of values

danno79

New Member
Joined
Oct 28, 2009
Messages
45
I have the following piece of code which copies worksheets, renames them, based on values from a range and puts that value in a specified cell in the copied worksheet. This all works fine but I'd like to add fuctionality to it so that when the macro is run again it creates worksheets in the same way for "new" entries onto the list, ignoring those that already exist. (ie not returning an error by trying to create another worksheet with the same name). I'm guessing it will involve "On Error Go to" somewhre but not sure where to add this in. Any help greatly appreciated


Code:
Sub AutoAddPrograms()

    Sheets("MAIN DATA ENTRY2").Select
    Dim i As Integer
    Dim wks As Worksheet
     
    Set wks = Sheets("MAIN DATA ENTRY2")
   
    For i = Range("B40").Value To 8 Step -1 'Starts at the bottom and works up
            Sheets(5).Copy After:=Sheets(5)
            ActiveSheet.Name = wks.Cells(i, 2)
            ActiveSheet.Cells(1, 3) = wks.Cells(i, 2)
       
    Next i
     
End Sub
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Perhaps

Rich (BB code):
Sub AutoAddPrograms()

    Sheets("MAIN DATA ENTRY2").Select
    Dim i As Integer
    Dim wks As Worksheet
     
    Set wks = Sheets("MAIN DATA ENTRY2")
   
    For i = Range("B40").Value To 8 Step -1 'Starts at the bottom and works up
            Sheets(5).Copy After:=Sheets(5)
            On Error Resume Next
            ActiveSheet.Name = wks.Cells(i, 2)
            On Error GoTo 0
            ActiveSheet.Cells(1, 3) = wks.Cells(i, 2)
       
    Next i
     
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,583
Messages
6,125,665
Members
449,247
Latest member
wingedshoes

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