Solved

Excel VBA Help with Dynamic VLOOKUP Range

Posted on 2011-09-19
5
329 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 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

Industry Leaders: 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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

705 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