Sub renameWorksheet()
Dim ws As Worksheet
For Each ws In ThisWorkbook.Worksheets
On Error Resume Next
ws.Name = Range("O11").Value & "-" & Range("N11").Value
Next ws
End Sub
Because when I rename worksheets and there are two or more worksheets names duplicate, get this error:
That sheet name is already taken. Try another one
Therefore I want if the worksheet name is same with other existing worksheet, then rename the worksheet with "_2". How can I modify the detect part?
(Update 1) I had updated my code and found that there are still errors, could someone give me some suggestion?
Sub renameWorksheet()
Dim ws As Worksheet
Dim rename As String
Dim rng As Worksheet
Dim i As Integer
i = 1
For Each ws In ThisWorkbook.Worksheets
rename = rng.Range("O11").Value & "-" & rng.Range("N11").Value & "-" & i
If rename = rng.Name Then
i = i + 1
ws.Name = rename
Else
ws.Name = rename
End If
rng.Name = rename
Next ws
End Sub
and now return an error:
Object variable or With block variable not set