Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Excel VBA Help with Dynamic VLOOKUP Range

Posted on 2011-09-19
5
Medium Priority
?
336 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
[X]
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
  • 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 2000 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
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…

636 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