You are here:

Excel/variables inserted in formula


HI Stuart,

This is a follow up to the question I posted last night. I'll try to be more clear.
The last line of Excel VBA code pasted below returns a debug Run time error 1004, "Application defined or object defined error."

The macro opens two excel workbooks and names them accordingly with variable names: SourceBook and DestinationSheet.

It attempts to insert a vlookup formula into a range of cells E4:E15 on the destination worksheet.  Based on the contents of cell $B$1 of the Destination worksheet, the formula searches the named range "test" in the SourceBook and returns the values stored in the column number of that range based on the formula "9 + the contents of cell C4" on the destination workbook.  

When I enter the vlookup formula directly in Excel referencing the actual name of the source workbook it works great.  I'm trying to do it in VBA because the source workbook will change each month and I want to eventually apply this to multiple worksheets on the destination workbook.  
Alternatively, I attempted to use Application.WorksheetFunction for VLookup but can't get that syntax to work properly either.
I'm confused how to reference variables in formulas.  I see they sometimes are surrounded by ".

thanks for your help

Sub postvolume()

Dim SourceBook As Workbook
Dim DestinationSheet As Worksheet

Call Open_File_Dialog_Box

Set SourceBook = ActiveWorkbook
ThisWorkbook.UpdateLinks = xlUpdateLinksNever

MsgBox SourceBook.Name

Call Open_File_Dialog_Box

Set DestinationSheet = ActiveSheet

MsgBox DestinationSheet.Name

ThisWorkbook.UpdateLinks = xlUpdateLinksNever
MsgBox Range("D4").Value

Range("E4:E15").Formula = _
"=VLOOKUP($B$1,'[" & SourceBook.Name & "]'!test,9+C4,FALSE)"

End Sub

Assuming that in your Source workbbook, you have the named range "test" on the first sheet, and this "test" range is used in your formula, the vba code isn't

    Range("E4:E15").Formula = _
         "=VLOOKUP($B$1,'[" & SourceBook.Name & "]'!test,9+C4,FALSE)"

but rather

    Range("E4:E15").Formula = _
         "=VLOOKUP($B$1,'[" & SourceBook.Name & "]" & _
         SourceBook.Sheets(1).Name & "'!test,9+C4,FALSE)"
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Stuart Resnick


I can answer questions relating to MS Excel formulas, or to programming with vba (Visual Basic for Applications) in the Excel environment. Please follow the following guidelines: your question should focus on one specific issue you want to learn. It's beyond the scope of this free service for me to create entire projects or complex vba solutions for you from scratch. You should be able to do most of the work yourself, and come here when you need help with a specific point you're stuck on. ALWAYS include a simple, concrete example illustrating what you want to learn. Explain this example in detail in the text of your question (what data is in which cells of which sheets, etc). Be very precise about the results you want, using this sample to make the logic clear. Always keep these examples SIMPLE. Never e.g. use 18 worksheets in your example if using 2 or 3 will do. Never use ranges like AI567:BB865 if using a range like A1:B3 will do. Thanks.


As a consultant, I've designed Excel tools since the 90s, working for the Federal Reserve Bank, AT&T, and (currently) Gap Inc.

My only "education" comes from 2 decades of doing spreadsheet/programming work, with major SF Bay Area corporations such as AT&T, Federal Reserve Bank, and Gap Inc.

©2017 All rights reserved.

[an error occurred while processing this directive]