?
Solved

Insert a variable in Vlookup and automate Vlookup with a macro

Posted on 2011-03-16
5
Medium Priority
?
631 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 1000 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

Technology Partners: 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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

752 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