Consulting

Results 1 to 9 of 9

Thread: vba help on vlookup

  1. #1
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    5
    Location

    vba help on vlookup

    Hi Team,

    I am testing vlookup formula in VBA, I am facing couple of issues ,

    1) I want to shorten my code for by giving a variable name for Thisworkbook.sheet2!A1:B7 and use it in formula.
    2) Before reaching to formula, my macro is getting stuck on this line, :=> Set ws1 = wbk.Worksheets object doesn't support this property or method.

    I am new in vba , please assist in correcting my syntax. Thanks.

    Sub Test()
    Dim wbk As Workbook
    Dim ws1 As Worksheet
    Dim ws As Worksheet
    Dim lr As Long
    Set wbk = Workbooks.Open(Sheet1.Range("B5").Value)   ' F:\varsha\Book2.xlsx
    Set ws1 = wbk.Worksheets
    For Each ws In wbk.Worksheets
        Select Case ws.Name
            Case "A", "B", "C"
                lr = ws.Cells(Rows.Count, "A").End(xlUp).Row
                ws.Range("B2:B" & lr).Formula = "=VLOOKUP(A2,Thisworkbook.sheet2!A1:B7,2,false)"
        End Select
    Next ws
    Thanks.
    Peter
    Last edited by Aussiebear; 07-01-2022 at 08:15 PM. Reason: Added code tags to supplied code

  2. #2
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    "=VLOOKUP(A2,'[" & ThisWorkbook.Name & "]sheet2'!A1:B7,2,false)"

  3. #3
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    5
    Location
    Hi Team,

    Thanks for Quick reply.

    how to read this syntax [" & ThisWorkbook.Name & "] that bracket what it tells. and Symbol & in it.


    Regards,
    Peter

  4. #4
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Sub test()
        MsgBox ThisWorkbook.Sheets("Sheet2").Range("A1:B7").Address(False, False, xlA1, True)
    End Sub

  5. #5
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    5
    Location
    Hi mana,

    Thanks your formula worked.

    I have one more question is there any other option to get the result ,

    if I want to use below statement in formula, where I am getting wrong in it.

    Dim rng as range
    Set rng = ThisWorkbook. Sheets("Sheet2").Range("A1:B7").value
    "=VLOOKUP(A2,rng ,2,false)"
    Thanks
    Peter
    Last edited by Aussiebear; 07-01-2022 at 08:15 PM. Reason: Added code tags to supplied code

  6. #6
    VBAX Expert
    Joined
    Sep 2016
    Posts
    788
    Location
    Option Explicit
    
    
    Sub test2()
        Dim rng As Range
        Dim adr As String
        
        Set rng = ThisWorkbook.Sheets("Sheet2").Range("A1:B7")
        adr = rng.Address(False, False, xlA1, True)
    
    
       MsgBox "=VLOOKUP(A2," & adr & ",2,false)"
     
    End Sub

  7. #7
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    5
    Location
    Hi, mana....Your advised code works perfectly. Thanks for reviewing every time

    Regards,
    Mallesh

  8. #8
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    If you want to use the named range rng in your formulae then instead of:
    Set rng = ThisWorkbook. Sheets("Sheet2").Range("A1:B7")
    try:
    ThisWorkbook.Sheets("Sheet2").Range("A1:B7").name = "rng"
    then you can use "=VLOOKUP(A2,rng ,2,false)"
    With the added benefit that if you want to change the rng range, you can do so, and the formulae will update by themseleves.
    p45cal
    Everyone: If I've helped and you can't be bothered to acknowledge it, I can't be bothered to look at further posts from you.

  9. #9
    VBAX Newbie
    Joined
    Aug 2017
    Posts
    5
    Location
    Thank you so much, it worked. 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
  •