VBA Dates

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
Ok, I am trying to create a custom variable with data pulled from other variables but I can't get excel to accept it. I keep getting a type mismatch but can't figure out how to get past it

Code:
Public StartDate As Date
StartDate = "11/1/2010"

Dim Month As String
Month = DatePart("M", StartDate)

Dim Year As String
Year = DatePart("YYYY", StartDate)

Dim MonthYear As Date
MonthYear = Month & "/" & "*" & "/" & Year
Can anyone tell me how to combine the found month and year into the new variable? Is there any easier way to get the date without a value in the day field?
 

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

JP2112

Board Regular
Joined
Oct 27, 2008
Messages
237
What you've done is create a String that looks like this:

"11/*/2010"

Since this doesn't represent a valid date, you cannot assign this String to a Date-type variable.
 

mmetzinger

Board Regular
Joined
Dec 30, 2010
Messages
61
I figured that much I just need to figure out how to generate a date that has no day value because I need to be able to search a sheet for the last row that has data for that month.
 

shg

MrExcel MVP
Joined
May 7, 2008
Messages
21,763
Office Version
2010
Platform
Windows
If you set the date to the last day of the month, then lookup functions would find the largest value <= that date, if the data is sorted ascending by date.
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,953
Office Version
365, 2010
Platform
Windows

ADVERTISEMENT

Why do you need VBA? This can be done with a formula:

With desired month in H1 and desired year in I1:

Excel Workbook
ABCDEFGHI
1112010
211/1/200912
311/3/20091530
411/4/201027
511/16/201030
612/1/2010
Sheet1
 

jasonb75

Well-known Member
Joined
Dec 30, 2008
Messages
10,684
Office Version
2019
Platform
Windows
using your variables, something like

Code:
monthyear = DateSerial(Year(startdate), Month(startdate) + 1, 0)
 

Scott Huish

MrExcel MVP
Joined
Mar 17, 2004
Messages
19,953
Office Version
365, 2010
Platform
Windows
Using those variables, Startdate should be entered as a date literal not a string:
Code:
StartDate = #11/1/2010#
 

Subscribe on YouTube

Watch MrExcel Video

Forum statistics

Threads
1,106,550
Messages
5,512,010
Members
408,872
Latest member
Lorid24

This Week's Hot Topics

  • Sort code advice please
    Hi, I have the code below which im trying to edit but getting a little stuck. This was the original code which worked fine,columns A-F would sort...
  • SUMPRODUCT with nested If statement
    Hi everyone, Hope you're all well. I'm hoping someone will be able to point me in the right direction with a problem I'm having with a SUMPRODUCT...
  • VBA - simple sort is killing me!
    Hello all! This should be so easy, but not for me, apparently! I have a table of data that can be of varying lengths and widths. My current macro...
  • Compare Two Lists
    I have two Lists and I need to be able to Identify differences between them. List 100 comes from a workbook - the other is downloaded form the...
  • Formula that deducts points for each code I input.
    I am trying to create a formula that will have each student in my class start at 100 points and then for each code that I enter (PP for Poor...
  • Conditional formatting formula required for day of week and a value
    Hi, I have a really simple spreadsheet where column A is the date, column B is the activity total shown as a number and column C states the day of...
Top