Vba date conversion not working

drluke

Active Member
Joined
Apr 17, 2014
Messages
314
Office Version
  1. 365
Platform
  1. Windows
I am using vlookup to return a date from my spreadsheet. I have tried to convert the date to Long by using CLng & CDate in my code, but I am still getting a type mismatch error.

Here is my code:

Code:
Private Sub TextBox9_Change()
Dim wsJournal As Worksheet
Dim MyDate As Long

    Set wsJournal = Worksheets("Journal")
    MyDate = CLng(CDate(Me.tbDate.Value))
    
    MyDate = Application.VLookup(Me.tbPeriod, [YrDte], 1, False)
    
End Sub

Any advice would be appreciated.
 

Excel Facts

Select a hidden cell
Somehide hide payroll data in column G? Press F5. Type G1. Enter. Look in formula bar while you arrow down through G.
Which line is causeing that error
The Vlookup might not be returning a Long data type, it might be Double or Error or String.
 
Upvote 0
Debugger highlights this row: MyDate = Clng .........
 
Upvote 0
What happens if you change
Code:
Dim MyDate As Long
to
Code:
Dim MyDate As Date
 
Upvote 0
I still get the Type mismatch error, but when debugging MyDate = 00:00:00 and Me.tbDate.Value or Me.tbDate.Text = ""
 
Upvote 0
That looks more like a time than a date, try dimming as double.
 
Upvote 0
I still get the Type mismatch error, but when debugging MyDate = 00:00:00 and Me.tbDate.Value or Me.tbDate.Text = ""

Why Me.tbDate.Value is empty? Is Me.tbDate actually has value but the code somehow can't get the value?
One possibility for the Type mismatch error is it Me.to Date.Value is not a date type or can't be treated as date type.
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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