Solved

Insert a variable in Vlookup and automate Vlookup with a macro

Posted on 2011-03-16
5
614 Views
Last Modified: 2012-05-11
Hello all

In the attached file I want to do the following;

1. create an input box
2. have the input box pass a variable ( the sheet name) into a Vlookup formula
    In this case the sheet name is Feb Data, but that will change month to month
    The Vlookup formula starts is cell F9 of sheet 82239 - OEM
3. have Vlookup run starting in cell F9 and filldown.
4. remove the Vlookup formula and leave its output.

Note: don't care about the totals in the file

Any help would be appreciated. Test.xls
0
Comment
Question by:pyramid
[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 39

Expert Comment

by:nutsch
ID: 35148492
Here is a code that will do it, after you do a copy / paste values on your existing vlookups.

Thomas


Sub Macro5()
    Dim strSheet As String, sht As Worksheet, rg As Range
    
    strSheet = InputBox("Enter new sheet name")
    
    For Each sht In ActiveWorkbook.Worksheets
        If sht.Name = strSheet Then GoTo foundSheet
    Next sht
    
    MsgBox "sheet doesn't exist"
    Exit Sub
    
foundSheet:
    
    Set sht = Sheets(strSheet)

On Error Resume Next

    Set rg = Range("F9:F" & Rows.Count).SpecialCells(xlCellTypeConstants, xlNumbers)
       
    If Err <> 0 Then
        MsgBox "no cells to replace"
        Exit Sub
    End If
    
    rg.FormulaR1C1 = _
        "=VLOOKUP(RC[-3],'" & strSheet & "'!R[-5]C[10]:R65536C17,2,FALSE)"
    
    rg.Value = rg.Value
    
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:pyramid
ID: 35150167
Hi Thomas, thanks for this

I am getting the no cells to replace error from your code so I am assuming I am doing something wrong probably related to the cut/paste values you taking about. Can you provide more detail? Do I need to have something in column F?

thanks

P.
0
 
LVL 1

Author Comment

by:pyramid
ID: 35150343
Hi Again

So I put values in each cell = to 0 Dollars. I created a button and assigned your code to a macro. I am getting alot of NA errors in cells that I know I should have data. Any suggestions? I attached another file so you can see what I mean.

thanks again
P.
Test2.xls
0
 
LVL 39

Accepted Solution

by:
nutsch earned 250 total points
ID: 35151569
Yes, the initial vlookup formula didn't have absolute addressing on the lookup range.

Here is a correction that should address that.


Option Explicit

Sub VlookupMaster()
    Dim strSheet As String, sht As Worksheet, rg As Range
    
    strSheet = InputBox("Enter new sheet name")
    
    For Each sht In ActiveWorkbook.Worksheets
        If sht.Name = strSheet Then GoTo foundSheet
    Next sht
    
    MsgBox "sheet doesn't exist"
    Exit Sub
    
foundSheet:
    
    Set sht = Sheets(strSheet)

On Error Resume Next

    Set rg = Range("F9:F" & Rows.Count).SpecialCells(xlCellTypeConstants)
       
    If Err <> 0 Then
        MsgBox "no cells to replace"
        Exit Sub
    End If
    
    rg.FormulaR1C1 = _
        "=VLOOKUP(RC[-3],'" & strSheet & "'!R4C16:R65536C17,2,FALSE)"
    
    rg.Value = rg.Value
    
End Sub

Open in new window

0
 
LVL 1

Author Comment

by:pyramid
ID: 35160050
Thomas
It works thanks
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
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…

730 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