Vba code to sheet name equal cell name

Lacan

Board Regular
Joined
Oct 5, 2016
Messages
85
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.
 

Some videos you may like

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,506
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Sub test()
ActiveSheet.Name = Range("B2").Value
End Sub
 

Lacan

Board Regular
Joined
Oct 5, 2016
Messages
85
Office Version
  1. 365
Platform
  1. Windows
igold

Try a few times and doesnt work.

Can you please confirm again?

Thanks.
 

igold

Well-known Member
Joined
Jul 8, 2014
Messages
2,506
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
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
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,254
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

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
 

Lacan

Board Regular
Joined
Oct 5, 2016
Messages
85
Office Version
  1. 365
Platform
  1. Windows
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.
 

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
17,254
Office Version
  1. 2013
Platform
  1. Windows

ADVERTISEMENT

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.
 

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
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:

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>B</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">Customer1</td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Sheet1</p><br /><br />

And, after the macro:

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>B</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">Customer1</td></tr></tbody></table><p style="width:5.4em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Customer1</p><br /><br />

Sample Sheet2:

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>B</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">Customer2</td></tr></tbody></table><p style="width:3.6em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Sheet2</p><br /><br />

And, after the macro:

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>B</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">Customer2</td></tr></tbody></table><p style="width:5.4em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Customer2</p><br /><br />

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.
 

Lacan

Board Regular
Joined
Oct 5, 2016
Messages
85
Office Version
  1. 365
Platform
  1. Windows
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.
 

hiker95

Well-known Member
Joined
Apr 8, 2009
Messages
17,649
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,970
Messages
5,599,108
Members
414,288
Latest member
horizon2

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
Top