VBA HLOOKUP - Error- why.. oh why..

Sinbad

Board Regular
Joined
Apr 18, 2012
Messages
224
Hi all,

I have 2 questions...

1. Don´t quite understand why I get an error when using this VBA code. It does not like me using curdate in the Hlookup statement
Code:
Sub ph()

'Dimensioning
Dim curdate As String
Dim ph As Variant

'Set values
curdate = Range("B34")

'work things out
MsgBox WorksheetFunction.HLookup(curdate, ActiveSheet.Range("AK2:AW2"), 1, False)
End Sub

when I change the code to this (replace curdate)
Code:
Sub ph()

'Dimensioning
Dim curdate As String
Dim ph As Variant

'work things out
MsgBox WorksheetFunction.HLookup(Range("B34"), ActiveSheet.Range("AK2:AW2"), 1, False)
End Sub

it works just fine ...


in B34 is a date eg. 01.01.2018 and in the Range AK2:AW2 are various dates in the same format.
I changed the


2. in the working code, it returns 43101 ?? where does that value come from ? I tried with 01.01.2018 in cell B34 and it is also in the search range. I would like it to simply return a 1 if found and a 0 if not found.

Thank you as always.
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
43101 is the number of days from 1/1/1900. each day is a whole number
 
Upvote 0
43101 is the date code for 1/1/2018. Format it as a date and it will show up correctly.
Are your dates real dates? if yes why are you declaring them as strings?
 
Last edited:
Upvote 0
yup, they are real dates, I did declare them as Date but makes no real differance that I can notice. Same behaviour.
 
Last edited:
Upvote 0
Code:
Sub ph()

'Dimensioning
Dim curdate As Long
'Set values
curdate = Range("B34")

'work things out
MsgBox Format(WorksheetFunction.HLookup(curdate, ActiveSheet.Range("AK2:AW2"), 1, False), "dd/mm/yyyy")
End Sub

Change "dd/mm/yyyy" to "mm/dd/yyyy" if you use American dates.
 
Last edited:
Upvote 0
Thanks Mark858, that is an interesting take, did not know you could combine it in that way.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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