Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

defining name worksheet variable

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » Defining the name of a worksheet as a variable

Defining the name of a worksheet as a variable

ResolvedVersion 365

Toby has attended:
Excel VBA Introduction course

Defining the name of a worksheet as a variable

Hello!

I have worksheets named as strings "DD MM YYYY" which I am trying to reformat and so that I can update a cell with the name from the last sheet in the workbook, in the format DD/MM/YYYY

Using variables is new to me, so I am sure it is something to do with my variable declaration.

The issue seems to be with my LSN variable where I want it to take the name of the last worksheet in my file.

Currently my code is as follows:

Sub UpdateEmailSummary()
'
' UpdateEmailSummary Macro
'
Dim LSN As String
Dim LF As Integer
Dim LM As Integer
Dim LR As Integer

LSN = Sheets(Sheet.Count).Name
LF = Left(LSN, 2)
LM = Mid(LSN, 4, 2)
LR = Right(LSN, 4)

Sheets("EMAIL SUMMARY").Select
Range("C3").Select
ActiveCell.FormulaR1C1 = LF & "/" & LM & "/" & LR

End Sub

Many thanks,
Toby

RE: Defining the name of a worksheet as a variable

Hi Toby

You are so close!!!

LSN = Sheets(Sheets.Count).Name

you missed the 's' off the Sheets.Count which causes the Object Required error


Additionally, you could have used the DATESERIAL function to construct the date from the individual parts (year, month, day)

ActiveCell.FormulaR1C1 = DateSerial(LR, LM, LF)

Hope this helps.

Dennis

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Create a hyperlink navigation sheet

In large files, it is often useful to have a front sheet with hyperlinks to the key databases and summary calculations in your spreadsheet. Hyperlinks can save you and (more importantly) those less familiar with your spreadsheet a great deal of pointless scrolling between and within sheets.



Hyperlinks appear as underlined text and can jump to any cell or range name in your file. You can also use hyperlinks to jump to other files.



To create a hyperlink to a location in the active workbook: (1) Select the cell that contains the text you want to use as the hyperlink and choose Insert|Hyperlink.(2)Click Place in this document.(3)Choose the sheet you want to link to or the range name from the list of "Defined Names".(4)If necessary, type the cell reference in the Type in the cell reference box. (5) Click OK.

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.11 secs.