VBA Set Sheet name with prefix text, user input, suffix text and cell value

vortensis

New Member
Joined
Apr 16, 2021
Messages
7
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Greetings!

I have a question, I've been using some simple code to set the Sheetname as cell value for some time (incidentally I think its from MrExcel too), I want to adjust it, but really haven't the slightest on how...
Current;
VBA Code:
Sub SetTabName()
Dim ws As Worksheet
For Each ws In Worksheets
ws.Activate
ws.Name = Range("A2").Value
Next
End Sub

What I would like to achieve is hitting the button, it runs through and does its thing, six unrelated bits and bobs and then sets the name... I'd like a little more detail by grabbing the contents of cell A2, and prompting for the last bit which is not in the spreadsheet...

The sheetname should end up looking like;
"Ticket-##### - Device 123456"

123456 is Cell A2 value
##### being a user prompt for a number in 5 to 6 digits

Can any Excel wizards assist in this?
Thanks,
vortensis
 

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
Should the ##### be the same for each sheet?
 
Upvote 0
VBA Code:
Dim strInput as String

strInput = InputBox("Prompt_here","Title_here")

ws.Name = "Ticket-" & strInput & " - Device " & Range("A2").Value

Might not work for your case exactly, but something like this just to give you an idea.

There is a 32 characters limit for sheet names iirc.
 
Upvote 0
Should the ##### be the same for each sheet?
Thanks for the reply Fluff!

For this particular dataset there is only ever one sheet.
At the moment there isnt a need to have it target multiple sheets...
And I have managed to adapt a number of other macro's to run through multiple sheets, but not sure that will ever be needed...
 
Upvote 0
If there is only one sheet, why are you looping through the worksheets?
 
Upvote 0
As there is just one sheet you can replace your code with
VBA Code:
ActiveSheet.Name = "Ticket-" & InputBox("please enter a ticket") & " - Device " & Range("A2").Value
 
Upvote 0
Solution
If there is only one sheet, why are you looping through the worksheets?
That's probably just a holdover from the code I borrowed, it worked fine as it was so didn't want to mess with it... :)
 
Upvote 0
As there is just one sheet you can replace your code with
VBA Code:
ActiveSheet.Name = "Ticket-" & InputBox("please enter a ticket") & " - Device " & Range("A2").Value
Fantastic, so simple... I may have been trying to over think the problem :)

Marked as solution, thanks again :D
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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