VBA lookup outputs incompatible types










3















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?










share|improve this question



















  • 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















3















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?










share|improve this question



















  • 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













3












3








3


1






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?










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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. 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












  • 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







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












1 Answer
1






active

oldest

votes


















0














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





share|improve this answer























  • 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










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
);



);













draft saved

draft discarded


















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









0














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





share|improve this answer























  • 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















0














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





share|improve this answer























  • 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













0












0








0







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





share|improve this answer













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






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 13 '18 at 15:16









Pierre44Pierre44

1,2861421




1,2861421












  • 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

















  • 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
















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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto