Consulting

Results 1 to 4 of 4

Thread: VBA Help - Reference to a Table & Vlookup

  1. #1
    VBAX Newbie
    Joined
    Feb 2021
    Posts
    1
    Location

    VBA Help - Reference to a Table & Vlookup

    Hi,

    I'm a Newbie to Excel VBA.

    I have 2 .xlsx files named, Template.xlsx and RequestChange.xlsx (the data in this file is in a Table view). Trying to Vlookup data and have it returned in the Template file, while RequestChange.xlsx is a closed book.

    I wrote the following code but keeps getting stuck on referencing to the Table. Also, end goal is to Vlookup the whole Table and return data in specific columns, like column B, C, E, F (for example).

    Please help! Tia

    ---------------------------------------------------------------------------------------------

    Sub Vlookup()
    Dim rw As Long, x As Long, x As Range
        Dim extwbk As Workbook, twb As Workbook
    Set twb = ThisWorkbook
        Set extwbk = Workbooks.Open("C:\Users\xxxx\MyDocument\RequestChange.xlsx", True, True)
        Set x = extwbk.Worksheets("Sheet1").ListObjects("Change__2").Range.Select
    With twb.Sheets("Form")
    For rw = 2 To .Cells(Rows.Count, 1).End(x1Up).Row
                On Error Resume Next
                .Cells(rw, 2) = Application.Vlookup(.Cells(rw, 1).Value2, x, 2, False)
            Next rw
    End With
    extwbk.Close savechanges:=False
    End Sub
    Last edited by Aussiebear; 02-21-2021 at 08:49 PM. Reason: Added code tags

  2. #2
    VBAX Guru mancubus's Avatar
    Joined
    Dec 2010
    Location
    "Where I lay my head is home" :D
    Posts
    2,644
    welcome to the forum.

    do not use vba specific or reserved words (such as vlookup, path, etc) as procedure or variable names.
    do not assign the same word to different variables (x as long, x as range in your code).
    use CODE tags when posting your code (see #1 in my forum signature).

    that being said, you may try this:

    Sub vbax_68443_Vlookup()
    
        Dim rw As Long
        Dim twb As Workbook
        Dim arr
        
        Set twb = ThisWorkbook
        Workbooks.Open ("C:\Users\xxxx\MyDocument\RequestChange.xlsx")
        arr = Worksheets("Sheet1").ListObjects("Change__2").DataBodyRange.Value
        extwbk.Close savechanges:=False
        
        On Error Resume Next
        
        With twb.Sheets("Form")
            For rw = 2 To .Cells(.Rows.Count, 1).End(x1Up).Row
                .Cells(rw, 2) = Application.Vlookup(.Cells(rw, 1).Value2, arr, 2, False)
            Next rw
        End With
    
    End Sub
    PLS DO NOT PM; OPEN A THREAD INSTEAD!!!

    1) Posting Code
    [CODE]PasteYourCodeHere[/CODE]
    (or paste your code, select it, click # button)

    2) Uploading File(s)
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) (multiple files can be selected while holding Ctrl key) / Upload Files / Done
    Replace company specific / sensitive / confidential data. Include so many rows and sheets etc in the uploaded workbook to enable the helpers visualize the data and table structure. Helpers do not need the entire workbook.

    3) Testing the Codes
    always back up your files before testing the codes.

    4) Marking the Thread as Solved
    from Thread Tools (on the top right corner, above the first message)

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,872
    try:
    Sub myVlookup()
    With Workbooks.Open("C:\Users\xxxx\MyDocument\RequestChange.xlsx", True, True)
      With ThisWorkbook.Sheets("Form")
        With .Range("B2:B" & .Cells(.Rows.Count, 1).End(xlUp).Row)
          .FormulaR1C1 = "=VLOOKUP(RC[-1],RequestChange.xlsx!Change__2[#Data],2,FALSE)"
          .Value = .Value
        End With
      End With
      .Close savechanges:=False
    End With
    End Sub
    (Untested)
    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.

  4. #4
    Knowledge Base Approver VBAX Wizard
    Joined
    Apr 2012
    Posts
    5,642
    Avoid Excel functions (like vlookup) in VBA: VBA is faster.
    Minimize interactions with the workbook (reading/writing)

    Sub M_snb()
      sn = thisworkbook.sheets("form").cells(1).currentregion.resize(,2)
      with Getobject("C:\Users\xxxx\MyDocument\RequestChange.xlsx")
        sp = .sheets(1)).ListObjects(1).DataBodyRange
        .Close 0
      end with
    
      with createobject("scripting.dictionary")    
        for j = 1 to ubound(sp)
          .item(sp(j,1)) = sp(j,2)
        next
    
        for j = 2 to ubound(sn)
          if .exists(sn(j,1)) then sn(j,2) = .item(sn(j,1))
        next
      End With
    
      thisworkbook.sheets("form").cells(1).currentregion.resize(,2) = sn
    End Sub
    Last edited by snb; 02-18-2021 at 03:32 AM.

Posting Permissions

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