Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Excel VBA Help with Dynamic VLOOKUP Range

Posted on 2011-09-19
5
323 Views
Last Modified: 2012-05-12
I have a large macro that gets data from many sheets and combines it into a template monthly.

At this point in the macro I have a workbook open with 2 sheets of data and the focus is at the last row of Sheet2

I also have a template open that has 4 columns that will reference 2 cols in each data sheets.
I wanted to use Vlookup from the Template which will always be in the same cells.
From that I will reference the data sheets which also always has the same columns but
the data is DYNAMIC monthly so I need a lookup down to a varaible number of rows.

On the Template, Col C has the lookup value for all 4 data columns.
The reference ranges on the data sheets will be Cols K to T or K to U.

I have tried to reuse some code that worked earlier in the sheet and have posted it below.  
Thank you for your assistance and direction.
I would like to learn as I go so any help there is also appreciated.
' Copy OGM into Template using Vlookup
    ' OGM Part$ Plan from ("MakePLGFP.xls") ColU
    Windows("GTFPTemplate.xls").Activate
    Range("G7").Select
    Set rngFormula2 = Range(Selection, Selection.End(xlDown))
'** Values recorded =VLOOKUP(C7,[MakePLGFP.xls]Sheet1!$K$1:$X$126,10,FALSE)
     strLookup2 = "=VLOOKUP(RC[-4],[MakePLGFP.xls]Sheet1!R1C11:R200C20,10,FALSE)"
     strFormula2 = "=IF(ISERROR(" & strLookup2 & "), 0, " & strLookup2 & ")"
     rngFormula2.FormulaR1C1 = strFormula2
** Application defined error at the line above   
   
 ' OGM Part$ Actual from ("MakePLGFP.xls")  ColT
    Range("H7").Select
    Set rngFormula = Range(Selection, Selection.End(xlDown))
     strLookup3 = "=VLOOKUP(RC[-5],[MakePLGFP.xls]Sheet1!C11:C19,9,FALSE)"
     strFormula3 = "=IF(ISERROR(" & strLookup3 & "), 0, " & strLookup3 & ")"
     rngFormula.FormulaR1C1 = strFormula3
 
 ' Copy CRU into Template 
    ' CRU Part$ Plan from ("MakePLGFP.xls")  ColU
    Range("N7").Select
    Set rngFormula = Range(Selection, Selection.End(xlDown))
     strLookup4 = "=VLOOKUP(RC[-4],[MakePLGFP.xls]Sheet2!C11:C20,10,FALSE)"
     strFormula4 = "=IF(ISERROR(" & strLookup4 & "), 0, " & strLookup4 & ")"
     rngFormula.FormulaR1C1 = strFormula4
    
    ' CRU Part$ Actual from ("MakePLGFP.xls")  ColT
    Range("O7").Select
    Set rngFormula = Range(Selection, Selection.End(xlDown))
     strLookup5 = "=VLOOKUP(RC[-5],[MakePLGFP.xls]Sheet2!C11:C19,9,FALSE)"
     strFormula5 = "=IF(ISERROR(" & strLookup5 & "), 0, " & strLookup5 & ")"
     rngFormula.FormulaR1C1 = strFormula5

Open in new window

0
Comment
Question by:Mswetsky
  • 3
  • 2
5 Comments
 
LVL 11

Expert Comment

by:thydzik
ID: 36564842
can you please ask a more specific question, I am not sure what your question is.
0
 
LVL 1

Author Comment

by:Mswetsky
ID: 36566995
Thanks for the reply.
Line 9 gets an error and I don't know why or what to change.
0
 
LVL 11

Accepted Solution

by:
thydzik earned 500 total points
ID: 36567661
try removing the equals sign from
strLookup2 = "=VLOOKUP(RC[-4],[MakePLGFP.xls]Sheet1!R1C11:R200C20,10,FALSE)"

to make it
strLookup2 = "VLOOKUP(RC[-4],[MakePLGFP.xls]Sheet1!R1C11:R200C20,10,FALSE)"
0
 
LVL 11

Expert Comment

by:thydzik
ID: 36567691
i would also consider changing your code to the following, if it does the same;

     strLookup2 = "VLOOKUP(RC[-4],[MakePLGFP.xls]Sheet1!R1C11:R200C20,10,FALSE)"
     strFormula2 = "=IF(ISERROR(" & strLookup2 & "), 0, " & strLookup2 & ")"
     Range("G7", Cells(Sheets("Sheet1").UsedRange.Rows.Count, 7).Address).FormulaR1C1 = strFormula2
0
 
LVL 1

Author Closing Comment

by:Mswetsky
ID: 36567692
Thank you very much!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question