Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default VLookup function in VBA problem?

It is not possible to use a Date datatype as the VLookup value because
the function doesn't know what to do with a Date data type. XL knows
only three data types: number, text, and boolean. It implements a date
as a number. It is only through formatting (which, for the most part,
has no effect on the type of data, only on how it is displayed) that
distinguishes between a number and a date.

So, the following works:

Sub testIt2()
Dim retireDate As Date, insurance As Long
retireDate = Range("Retire_Date_Primary")
insurance = -18
MsgBox Application.VLookup(Range("Retire_Date_Primary"), _
Range("Pension_Table"), 21 + insurance - 1)
MsgBox Application.VLookup(CDbl(retireDate), _
Range("Pension_Table"), 21 + insurance - 1)
End Sub


--
Regards,

Tushar Mehta
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
I am perplexed by a data type problem that occurs when I use a date to VLookup on a table. If I use a date directly from a spreadsheet via Range("Retire_Date_Primary") the lookup performs perfectly. However if I declare a variable As Date and the assign this worksheet range to this variable, the lookup fails with a type mismatch.

Dim retireDate As Date
retireDate = Range("Retire_Date_Primary")
Application.VLookup(Range("Retire_Date_Primary"), Range("Pension_Table"), 21 + insurance - 1) '<= This works
Application.VLookup(retireDate , Range("Pension_Table"), 21 + insurance - 1) '<= This fails


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 126
Default VLookup function in VBA problem?

You are welcome. Glad to be of help.

--
Regards,

Tushar Mehta
www.tushar-mehta.com
Multi-disciplinary business expertise
+ Technology skills
= Optimal solution to your business problem
Recipient Microsoft MVP award 2000-2004

In article ,
says...
Tushar,

Thank you for the solution (and an excellent explaination).

Interesting to note that if I do the CDbl conversion in the assignment statement instead of the VLookup function call it still returns a type conversion. The conversion has to be done in the function call.

Thanks again.

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with VLOOKUP function vsoler Excel Worksheet Functions 0 September 12th 09 04:06 PM
Problem with VLookUp Function Jelinek Excel Worksheet Functions 1 April 25th 06 03:04 PM
Vlookup Function Problem Parker Excel Worksheet Functions 3 January 13th 05 06:53 PM
VLookup function in VBA problem? JWolf Excel Programming 4 May 15th 04 04:18 PM
VLookup function in VBA problem? Bill Buckner Excel Programming 0 May 11th 04 07:41 PM


All times are GMT +1. The time now is 10:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"