type mismatch

E

ExcelQuestion123

I'm having an issues with this simple code and can't figure it out. I
always get the type mismatch error. I do have a range named
StatusTable, so that's not the problem. Please help.

Sub msg()
Dim i, j, k As Integer
i = 1
j = 3

If Application.VLookup(Worksheets(i).Range("A" & j) &
Worksheets(i).Range("D" & j), Range("StatusTable"), 2, False) = "Paid"
Then
MsgBox ("Correct")
Else: MsgBox ("Error")
End If

End Sub
 
W

ward376

One thing:
Dim i, j, k As Integer

You have to specify each dimensioned variable as a data type -

dim i as integer, j as long, k as integer

Which line generates the error?

Cliff Edwards
 
W

ward376

I'm having an issues with this simple code and can't figure it out.  I
always get the type mismatch error.  I do have a range named
StatusTable, so that's not the problem.  Please help.

Sub msg()
Dim i, j, k As Integer
i = 1
j = 3

If Application.VLookup(Worksheets(i).Range("A" & j) &
Worksheets(i).Range("D" & j), Range("StatusTable"), 2, False) = "Paid"
Then
    MsgBox ("Correct")
    Else: MsgBox ("Error")
End If

End Sub
 
J

Joel

I have excel 2003 and you would need this change

from
Application.VLookup(......
to
WorksheetFunction.VLookup(.....
 
W

ward376

OK theres really only one line - try using

(sheets(i).Range("A" & j)

instead of

(Worksheets(i).Range("A" & j)

Cliff Edwards
 
E

ExcelQuestion123

I have excel 2007 and the WorksheetFunction doesn't seem to work on
that.

The Worksheets --> sheets didn't change anything

I want both i and j to be integers and in excel 2007 I've always been
able to put them together like that.

Any other ideas?
 
W

ward376

You can put them together like that - but i and j are dimmed as
variants, not integers.

j should be dimmed long because you're using it for rows and row
numbers can exceed the parameters of the integer data type. (-32,768
to 32,767)

Try it and let us know what happens.

Cliff Edwards
 
E

ExcelQuestion123

I tried that; here is my code now:

Sub msg()
Dim i As Integer
Dim j As Long

i = 1
j = 3

If Application.VLookup(Worksheets(i).Range("A" & j) &
Worksheets(i).Range("D" & j), Range("StatusTable"), 2, False) = "Paid"
Then
MsgBox ("Correct")
Else: MsgBox ("Error")
End If

End Sub

and the error message i currently get is: "Method 'Range' of object
'_Global' failed."

Any other suggestions?
 
E

ExcelQuestion123

Nevermind on the last error message. I'm still getting the type
mismatch though. Could it have something to do with the data type/
format?
 
D

Dave Peterson

I'd use:

Option Explicit
Sub msg()
Dim i as long
dim j as long
dim k As long
dim res as variant
i = 1
j = 3

with worksheets(i)
res = Application.VLookup(Worksheets(i).Range("A" & j).value _
& Worksheets(i).Range("D" & j).value, _
.Range("StatusTable"), 2, False)

if iserror(res) then
msgbox "No match" 'or "Error" '????
elseif lcase(res) = lcase("Paid") then
msgbox "Correct"
else
msgbox "Error"
end if
End with

End Sub

(Untested, but it did compile.)

I put the statustable range on worksheets(i). If that's incorrect, then qualify
that range accordingly:

res = Application.VLookup(Worksheets(i).Range("A" & j).value _
& Worksheets(i).Range("D" & j).value, _
worksheets("somesheetnamehere").Range("StatusTable"), 2, False)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top