copy sheet and rename macro

moossienl

New Member
Joined
Jan 10, 2010
Messages
4
Hello, I have the following macro:

Sub Macro2()
Dim r As Range
Set r = [a6]
Do Until r = ""
With Sheets("sheet2").Copy(, Worksheets(Worksheets.Count))
ActiveSheet.Name = r
Range("A2").Value = ActiveSheet.Name
End With
Set r = r.Offset(1)
Loop
Endsub


It creates a copy of "sheet2" and renames it to cell A6, A7 etc of the sheet selected when running the macro. Problem When I run the macro for the first time (with a value in A6 and A7) there are no problems When I add something in cell A8 and run it again (or just run the macro again), it doesnt work anymore, because the sheets of A6 and A7 are already created. Deleting all the sheets and running the macro is a solution, but slows down the process. Do you guys have a better solution for my problem? Thx in advance Moossie
 
Last edited:

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
You could add this in (stolen from here http://www.mrexcel.com/forum/showthread.php?t=3228):

Rich (BB code):
Public Function WorksheetExists(ByVal WorksheetName As String) As Boolean 
 
On Error Resume Next
WorksheetExists = (Sheets(WorksheetName).Name <> "")
On Error GoTo 0
 
End Function

...and then change your code to be this:

Rich (BB code):
Sub Macro2() 
Dim r As Range 
Set r = [a6] 
Do Until r = "" 
If not WorkSheetExists(r.value) Then
With Sheets("sheet2").Copy(, Worksheets(Worksheets.Count)) 
ActiveSheet.Name = r 
Range("A2").Value = ActiveSheet.Name 
End With 
End If
Set r = r.Offset(1) 
Loop 
End Sub
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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