formatting name in new worskeet VBA

GedSalter

Board Regular
Joined
Apr 24, 2019
Messages
80
I have a macro which doesn all that I need except with the new worksheet name. the name is relevant to a figure in the cell E9. The number in E9 is formatted as a four digit number so if 21 was typed in the cell reads 0021. Unfortunately when the new worksheet is created using this number it only creates the sheet as 21.

I require the sheet to be called 0021 in the tab. The numbers will change for each sheet created as Cell E9 value changes.

This is the current macro I have

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]Sub AddSheet()
Dim ws As Worksheet
Dim wh As Worksheet
Set ws = Worksheets(ActiveSheet.Name)[/FONT]

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
ActiveSheet.Copy After:=Worksheets(Sheets.Count)
Set wh = Worksheets(Sheets.Count)
If ws.Range("e9").Value <> "" Then
wh.Name = ws.Range("E9").Value
ActiveSheet.Protect
End If
[/FONT]

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif] strPath = ActiveWorkbook.Path & "\Invoices"

ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=strPath & Range("E9"), _
Quality:=xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False[/FONT]

[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]
End Sub[/FONT]
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
try

Code:
wh.Name = Format(ws.Range("E9").Value,"0000")
 
Upvote 0
Hi,

Use the Range.Text property to return what you see in the cell & not its underlying value

change this line

Code:
wh.Name = ws.Range("E9").Value

to this


Code:
wh.Name = CStr(ws.Range("E9").Text)

and see if does what you want.

Dave
 
Upvote 0

Forum statistics

Threads
1,224,737
Messages
6,180,662
Members
452,992
Latest member
TokugawaIesuma

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