Solved

How can I return the weekday name from a date in VBA ?

Posted on 2014-03-14
5
1,591 Views
Last Modified: 2014-03-15
Hi,

I'm trying to return the name of the day of the week from a date but I keep getting a compilation error on the "ddddd" part of the statement...

Dim lWeekday As Long
Dim strWeekdayText As Long

lWeekday = WorksheetFunction.Weekday(("teTrainingDate" & Idx))
strWeekdayText = WorksheetFunction.Text(lWeekday, “ddddd”)

Any ideas ?

Thanks
Toco
0
Comment
Question by:Tocogroup
  • 3
  • 2
5 Comments
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39929825
Hi,

the function Text in this case waits for a date
I do not see in your code that
"dddd" 4d only for the date in letters
You can use format as well

strWeekdayText = Format(yourDate, “dddd”)

Open in new window


EDIT I do not understand you frist line of code

How do you want to find a weekday with a "teTrainingDate" & Idx, it's no date

Regards
0
 

Author Comment

by:Tocogroup
ID: 39929884
Hi,

I'm getting a Run-time error 13 Type mismatch

Does the date have to be in a particular date format ?

strWeekdayText = WorksheetFunction.Text("teTrainingDate" & Idx, "ddddd")

My date is being held as dd-mmm-yyyy
0
 
LVL 49

Expert Comment

by:Rgonzo1971
ID: 39929896
Hi,

what is the value of
"teTrainingDate" & Idx
0
 
LVL 49

Accepted Solution

by:
Rgonzo1971 earned 500 total points
ID: 39930024
Hi,

You cannot refer to a variable via a string like in your code

pls try (add cases for your different dates indexes if necessary)

Dim lWeekday As Long
Dim strWeekdayText As String 'Edited
Dim strDate As String

Select Case Idx
    Case 1
        strDate = teTrainingDate1
    Case 2
        strDate = teTrainingDate2
    Case 3
        strDate = teTrainingDate3
    Case 4
        strDate = teTrainingDate4
        
    ' And so on
End Select
strWeekdayText = Format(strDate, "dddd")

Open in new window

Regards
0
 

Author Closing Comment

by:Tocogroup
ID: 39931137
Thanks for that
Toco
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question