I am very new to excel and VBA in general. I am using the following code in a userform to generate a new sheet based on a column range named "Weeks", each cell is formatted as 07-JAN, 14-JAN, 21-JAN etc. I use this column to provide a list of weeks for data collection and to provide a predefined list of weeks to create new sheets. I would like the sheets to organize themselves no matter when they may created. so if I have a sheet for week 1,2,3,5,8 and I want to create Week 7 then it will place it between 5 and 8. I assume the process is to assign the new sheet name to a var then look at each cell before it and if it exists, then put that value into a variable which would be the entry for .copy After function. I struggle with how to do this efficiently though. obviously, the column range is 52 cells long if that helps at all. Thank you for any help.
VBA Code:
Private Sub CommandButton1_Click()
Dim exists As Boolean
For I = 1 To Worksheets.Count
If Worksheets(I).Name = ComboBox1 Then
exists = True
MsgBox ComboBox1 & " already exists.", vbOKOnly + vbInformation
End If
Next I
If Not exists Then
Sheets("TEMPLATE").Copy After:=Sheets("Template")
ActiveSheet.Name = ComboBox1
End If
Call Unload(NewWeeklySheet)
End Sub