automatically rename tab sheet VBA

March 23, 2011 at 02:10:09
Specs: Windows 7
Hi,
I want to rename excel sheet name tab automatically based on a cell (in the same sheet).

i know little about VBA but i could find this code which should work.. but it doen't.
can you help correct this code or provide an alternative solution.
thanks
Nicolas

Sub RenameTabsHandlingNulls()

'Renames all worksheet tabs with each worksheet's cell A5 contents.
'If cell A5 has no content, then the tab is named as "Default"

For i = 1 To Sheets.Count
If Worksheets(i).Range("A5").Value <> "" Then Sheets(i).Name = Worksheets(i).Range("A5").Value
Else: Sheets(i).Name = "Default (" & i & ")"
End If
Next
End Sub


See More: automatically rename tab sheet VBA

Report •


#1
March 23, 2011 at 05:14:06
There are two versions of If. One is a single command on a single line; the other is multiple commands over multiple lines, and allows for Else.
Sub RenameTabsHandlingNulls()
  
  'Renames all worksheet tabs with each worksheet's cell A5 contents.
  'If cell A5 has no content, then the tab is named as "Default"
  
  For i = 1 To Sheets.Count
    If Worksheets(i).Range("A5").Value <> "" Then
      Sheets(i).Name = Worksheets(i).Range("A5").Value
    Else
      Sheets(i).Name = "Default (" & i & ")"
    End If
  Next
End Sub

How To Ask Questions The Smart Way


Report •

#2
March 23, 2011 at 06:11:04
Thanks, it works !!!
Really appreciate your support
Nicolas

Report •

Related Solutions


Ask Question