I hit a wall creating my first loop. The sheet rename was created for and works in ThisWorkbook but I want to convert it to loop through all worksheets as a button push.
Conditions:
Must exclude renaming 5 sheets.
B1 on each sheet contains an email or first name last name to be used as the sheet name in proper case.
If B1 is email address sheetname is name@domain
If B1 is text sheetname is first name and last initial
If error or duplicate sheetname is B1
Conditions:
Must exclude renaming 5 sheets.
B1 on each sheet contains an email or first name last name to be used as the sheet name in proper case.
If B1 is email address sheetname is name@domain
If B1 is text sheetname is first name and last initial
If error or duplicate sheetname is B1
VBA Code:
Sub SetupWks() '(ByVal sh As Object, ByVal Target As Range)
Dim sh As Object
Dim Target As Range
' START LOOP ***********************************************************
Dim ws As Worksheet
For Each sh In ActiveWorkbook.Worksheets
Select Case sh.Name
Case Is <> "Sheet1", "Sheet2", "Sheet3", "Sheet4", "Sheet5"
Case Else
With sh
' START SHEET RENAME ***********************************************************
If Target.Address <> "$B$1" Then Exit Sub
If Trim(Target.Address) = "" Then Exit Sub
On Error Resume Next
' IF Range is Email name sheet name@domain
If Target Like "*[\!%^:~#|@.;`\/*$,""]*" Then
sh.Name = Left(Target, Len(Target) - 4)
sh.Name = StrConv(sh.Name, vbProperCase)
Else
' If range is text name sheet first name last initial
sh.Name = Split(Replace(Target, " ", " "), " ")(0) & " " & Left(Split(Replace(Target, " ", " "), " ")(1), 1)
sh.Name = StrConv(sh.Name, vbProperCase)
' If error return B1
If Err.Number <> 0 Then
sh.Name = B1
sh.Name = StrConv(sh.Name, vbProperCase)
Exit Sub
End If
End If
On Error GoTo 0
' END SHEET RENAME ***********************************************************
' END LOOP ***********************************************************
End With
End Select
Next sh
End Sub