[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Insert a variable in Vlookup and automate Vlookup with a macro

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

650 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