Solved

Insert a variable in Vlookup and automate Vlookup with a macro

Posted on 2011-03-16
5
583 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
  • 3
  • 2
5 Comments
 
LVL 39

Expert Comment

by:nutsch
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thomas
It works thanks
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
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 Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

728 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

12 Experts available now in Live!

Get 1:1 Help Now