I have some code in Excel VBA, what it does is that whenever a user opens a specific workbook what it does it copies the content from the last sheet, then puts this new sheet at the end, and then it gives a new name to the sheet by putting a date format. The problem is that if for some reason you need to open again this workbook the same day it gives an error as the name of the sheet is already taken.
I'm trying to find a solution where if the name already exists then do not add a new sheet. Probably I've got some part of the code right but I'm guessing probably the order of the code lines is what is making it wrong.
Sub Auto()
Sheets(Sheets.Count).Select
Sheets(Sheets.Count).Copy After:=Sheets(Sheets.Count)
Sheets(Sheets.Count).Select
For i = 1 To Sheets.Count
If Sheets(i).Name = UCase(Format(Now, "DDMMM")) Then
exists = True
End If
Next i
If Not exists Then
Sheets(Sheets.Count).Name = UCase(Format(Now, "DDMMM"))
End If
Range("A3:P3").Select
ActiveCell.FormulaR1C1 = Format(Now, "medium date")
End Sub
I just added the loop condition but 2 things are wrong for this code. The if will go on and look up for every sheet in the workbook so if you have 200 sheets prob it will take sometime to go out of the loop. The other is that whenever you add a sheet that is a copy of another one Excel will add to the name of the sheet the corresponding copy (Sheet (2)) So the loop will do nothing as it will never find the same name.
So if you've got a better way to check if the sheet name already exists I will be glad to learn.
Thanks