Vba code to sheet name equal cell name

Lacan

Board Regular
Joined
Oct 5, 2016
Messages
158
Office Version
  1. 365
Platform
  1. Windows
Simple Question:

Have in cell B2 the name of my customer that would like that sheet name return the same equal name in B2.

How to do so?

Thanks for the great advice.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Sub test()
ActiveSheet.Name = Range("B2").Value
End Sub
 
Upvote 0
igold

Try a few times and doesnt work.

Can you please confirm again?

Thanks.
 
Upvote 0
If you want the name of the ActiveSheet to equal the contents of cell B2 on that sheet then this one line will do it.

Code:
ActiveSheet.Name = Range("B2").Value

Post what you tried which is not working...

igold
 
Upvote 0
Do you want this to apply to all your sheets buy just running the script once?

If so try this script.

Code:
Sub Sheet_Names()
Application.ScreenUpdating = False
Dim i As Long
    For i = 1 To Sheets.Count
        Sheets(i).Name = Sheets(i).Cells(2, 2).Value
    Next
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Just want to apply to one sheet only.

Try this vba code in different workbooks and nothing.

Sub test()
ActiveSheet.Name = Range("B2").Value
End Sub


What do you suggest?

Thanks.
 
Upvote 0
The script works for me. Are you sure you have some value in Range"B2"?

How did you install the script?
Have you used Vba scripts before?

Try this script. And see what pop ups in the Messagebox

Code:
Sub test()
ActiveSheet.Name = Range("B2").Value
MsgBox Range("B2").Value
End Sub


Just want to apply to one sheet only.

Try this vba code in different workbooks and nothing.

Sub test()
ActiveSheet.Name = Range("B2").Value
End Sub


What do you suggest?

Thanks.
 
Upvote 0
Simple Question:

Have in cell B2 the name of my customer that would like that sheet name return the same equal name in B2.

How to do so?

Thanks for the great advice.

Lacan,

Here is a macro solution for you to consider, that will only rename the ActiveSheet name, if cell B2 is not blank/empty.

Sample Sheet1:


Excel 2007
B
2Customer1
Sheet1


And, after the macro:


Excel 2007
B
2Customer1
Customer1


Sample Sheet2:


Excel 2007
B
2Customer2
Sheet2


And, after the macro:


Excel 2007
B
2Customer2
Customer2


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below code
2. Open your NEW workbook
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys ALT + Q to exit the Editor, and return to Excel
8. To run the macro from Excel press ALT + F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.

Code:
Sub RenameActiveSheet()
' hiker95, 12/28/2016, ME982790
With ActiveSheet
  If Not .Range("B2") = vbEmpty Then
    .Name = .Range("B2").Value
  End If
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm, and, answer the "do you want to enable macros" question as "yes" or "OK" (depending on the button label for your version of Excel) the next time you open your workbook.


Then run the RenameActiveSheet macro.


The macro should work correctly, but, there are some characters that can not be used in worksheet names.
 
Upvote 0
Dear igold and Dear hiker95,

Congratulations!!!
Both of you were right.

Both of vba codes work correct only if you first take the steps (1-8) that hiker95 wrote before.

However my last question: - is theres any way to do it automatically instead of everytime I rename B2 have to do Run Macro Dialog?

Thanks again gentlemen.
 
Upvote 0
Dear igold and Dear hiker95,

Congratulations!!!
Both of you were right.

Both of vba codes work correct only if you first take the steps (1-8) that hiker95 wrote before.

Thanks again gentlemen.

Lacan,

Thanks for the feedback.

You are very welcome. Glad we could help.
 
Upvote 0

Forum statistics

Threads
1,213,510
Messages
6,114,040
Members
448,543
Latest member
MartinLarkin

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