Rename sheet by Cell value

mayaa_mmm

Board Regular
Joined
Jul 30, 2014
Messages
54
Office Version
  1. 2010
Platform
  1. Windows
In this code, if the cell value is having some special characters like "/" etc, it showing error.
I need to exclude that error and remove the special character in the particular cell and rename as it is.

Example : B1 cell is having " ABC/ DCF"

While renaming the sheet, it should come as " ABC DCF"


Sub RenameFromA1()
Dim Msg As String, i As Integer
For i = 2 To Sheets.Count
If Sheets(i).Range("B1").Value = "" Then
Msg = "Sheet " & i & "(" & Sheets(i).Name & ") has no value in A1. Fix sheet, then rerun."
MsgBox Msg, vbExclamation
Exit Sub
Else
On Error GoTo ErrSheetName
Sheets(i).Name = Sheets(i).Range("B1").Value
On Error GoTo 0
End If
Next i
Exit Sub
ErrSheetName: Msg = "Sheet " & i & "(" & Sheets(i).Name & ") could not be renamed. Check if name already used."
MsgBox Msg, vbExclamation
End Sub


Thank you in advance
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I use these 2 functions that I wrote:

Code:
Function FixFileName(FileNamePiece As String) As String
Dim fname As String


fname = Trim(FileNamePiece)


fname = Replace(fname, " ", "_")
fname = Replace(fname, ",", "")
fname = Replace(fname, "'", "")
fname = Replace(fname, "(", "")
fname = Replace(fname, ")", "")
fname = Replace(fname, "~", "")
fname = Replace(fname, "*", "")
fname = Replace(fname, "?", "")
fname = Replace(fname, "/", "")
fname = Replace(fname, "\", "")
fname = Replace(fname, """", "")


FixFileName = fname


End Function
Function cleanSheetName(sheetName As String) As String


sheetName = Replace(sheetName, ":", "-")
sheetName = Replace(sheetName, "/", "-")
sheetName = Replace(sheetName, "\", "-")
sheetName = Replace(sheetName, "*", "-")
sheetName = Replace(sheetName, "?", "-")
sheetName = Replace(sheetName, "[", "-")
sheetName = Replace(sheetName, "]", "-")


cleanSheetName = sheetName
End Function
 
Upvote 0

Forum statistics

Threads
1,214,912
Messages
6,122,204
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top