Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 8
Default vlookup type mismatch error

Hello again,

I'm having an exceptionally persistent and rather annoying vlookup
problem. I use this function a number of times in various macros, and
it seems random whether on any given day it will decide to work or not.
A macro that worked fine yesterday may fail to work today, without me
making any changes to the script or documents involved.

The problem is "runtime error 13: type mismatch". I have checked the
type of both the element in and the element out (avoiding the vlookup
function) and found them to be identical, so I have *no* idea where
this type mismatch is coming from. The example below involves a table
in word simply because that's what I'm working on right now, but I have
encountered this error at some point every single time I have used
vlookup.

Sub tablefun()

Dim oXL As Excel.Application
Set oXL = GetObject(, "Excel.Application")

Dim oXLwb As Excel.Workbook
Set oXLwb = oXL.Workbooks("formtest.xls")

Dim pnum As Variant
pnum = Selection.Cells(1).Range.Text

'testing the type of pnum, it returns as a string
'MsgBox TypeName(pnum)

Dim pname As Variant
Dim wsrange As Range

'testing the type of pname (since if vlookup worked it would find the
value in A1 and return the value in A2)
'pname = oXLwb.Worksheets(1).Range("A1").Value
'Dim pname2 As Variant
'pname2 = oXLwb.Worksheets(1).Range("A2").Value
'MsgBox TypeName(pname)
'MsgBox TypeName(pname2)

Set wsrange = oXLwb.Worksheets(1).Range("A:B")

'here is where the type mismatch is thrown
pname = oXL.VLookup(pnum, wsrange, 2, False)

MsgBox pname
End Sub


As I said above, the type returns as string for each of the values
involved, so I don't understand how vlookup is thinking their types
don't match. I have tried wrapping the entire function as well as pnum
in the function with CStr(), to no avail, as well as defining pnum and
pname as strings rather than variants. I would badly like to get to the
root of this problem, because I am having to deal with it on a daily
basis, and constantly revisit "finished" work when it decides not to
play nice today. I'm also open to a solution that uses a different
method than vlookup if it will be more stable.

I am using excel 2000 on win 2000 if that helps.

Thanks a bunch!

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 6,953
Default vlookup type mismatch error

Are you sure it is on the assignment statement. I would suggest

pname = oXL.VLookup(pnum, wsrange, 2, False)
if iserror(pname) then
msgbox pnum & " was not found"
else
MsgBox pname
end sub

I use the above construct all the time and have never had any problems.

--
Regards,
Tom Ogilvy

"Lilivati" wrote:

Hello again,

I'm having an exceptionally persistent and rather annoying vlookup
problem. I use this function a number of times in various macros, and
it seems random whether on any given day it will decide to work or not.
A macro that worked fine yesterday may fail to work today, without me
making any changes to the script or documents involved.

The problem is "runtime error 13: type mismatch". I have checked the
type of both the element in and the element out (avoiding the vlookup
function) and found them to be identical, so I have *no* idea where
this type mismatch is coming from. The example below involves a table
in word simply because that's what I'm working on right now, but I have
encountered this error at some point every single time I have used
vlookup.

Sub tablefun()

Dim oXL As Excel.Application
Set oXL = GetObject(, "Excel.Application")

Dim oXLwb As Excel.Workbook
Set oXLwb = oXL.Workbooks("formtest.xls")

Dim pnum As Variant
pnum = Selection.Cells(1).Range.Text

'testing the type of pnum, it returns as a string
'MsgBox TypeName(pnum)

Dim pname As Variant
Dim wsrange As Range

'testing the type of pname (since if vlookup worked it would find the
value in A1 and return the value in A2)
'pname = oXLwb.Worksheets(1).Range("A1").Value
'Dim pname2 As Variant
'pname2 = oXLwb.Worksheets(1).Range("A2").Value
'MsgBox TypeName(pname)
'MsgBox TypeName(pname2)

Set wsrange = oXLwb.Worksheets(1).Range("A:B")

'here is where the type mismatch is thrown
pname = oXL.VLookup(pnum, wsrange, 2, False)

MsgBox pname
End Sub


As I said above, the type returns as string for each of the values
involved, so I don't understand how vlookup is thinking their types
don't match. I have tried wrapping the entire function as well as pnum
in the function with CStr(), to no avail, as well as defining pnum and
pname as strings rather than variants. I would badly like to get to the
root of this problem, because I am having to deal with it on a daily
basis, and constantly revisit "finished" work when it decides not to
play nice today. I'm also open to a solution that uses a different
method than vlookup if it will be more stable.

I am using excel 2000 on win 2000 if that helps.

Thanks a bunch!


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default vlookup type mismatch error

Isn't .VLOOKUP a worksheetfunction ?
oXL.WorksheetFunction.VLookup(pnum, wsrange, 2, False)

NickHK

"Lilivati" wrote in message
oups.com...
Hello again,

I'm having an exceptionally persistent and rather annoying vlookup
problem. I use this function a number of times in various macros, and
it seems random whether on any given day it will decide to work or not.
A macro that worked fine yesterday may fail to work today, without me
making any changes to the script or documents involved.

