Solved

Excel VBA Help with Dynamic VLOOKUP Range

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Dealing with unintended Excel Active-X resizing quirks (VBA code simulates "self correction") David Miller (dlmille) Intro Not everyone is a fan of Active-X controls in spreadsheets (as opposed to the UserForm approach, the older Form controls …
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 …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now