Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 29th, 2012, 05:08 PM
Registered User
 
Join Date: Oct 2012
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default lookup function in VBA EXcel 2007

this code results in a name error... if i put the actual cell reference of f92 in vlookup in place of the varibale lookupvalue it works, but i have to use a variable because the row is different each day.
a previous vba procedure figures out the rowcount of error 84 and puts it in row84count, a global variable. this procedure then correctly figures out the value for LookupValue .. my msgbox shows the right value is in lookupvalue, but the lookupfunction is not recoognizing my variable Lookupvalue. here is the code




Windows("Copy of UMJ134Errors_Today.xls").Activate


Dim LookupStartRow As String
Dim LookupStartCell As String
Dim LookupValue As String
'Dim LookupErrorCode As String

'Variable setup
LookupStartRow = Row84Count + 2
LookupStartCell = "Q" & LookupStartRow
LookupValue = "f" & LookupStartRow


MsgBox "The contents of Row84Count is " & Row84Count
MsgBox "The contents of LookupStartCell is " & LookupStartCell

'Select Cell to Begin Lookup
Range(LookupStartCell).Select

'Vlookup
ActiveCell.Formula = "=VLOOKUP(lookupvalue,Error84,10,false)"

MsgBox "The contents of LookupValue is " & LookupValue
 
Old December 19th, 2012, 04:07 PM
Friend of Wrox
 
Join Date: Feb 2007
Posts: 163
Thanks: 0
Thanked 2 Times in 2 Posts
Default

The problem is that what you're placing as your formula in the cell is exactly what is between the quotes.

Let us assume LookupValue contains "f100". The way you have your variable set up, your active cell's formula would read:
=VLOOKUP(lookupvalue,Error84,10,false)

Another issue is Error84 a named range with at least 10 columns? I'll assume that it is.

Your code would be:
Code:
ActiveCell.Formula = "=VLOOKUP(" & LookupValue & ",Error84,10,false)"
Note that the LookupValue containing the string you want (in this case "f100") added is outside your literal string quotes now. The formula in the cell would then read:
=VLOOKUP(f100,Error84,10,false)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Excel 2007 VBA UDF problem snowman.hk Excel VBA 1 October 24th, 2009 08:50 AM
#NAME? error running Excel 2003 VBA in Excel 2007 steveburn Excel VBA 0 October 24th, 2009 08:47 AM
Help! Conversion Excel 2003 VBA codes to Excel 2007 sunny76 BOOK: Excel 2007 VBA Programmer's Reference ISBN: 978-0-470-04643-2 0 August 13th, 2009 05:38 AM
excel 2007 vba UDF DavidReese BOOK: Access 2007 VBA Programmer's Reference ISBN: 978-0-470-04703-3 1 February 24th, 2009 01:10 AM
Excel/VBA Multi-Column Lookup - Round 2 RollingWoodFarm Excel VBA 4 August 3rd, 2006 07:28 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.