The problem is "runtime error 13: type mismatch". I have checked the
type of both the element in and the element out (avoiding the vlookup
function) and found them to be identical, so I have *no* idea where
this type mismatch is coming from. The example below involves a table
in word simply because that's what I'm working on right now, but I have
encountered this error at some point every single time I have used
vlookup.

Sub tablefun()

Dim oXL As Excel.Application
Set oXL = GetObject(, "Excel.Application")

Dim oXLwb As Excel.Workbook
Set oXLwb = oXL.Workbooks("formtest.xls")

Dim pnum As Variant
pnum = Selection.Cells(1).Range.Text

'testing the type of pnum, it returns as a string
'MsgBox TypeName(pnum)

Dim pname As Variant
Dim wsrange As Range

'testing the type of pname (since if vlookup worked it would find the
value in A1 and return the value in A2)
'pname = oXLwb.Worksheets(1).Range("A1").Value
'Dim pname2 As Variant
'pname2 = oXLwb.Worksheets(1).Range("A2").Value
'MsgBox TypeName(pname)
'MsgBox TypeName(pname2)

Set wsrange = oXLwb.Worksheets(1).Range("A:B")

'here is where the type mismatch is thrown
pname = oXL.VLookup(pnum, wsrange, 2, False)

MsgBox pname
End Sub


As I said above, the type returns as string for each of the values
involved, so I don't understand how vlookup is thinking their types
don't match. I have tried wrapping the entire function as well as pnum
in the function with CStr(), to no avail, as well as defining pnum and
pname as strings rather than variants. I would badly like to get to the
root of this problem, because I am having to deal with it on a daily
basis, and constantly revisit "finished" work when it decides not to
play nice today. I'm also open to a solution that uses a different
method than vlookup if it will be more stable.

I am using excel 2000 on win 2000 if that helps.

Thanks a bunch!



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 48
Default vlookup type mismatch error

Hello,

I'm always confused how to assign Function to cell via VBA.

Can I do it with Application.WorkSheetFunction.Vlookup(...)? Then How to
assign to Cell?

Thanks At advance,
Viesta
Shanghai, CN

"NickHK" wrote:

Isn't .VLOOKUP a worksheetfunction ?
oXL.WorksheetFunction.VLookup(pnum, wsrange, 2, False)

NickHK

"Lilivati" wrote in message
oups.com...
Hello again,

I'm having an exceptionally persistent and rather annoying vlookup
problem. I use this function a number of times in various macros, and
it seems random whether on any given day it will decide to work or not.
A macro that worked fine yesterday may fail to work today, without me
making any changes to the script or documents involved.

The problem is "runtime error 13: type mismatch". I have checked the
type of both the element in and the element out (avoiding the vlookup
function) and found them to be identical, so I have *no* idea where
this type mismatch is coming from. The example below involves a table
in word simply because that's what I'm working on right now, but I have
encountered this error at some point every single time I have used
vlookup.

Sub tablefun()

Dim oXL As Excel.Application
Set oXL = GetObject(, "Excel.Application")

Dim oXLwb As Excel.Workbook
Set oXLwb = oXL.Workbooks("formtest.xls")

Dim pnum As Variant
pnum = Selection.Cells(1).Range.Text

'testing the type of pnum, it returns as a string
'MsgBox TypeName(pnum)

Dim pname As Variant
Dim wsrange As Range

'testing the type of pname (since if vlookup worked it would find the
value in A1 and return the value in A2)
'pname = oXLwb.Worksheets(1).Range("A1").Value
'Dim pname2 As Variant
'pname2 = oXLwb.Worksheets(1).Range("A2").Value
'MsgBox TypeName(pname)
'MsgBox TypeName(pname2)

Set wsrange = oXLwb.Worksheets(1).Range("A:B")

'here is where the type mismatch is thrown
pname = oXL.VLookup(pnum, wsrange, 2, False)

MsgBox pname
End Sub


As I said above, the type returns as string for each of the values
involved, so I don't understand how vlookup is thinking their types
don't match. I have tried wrapping the entire function as well as pnum
in the function with CStr(), to no avail, as well as defining pnum and
pname as strings rather than variants. I would badly like to get to the
root of this problem, because I am having to deal with it on a daily
basis, and constantly revisit "finished" work when it decides not to
play nice today. I'm also open to a solution that uses a different
method than vlookup if it will be more stable.

I am using excel 2000 on win 2000 if that helps.

Thanks a bunch!




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
Conditional Formatting - Run Time Error '13' Type Mismatch Error ksp Excel Programming 0 July 11th 06 07:06 AM
Type mismatch in vlookup carg1[_12_] Excel Programming 5 April 6th 06 11:58 PM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
Befuddled with For Next Loop ------ Run - Time Error '13' Type Mismatch Error rdavis7408 Excel Programming 1 August 25th 04 03:54 AM
Type Mismatch in Vlookup? LJones[_2_] Excel Programming 1 August 5th 04 07:30 AM


All times are GMT +1. The time now is 06:32 PM.

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"