Sheet naming using String Variable

KWilkinson

New Member
Joined
Dec 9, 2016
Messages
11
Hi all,

I'm fairly new to VBA and trying to write some basic macros to reduce some drudgery when working with a few of our standard spreadsheets. As part of this I'm trying to create a new sheet which needs to be named based on data in 2 separate cells.

The code is pretty basis but I can see what I'm doing wrong when it comes to naming the sheet - can anyone assist and point me in the right direction?

Sub CreateNewSheet()
Dim Sheetname As String
Dim ws As Worksheet
Sheeetname = Worksheets("Proposed").Cells(2, "A") & "-" & Cells(2, "B").Value


Set ws = ThisWorkbook.Sheets.Add(After:= _
ThisWorkbook.Sheets("Proposed"))
ws.Name = Sheetname


End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Check your spelling of sheetname
 
Upvote 0
Thanks, I still get a runtime error '1004' Method 'Name' of object'_Worksheet' failed...

I'm also finding when debugging the script that the Sheetname value works sometimes but not others - in some instances it's not picking up the value in cell B2...
 
Upvote 0
Okay - first issue is because I've called the variable Sheetname - If I change this to x it works fine.

The concatenation though isn't working reliably - can anyone tell me what is wrong with this or perhaps a better way of doing it?
 
Upvote 0
This:

Code:
Cells(2, "B").Value

is essentially

Code:
Activesheet.Cells(2, "B").Value

not

Code:
Worksheets("Proposed").Cells(2, "B").Value

So it depends what is the activesheet at the time the line executes.
 
Upvote 0
Thanks for the reply. The issue I'm having is that this line isn't concatenating correctly.

In Cell A2 I have 2017-08-04 and in B2 I have P01 the result I'm after is 2017-08-04-P01 what I'm getting is 2017-08-04-

Sometimes it seems to work and others it doesn't... at the moment it very much doesn't.. :eek:(
 
Upvote 0
Then you need to qualify your range as i pointed out. You cant use:

Code:
Cells(2, "B").Value

You need to use

Code:
Sheets("Proposed").Cells(2, "B").Value

or more fully:

Code:
Sheetname = Sheets("Proposed").Cells(2, "A") & "-" & Sheets("Proposed").Cells(2, "B").Value
 
Upvote 0
Okay, i understand now what you are saying. I need to set my active worksheet first then run the concatenation code.

Code:
Sub CreateNewSheet()
Dim x As String
Dim ws As Worksheet
    
    Set ws = ThisWorkbook.Sheets.Add(After:= _
             ThisWorkbook.Sheets("Proposed"))
    Worksheets("Proposed").Activate
    x = Cells(2, "A") & "-" & Cells(2, "B").Value
    ws.Name = x
    
End Sub

This code now works as expected. Thanks everyone for your time, much appreciated.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

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