Public Function AddSheetWithNameCheckIfExists(i As Integer) As Worksheet
Dim ws As Worksheet
Dim newSheetName As String
newSheetName = "result" & i ' Substitute your range here
For Each ws In Worksheets
If ws.Name = newSheetName Or newSheetName = "" Or IsNumeric(newSheetName) Then
MsgBox "Sheet already exists or name is invalid", vbInformation
Exit Function
End If
Next
Sheets.Add Type:="Worksheet"
With ActiveSheet
.Move after:=Worksheets(Worksheets.Count)
.Name = newSheetName
End With
Set AddSheetWithNameCheckIfExists = ActiveSheet
End Function
Dim ws As Worksheet
Dim newSheetName As String
newSheetName = "result" & i ' Substitute your range here
For Each ws In Worksheets
If ws.Name = newSheetName Or newSheetName = "" Or IsNumeric(newSheetName) Then
MsgBox "Sheet already exists or name is invalid", vbInformation
Exit Function
End If
Next
Sheets.Add Type:="Worksheet"
With ActiveSheet
.Move after:=Worksheets(Worksheets.Count)
.Name = newSheetName
End With
Set AddSheetWithNameCheckIfExists = ActiveSheet
End Function