+ Reply to Thread
Results 1 to 5 of 5

Vlookup from another workbook Using R1C1 Notation

Hybrid View

  1. #1
    Registered User
    Join Date
    04-29-2019
    Location
    Tampa, Florida
    MS-Off Ver
    Office 365
    Posts
    8

    Vlookup from another workbook Using R1C1 Notation

    Hi,

    I am building a macro to open a workbook and import data from that workbook using VLOOKUP.

    I am getting a Run-time error '13' Type mismatch

    The code uses FileDialog to prompt user to select the file.
    Then I have used variables to store the workbook name and sheet name.
    I then use those variables to set range variables to plug into the VLOOKUP formula.
    The line of code triggering the error is the .formulaR1C1 line.

    Any help to figure out what is wrong would be appreciated.

    
    Set wb = Workbooks(fullpath)
        Set ws = wb.Sheets(1)
        lastcolumn = ws.Cells(1, ws.Columns.Count).End(xlToLeft).Column
        lastrow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row
        lcOEF = wsOEF.Cells(1, ws.Columns.Count).End(xlToLeft).Column
        lrOEF = wsOEF.Cells(ws.Rows.Count, 1).End(xlUp).Row
        Set headerMRP = ws.Range("A1", Cells(1, lastcolumn))
        Set tblMRP = ws.Range("A2", Cells(lastrow, lastcolumn))
        Set importMRPRange = wsOEF.Range(wsOEF.Cells(6, 121), wsOEF.Cells(lrOEF, 133))
        
        With importMRPRange
            .FormulaR1C1 = _
            "=VLOOKUP(RC1," & tblMRP & ",MATCH(R5C," & headerMRP & ",0),FALSE)"
            .Value = .Value
        End With

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Vlookup from another workbook Using R1C1 Notation

    Try replacing tblMRP with tblMRP..Address(ReferenceStyle:=xlR1C1), do the same for headerMRP.
    Set wb = Workbooks(fullpath)
    Set ws = wb.Sheets(1)
    
    With ws
        lastcolumn = .Cells(1, ws.Columns.Count).End(xlToLeft).Column
        lastrow = .Cells(ws.Rows.Count, 1).End(xlUp).Row
        Set headerMRP = .Range("A1", .Cells(1, lastcolumn))
        Set tblMRP = .Range("A2", .Cells(lastrow, lastcolumn))
    End With
    
    With wsOEF
        lcOEF = .Cells(1, ws.Columns.Count).End(xlToLeft).Column
        lrOEF = .Cells(ws.Rows.Count, 1).End(xlUp).Row
        Set importMRPRange = .Range(wsOEF.Cells(6, 121), .Cells(lrOEF, 133))
    End With
    
    With importMRPRange
        .FormulaR1C1 = _
        "=VLOOKUP(RC1," & tblMRP.Address(ReferenceStyle:=x1R1C1) & ",MATCH(R5C," & headerMRP.Address(ReferenceStyle:=x1R1C1) & ",0),FALSE)"
        .Value = .Value
    End With
    If posting code please use code tags, see here.

  3. #3
    Registered User
    Join Date
    04-29-2019
    Location
    Tampa, Florida
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Vlookup from another workbook Using R1C1 Notation

    Thank you for that reply. That solved the error message but the formula that writes to the range is missing the reference to the new workbook so I get #n/a in the cells. Any idea why that might be happening?

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,646

    Re: Vlookup from another workbook Using R1C1 Notation

    Try this.
    With importMRPRange
        .FormulaR1C1 = _
        "=VLOOKUP(RC1," & tblMRP.Address(ReferenceStyle:=x1R1C1, External:=True) & ",MATCH(R5C," & headerMRP.Address(ReferenceStyle:=x1R1C1, External:=True) & ",0),FALSE)"
        .Value = .Value
    End With
    Last edited by Norie; 11-20-2020 at 10:36 AM.

  5. #5
    Registered User
    Join Date
    04-29-2019
    Location
    Tampa, Florida
    MS-Off Ver
    Office 365
    Posts
    8

    Re: Vlookup from another workbook Using R1C1 Notation

    Norie, that was awesome. That did the trick. I can't thank you enough.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Simplifying R1C1 Notation
    By PercivalP in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-13-2018, 08:43 AM
  2. [SOLVED] Using R1C1 notation
    By samot79 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-10-2018, 04:19 PM
  3. R1C1 notation in a FormulaArray
    By n043480 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-10-2016, 05:29 AM
  4. R1C1 notation
    By samot79 in forum Excel General
    Replies: 4
    Last Post: 12-11-2014, 06:32 AM
  5. [SOLVED] What is the advantage of A1 or R1C1 Notation ?
    By nur2544 in forum Excel General
    Replies: 1
    Last Post: 02-08-2013, 12:18 PM
  6. Sum Columns:R1C1 notation
    By Zone in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-17-2006, 11:05 AM
  7. [SOLVED] R1C1 Notation in VBA
    By Fred Holmes in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-21-2005, 05:05 PM

Tags for this Thread

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1