Solved

Automating VLookup

Posted on 2011-02-27
5
358 Views
Last Modified: 2012-05-11
Hello,

I was able to record a macro which used a vlookup.  The problem I am facing is that the range in the vlookup will change from day to day.  How do I set and update the range for the vlookup formula when the macro is run? Please let me know if any information is needed. Thanks for the help.

0
Comment
Question by:jvera524
  • 3
  • 2
5 Comments
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
Will the number of rows increase or the columns?

Sid
0
 

Author Comment

by:jvera524
Comment Utility
number of rows
0
 
LVL 30

Expert Comment

by:SiddharthRout
Comment Utility
Then it shouldn't be a problem.

Two ways to achieve what you want.

Here is an example

Let' s say the current VBA Code is

Sub Sample()
    Range("F2").Formula = "=Vlookup(C1,$C$1:$E$10,3,0)"
End Sub

Open in new window


You can change that to

1) Amend the formula to take the entire column into account. So the above becomes

Sub Sample()
    Range("F2").Formula = "=Vlookup(C1,C:E,3,0)"
End Sub

Open in new window


2) Or this to take realistic rows into account.

Sub Sample()
    Dim LastRow As Long
    LastRow = Sheets("Sheet1").Range("C" & Rows.Count).End(xlUp).Row
    Range("F2").Formula = "=Vlookup(C1,$C$1:$E$" & LastRow & ",3,0)"
End Sub

Open in new window


Sid

0
 

Author Comment

by:jvera524
Comment Utility
Below is some code.  I am trying use what you gave above in a for loop. It gives a complie error on the bolded line Thank you for the help.

    Dim cls As Range
    Dim cl As Integer
    Dim LastRow As Long
   
    Sheets("Data").Select
    Range("A2").Select
    Set cls = Cells(Rows.Count, "A").End(xlUp)
    LastRow = Sheets("Yesterday").Range("BM" & Rows.Count).End(xlUp).Row
       
    For cl = 2 To cls.Row + 1
       Cells(cl, "BP").Formula "=Vlookup(Cells(cl,"BM"),Sheets("Yesterday").Range($BM$2:$BM$" &     LastRow & ",1,False)"
    Next
0
 
LVL 30

Accepted Solution

by:
SiddharthRout earned 500 total points
Comment Utility
Is this what you are trying to do?

Sub Sample()
    Dim cls As Long
    Dim cl As Long, LastRow As Long
   
    cls = Sheets("Data").Range("A" & Rows.Count).End(xlUp).Row
    
    LastRow = Sheets("Yesterday").Range("BM" & Rows.Count).End(xlUp).Row
    
    For cl = 2 To cls
        '~~> I am assuming that you want to put the formula in Cell BP
        '~~> Of Sheet Data
        Sheets("Data").Range("BP" & cl).Formula = _
        "=Vlookup(BM" & cl & ",Yesterday!$BM$2:$BM$" & LastRow & ",1,False)"
    Next
End Sub

Open in new window


Sid
0

Featured Post

Free Trending Threat Insights Every Day

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

Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

10 Experts available now in Live!

Get 1:1 Help Now