?
Solved

VBA Date code returning error

Posted on 2014-01-10
14
Medium Priority
?
588 Views
Last Modified: 2014-01-16
I have the following code.  My workbook has multiple worksheets with dates as the names.  I am getting an error when testing this code in the immediate window to get the most recent date.  I can't figure out what is wrong.  Please help!

Function getMostRecentDate()
    Dim wsSheet As Worksheet
    Dim datMostRecent As Date
    Dim datTestDate As Date
   
    datMostRecent = 0
    For Each wsSheet In Worksheets
        datTestDate = CDate(wsSheet.Name)
        If datTestDate > datMostRecent Then
        datMostRecent = datTestDate
        End If
    Next wsSheet
    getMostRecentDate = datMostRecent
   
End Function
0
Comment
Question by:karaflanagan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
14 Comments
 

Author Comment

by:karaflanagan
ID: 39772068
I am getting a Run-time error '13'
Type mismatch
0
 
LVL 10

Expert Comment

by:Anthony Berenguel
ID: 39772091
What is the date format you're using in your worksheet names? Remember that value (worksheet.name) is a string so you need to keep that in mind when you use that value to populate a date variable.
0
 

Author Comment

by:karaflanagan
ID: 39772098
23-Aug-13
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 81

Expert Comment

by:byundt
ID: 39772112
I assume that the error occurs on the CDate statement. If so, what is the value of wsSheet.Name?

Just for grins, I tested CDate("2-29-14") in the Immediate pane--and got a Type 13 Type Mismatch error. But DateSerial(2014, 2, 29) returned March 1, 2014 (as it should in a non-Leap Year).
0
 
LVL 81

Expert Comment

by:byundt
ID: 39772115
?CDate("23-Aug-13") is working in Excel 2013 (32-bit) for me in the Immediate pane.

Which version of Excel are you using?
0
 

Author Comment

by:karaflanagan
ID: 39772124
Excel 2010 not sure if it is 32 bit or 64 bit
0
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 39772133
You're declaring
Dim datMostRecent As Date
and then Assign the value of 0 here  datMostRecent = 0 ??
0
 
LVL 81

Expert Comment

by:byundt
ID: 39772143
I also have 32-bit Excel 2010, and ?CDate("23-Aug-13") is working for me there, as well.

The statement flagged by Jesus Rodriguez is working fine in that test workbook.
0
 

Author Comment

by:karaflanagan
ID: 39772316
byundt, I am trying to actually run the GetMostRecentDate Function and have it return a value, not CDate.
0
 

Author Comment

by:karaflanagan
ID: 39772320
Jesus - Excel reads dates as serial numbers.
0
 
LVL 81

Expert Comment

by:byundt
ID: 39772326
Could you post a workbook that demonstrates the problem? It doesn't need to have any data in the worksheets, and it doesn't need to include any code other than the function being discussed.
0
 
LVL 81

Expert Comment

by:byundt
ID: 39772363
If one of the worksheet names is not a date, then CDate will fail. A worksheet formula referencing your function will return #VALUE! error, and the Immediate pane will return a Type 13.

Should this be a concern, you can add "On Error Resume Next" to handle the possibility of worksheet names that aren't dates.
Function getMostRecentDate()
    Dim wsSheet As Worksheet
    Dim datMostRecent As Date
    Dim datTestDate As Date
    
    On Error Resume Next
    datMostRecent = 0
    For Each wsSheet In Worksheets
        datTestDate = CDate(wsSheet.Name)
        If datTestDate > datMostRecent Then
            datMostRecent = datTestDate
        End If
    Next wsSheet
    On Error GoTo 0
    getMostRecentDate = datMostRecent
    
End Function

Open in new window

0
 

Author Comment

by:karaflanagan
ID: 39772389
Here is an example.  I am not done with the project yet but I don't want to continue with these functions still not working.
Test-Workbook.xlsm
0
 
LVL 81

Accepted Solution

by:
byundt earned 2000 total points
ID: 39772680
You have a worksheet named Reports. This will cause a run-time error with CDate.  As a workaround, I added a test for the worksheet name beginning with a number.

You also put the functions in the code pane for a userform. I moved them to a regular module sheet (which I named modFunctions).

The function getEarliestDate referred to an undeclared variable datMostRecent. I changed that reference to datEarliest.
Function getMostRecentDate()
    Dim wsSheet As Worksheet
    Dim datMostRecent As Date
    Dim datTestDate As Date
    
    datMostRecent = 0
    For Each wsSheet In Worksheets
        If Val(wsSheet.Name) > 0 Then
            datTestDate = CDate(wsSheet.Name)
            If datTestDate > datMostRecent Then
                datMostRecent = datTestDate
            End If
        End If
    Next wsSheet
    getMostRecentDate = datMostRecent
    
End Function
Function getEarliestDate()
    Dim wsSheet As Worksheet
    Dim datEarliest As Date
    Dim datTestDate As Date
    
    datEarliest = 99999
    For Each wsSheet In Worksheets
        If Val(wsSheet.Name) > 0 Then
            datTestDate = CDate(wsSheet.Name)
            If datTestDate < datEarliest Then
                datEarliest = datTestDate
            End If
        End If
    Next wsSheet
    getEarliestDate = datEarliest
    
End Function

Open in new window

Test-WorkbookQ28335626.xlsm
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

How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
This article describes a serious pitfall that can happen when deleting shapes using VBA.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

762 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