VBA lookup outputs incompatible types
I'm trying to do a inversed vlookup, from bottom to top, using lookup, it works fine in the excel tab but I'm having a few issues translating it to VBA.
Excel formula: =LOOKUP(2;1/(Table1[Code]=L4);Table1[Key])
VB:
Set sht2 = Worksheets("Base")
Set sht3 = Worksheets("Extra")
rng1 = sht2.ListObjects("Table1").ListColumns(1).DataBodyRange.Value
rng2 = sht2.ListObjects("Table1").ListColumns(2).DataBodyRange.Value
cll = sht3.Cells(2, 12)
rw = Application.WorksheetFunction.Lookup(2, 1 / (rng1 = cll), rng2)
sht3.Cells(2,13) = rw
Any tips?
excel vba excel-vba
|
show 1 more comment
I'm trying to do a inversed vlookup, from bottom to top, using lookup, it works fine in the excel tab but I'm having a few issues translating it to VBA.
Excel formula: =LOOKUP(2;1/(Table1[Code]=L4);Table1[Key])
VB:
Set sht2 = Worksheets("Base")
Set sht3 = Worksheets("Extra")
rng1 = sht2.ListObjects("Table1").ListColumns(1).DataBodyRange.Value
rng2 = sht2.ListObjects("Table1").ListColumns(2).DataBodyRange.Value
cll = sht3.Cells(2, 12)
rw = Application.WorksheetFunction.Lookup(2, 1 / (rng1 = cll), rng2)
sht3.Cells(2,13) = rw
Any tips?
excel vba excel-vba
1
You can't compare or divide arrays like that in VBA. UseEvaluate
instead, passing the same formula string you would use in a cell.
– Rory
Nov 13 '18 at 14:31
Or use Find searching up to find the last cell that has that value and use offset to return the correct value.
– Scott Craner
Nov 13 '18 at 14:33
Evaluate doesn't work for me, I need the result of the row in VBA.
– Gabriel_Koch
Nov 13 '18 at 14:43
I tried using Find, but couldn't get it quite right
– Gabriel_Koch
Nov 13 '18 at 14:44
Evaluate
will return exactly the same result as the formula you mention, so it's hard to see how it wouldn't work for you.
– Rory
Nov 13 '18 at 15:02
|
show 1 more comment
I'm trying to do a inversed vlookup, from bottom to top, using lookup, it works fine in the excel tab but I'm having a few issues translating it to VBA.
Excel formula: =LOOKUP(2;1/(Table1[Code]=L4);Table1[Key])
VB:
Set sht2 = Worksheets("Base")
Set sht3 = Worksheets("Extra")
rng1 = sht2.ListObjects("Table1").ListColumns(1).DataBodyRange.Value
rng2 = sht2.ListObjects("Table1").ListColumns(2).DataBodyRange.Value
cll = sht3.Cells(2, 12)
rw = Application.WorksheetFunction.Lookup(2, 1 / (rng1 = cll), rng2)
sht3.Cells(2,13) = rw
Any tips?
excel vba excel-vba
I'm trying to do a inversed vlookup, from bottom to top, using lookup, it works fine in the excel tab but I'm having a few issues translating it to VBA.
Excel formula: =LOOKUP(2;1/(Table1[Code]=L4);Table1[Key])
VB:
Set sht2 = Worksheets("Base")
Set sht3 = Worksheets("Extra")
rng1 = sht2.ListObjects("Table1").ListColumns(1).DataBodyRange.Value
rng2 = sht2.ListObjects("Table1").ListColumns(2).DataBodyRange.Value
cll = sht3.Cells(2, 12)
rw = Application.WorksheetFunction.Lookup(2, 1 / (rng1 = cll), rng2)
sht3.Cells(2,13) = rw
Any tips?
excel vba excel-vba
excel vba excel-vba
edited Nov 13 '18 at 17:57
Dominique
2,02041540
2,02041540
asked Nov 13 '18 at 14:18
Gabriel_KochGabriel_Koch
10511
10511
1
You can't compare or divide arrays like that in VBA. UseEvaluate
instead, passing the same formula string you would use in a cell.
– Rory
Nov 13 '18 at 14:31
Or use Find searching up to find the last cell that has that value and use offset to return the correct value.
– Scott Craner
Nov 13 '18 at 14:33
Evaluate doesn't work for me, I need the result of the row in VBA.
– Gabriel_Koch
Nov 13 '18 at 14:43
I tried using Find, but couldn't get it quite right
– Gabriel_Koch
Nov 13 '18 at 14:44
Evaluate
will return exactly the same result as the formula you mention, so it's hard to see how it wouldn't work for you.
– Rory
Nov 13 '18 at 15:02
|
show 1 more comment
1
You can't compare or divide arrays like that in VBA. UseEvaluate
instead, passing the same formula string you would use in a cell.
– Rory
Nov 13 '18 at 14:31
Or use Find searching up to find the last cell that has that value and use offset to return the correct value.
– Scott Craner
Nov 13 '18 at 14:33
Evaluate doesn't work for me, I need the result of the row in VBA.
– Gabriel_Koch
Nov 13 '18 at 14:43
I tried using Find, but couldn't get it quite right
– Gabriel_Koch
Nov 13 '18 at 14:44
Evaluate
will return exactly the same result as the formula you mention, so it's hard to see how it wouldn't work for you.
– Rory
Nov 13 '18 at 15:02
1
1
You can't compare or divide arrays like that in VBA. Use
Evaluate
instead, passing the same formula string you would use in a cell.– Rory
Nov 13 '18 at 14:31
You can't compare or divide arrays like that in VBA. Use
Evaluate
instead, passing the same formula string you would use in a cell.– Rory
Nov 13 '18 at 14:31
Or use Find searching up to find the last cell that has that value and use offset to return the correct value.
– Scott Craner
Nov 13 '18 at 14:33
Or use Find searching up to find the last cell that has that value and use offset to return the correct value.
– Scott Craner
Nov 13 '18 at 14:33
Evaluate doesn't work for me, I need the result of the row in VBA.
– Gabriel_Koch
Nov 13 '18 at 14:43
Evaluate doesn't work for me, I need the result of the row in VBA.
– Gabriel_Koch
Nov 13 '18 at 14:43
I tried using Find, but couldn't get it quite right
– Gabriel_Koch
Nov 13 '18 at 14:44
I tried using Find, but couldn't get it quite right
– Gabriel_Koch
Nov 13 '18 at 14:44
Evaluate
will return exactly the same result as the formula you mention, so it's hard to see how it wouldn't work for you.– Rory
Nov 13 '18 at 15:02
Evaluate
will return exactly the same result as the formula you mention, so it's hard to see how it wouldn't work for you.– Rory
Nov 13 '18 at 15:02
|
show 1 more comment
1 Answer
1
active
oldest
votes
If you want to use Find
instead of a lookup you can do it like this:
Sub Find_from_bottom()
Dim Rowaddress As Long
Dim sht2 As Worksheet
Dim sht3 As Worksheet
Set sht2 = Worksheets("Base")
Set sht3 = Worksheets("Extra")
Rowaddress = Range("A:A").Find(sht3.Cells(2, 12).Value, Range("A1"), SearchDirection:=xlPrevious).Row
sht3.Cells(2, 13) = sht2.Cells(Rowaddress, 2)
End Sub
Tryied your sugestion but it returns an error, shouldntRange...
inRowadress
be linked to a sheet? Something likesht3.Range...
– Gabriel_Koch
Nov 14 '18 at 15:09
Yes exactly at best you should put the sheet before as you wrotesht3.Range
, if you don't write it, thenRange.
refers to the active sheet (which might bring an error). If the Value is not in The Range("A:A") you also might get an error
– Pierre44
Nov 14 '18 at 15:13
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283049%2fvba-lookup-outputs-incompatible-types%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you want to use Find
instead of a lookup you can do it like this:
Sub Find_from_bottom()
Dim Rowaddress As Long
Dim sht2 As Worksheet
Dim sht3 As Worksheet
Set sht2 = Worksheets("Base")
Set sht3 = Worksheets("Extra")
Rowaddress = Range("A:A").Find(sht3.Cells(2, 12).Value, Range("A1"), SearchDirection:=xlPrevious).Row
sht3.Cells(2, 13) = sht2.Cells(Rowaddress, 2)
End Sub
Tryied your sugestion but it returns an error, shouldntRange...
inRowadress
be linked to a sheet? Something likesht3.Range...
– Gabriel_Koch
Nov 14 '18 at 15:09
Yes exactly at best you should put the sheet before as you wrotesht3.Range
, if you don't write it, thenRange.
refers to the active sheet (which might bring an error). If the Value is not in The Range("A:A") you also might get an error
– Pierre44
Nov 14 '18 at 15:13
add a comment |
If you want to use Find
instead of a lookup you can do it like this:
Sub Find_from_bottom()
Dim Rowaddress As Long
Dim sht2 As Worksheet
Dim sht3 As Worksheet
Set sht2 = Worksheets("Base")
Set sht3 = Worksheets("Extra")
Rowaddress = Range("A:A").Find(sht3.Cells(2, 12).Value, Range("A1"), SearchDirection:=xlPrevious).Row
sht3.Cells(2, 13) = sht2.Cells(Rowaddress, 2)
End Sub
Tryied your sugestion but it returns an error, shouldntRange...
inRowadress
be linked to a sheet? Something likesht3.Range...
– Gabriel_Koch
Nov 14 '18 at 15:09
Yes exactly at best you should put the sheet before as you wrotesht3.Range
, if you don't write it, thenRange.
refers to the active sheet (which might bring an error). If the Value is not in The Range("A:A") you also might get an error
– Pierre44
Nov 14 '18 at 15:13
add a comment |
If you want to use Find
instead of a lookup you can do it like this:
Sub Find_from_bottom()
Dim Rowaddress As Long
Dim sht2 As Worksheet
Dim sht3 As Worksheet
Set sht2 = Worksheets("Base")
Set sht3 = Worksheets("Extra")
Rowaddress = Range("A:A").Find(sht3.Cells(2, 12).Value, Range("A1"), SearchDirection:=xlPrevious).Row
sht3.Cells(2, 13) = sht2.Cells(Rowaddress, 2)
End Sub
If you want to use Find
instead of a lookup you can do it like this:
Sub Find_from_bottom()
Dim Rowaddress As Long
Dim sht2 As Worksheet
Dim sht3 As Worksheet
Set sht2 = Worksheets("Base")
Set sht3 = Worksheets("Extra")
Rowaddress = Range("A:A").Find(sht3.Cells(2, 12).Value, Range("A1"), SearchDirection:=xlPrevious).Row
sht3.Cells(2, 13) = sht2.Cells(Rowaddress, 2)
End Sub
answered Nov 13 '18 at 15:16
Pierre44Pierre44
1,2861421
1,2861421
Tryied your sugestion but it returns an error, shouldntRange...
inRowadress
be linked to a sheet? Something likesht3.Range...
– Gabriel_Koch
Nov 14 '18 at 15:09
Yes exactly at best you should put the sheet before as you wrotesht3.Range
, if you don't write it, thenRange.
refers to the active sheet (which might bring an error). If the Value is not in The Range("A:A") you also might get an error
– Pierre44
Nov 14 '18 at 15:13
add a comment |
Tryied your sugestion but it returns an error, shouldntRange...
inRowadress
be linked to a sheet? Something likesht3.Range...
– Gabriel_Koch
Nov 14 '18 at 15:09
Yes exactly at best you should put the sheet before as you wrotesht3.Range
, if you don't write it, thenRange.
refers to the active sheet (which might bring an error). If the Value is not in The Range("A:A") you also might get an error
– Pierre44
Nov 14 '18 at 15:13
Tryied your sugestion but it returns an error, shouldnt
Range...
in Rowadress
be linked to a sheet? Something like sht3.Range...
– Gabriel_Koch
Nov 14 '18 at 15:09
Tryied your sugestion but it returns an error, shouldnt
Range...
in Rowadress
be linked to a sheet? Something like sht3.Range...
– Gabriel_Koch
Nov 14 '18 at 15:09
Yes exactly at best you should put the sheet before as you wrote
sht3.Range
, if you don't write it, then Range.
refers to the active sheet (which might bring an error). If the Value is not in The Range("A:A") you also might get an error– Pierre44
Nov 14 '18 at 15:13
Yes exactly at best you should put the sheet before as you wrote
sht3.Range
, if you don't write it, then Range.
refers to the active sheet (which might bring an error). If the Value is not in The Range("A:A") you also might get an error– Pierre44
Nov 14 '18 at 15:13
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53283049%2fvba-lookup-outputs-incompatible-types%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
You can't compare or divide arrays like that in VBA. Use
Evaluate
instead, passing the same formula string you would use in a cell.– Rory
Nov 13 '18 at 14:31
Or use Find searching up to find the last cell that has that value and use offset to return the correct value.
– Scott Craner
Nov 13 '18 at 14:33
Evaluate doesn't work for me, I need the result of the row in VBA.
– Gabriel_Koch
Nov 13 '18 at 14:43
I tried using Find, but couldn't get it quite right
– Gabriel_Koch
Nov 13 '18 at 14:44
Evaluate
will return exactly the same result as the formula you mention, so it's hard to see how it wouldn't work for you.– Rory
Nov 13 '18 at 15:02