Results 1 to 7 of 7

Thread: vlookup in vba

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Unhappy vlookup in vba

    I wish to find a value in a worksheet table, which I can do as a function within the worksheet by using the VLookup function. I am trying to automate the calculation of this by placing it as part of a macro, but I can not find the syntax in the online help.


    Can someone give me a sample code that shows how you use a worksheet
    function (VLookup) within a VBA script, capture the result and then set the
    value of a cell?

    I know this doesn't work, but this is what I am looking for...

    =vlookup(activecell.value,named_range,2,false)

    Thank you!

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vlookup in vba

    The .Find method should be easier for you if you want vba code. Otherwise you can record a macro of your desired actions and stop. Then check out the geneated code in the module section.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Re: vlookup in vba

    if my worksheet has

    thu 1
    fri 2
    sat 3
    in cells a1:b3

    what would the code (within the macro) look like to be able to lookup friday and return the value 2.

    something like...

    varDate = codecodecode

    Then I could use varDate as another variable.

    This is all part of a email script where the day is used as part of the .body of the email.

    .Body = "Hello " & varFirs & "," & vbCrLf & "Your service commitment is on " & varDate & ". That's " & varDay & " between " & varTime2 & " " & varTime3 & " " & varTime4 & "." & vbCrLf & "You will be filling the position of " & varPosi & "." & vbCrLf & "Please respond to this email as soon as possible to confirm that you will be able to attend and fulfill your commitment." & vbCrLf & vbCrLf & myform.txtBody.Text & vbCrLf & vbCrLf & "Thank You," & vbCrLf & "Jennie C

    On a side note, I am not able to use "space_" to drop this large script on more than one line. It turns it all red.

    thanks for your help.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vlookup in vba

    Did you record a macro of you creating the vlookup for this yet?

    To do a space_ line continuation yoou need to close any double quotes on your string and inclide an ampersand character.

    "This is a long text of code" _
    & " and it continues on the next line of code."
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Re: vlookup in vba

    Yes I did create a macro, but I am to select one lookup variable, i.e. A1 (or the named range for A1).

    The macro I need it for tests various criteria based on ActiveCell.Value and ActiveCell.Offset. I need the lookup function to use one of these ActiveCell objects as the lookup value. Because of this, it seems to me that it needs to be imbedded in the code of the particular subroutine.

    Does this make sense?

    I understand the & deal, thanks.

  6. #6
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: vlookup in vba

    Sounds like you may be needing more then what vloopup can do for you.

    Did you look at the .Find method as I think it will be more of what you want to use.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  7. #7

    Thread Starter
    New Member
    Join Date
    Mar 2006
    Posts
    14

    Re: vlookup in vba

    I'll check it out.
    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width