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 November 10th, 2006, 11:28 AM
Registered User
Join Date: Nov 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default dynamic variable/object names


I'm slowly getting my head round VBA, but one feature I make use of frequently in other languages is dynamically naming a variable or object. I understand that some of this functionality can be reproduced with named ranges but could someone please give me a straight answer to this question:

In VBA is there an equivelent function to javaScript's eval() function or Object access operator [], which will allow me to create a variable at runtime like this:

Dim someString & someNumber As Integer

Js equivalent var eval(someString + someNumber) //(ish I'm a bit out of practice)

VBA claims to be an OO environment so it seems this functionality must exist. This is more for my own understanding of the language rather than a practical problem so please no "just use a named range" answers.

Many thanks
Old November 13th, 2006, 07:03 AM
Friend of Wrox
Join Date: Jun 2003
Posts: 173
Thanks: 0
Thanked 3 Times in 3 Posts

Hi there,

I'm not 100% sure I understand the question but I think you're asking if you can dynamically, at run-time, declare a variable with a name to be determined at run-time. If this is what you're asking after then I'm not aware of any such functionality.

I guess one work-around would be to declare a two part User Type variable which consisted of (to extend your original example) an integer property and a tag string property. This would then lend itself to holding the integer information in the integer part and the dynamic name in the tag placeholder. Of course this is only useful if you really need this sort of functionality as it is a bit clunky. I guess having learnt to write code in VBA I don't see the need for dynamic run-time variable names.

I'd be wary of any claims from VBA to be OO because it isn't. To be fair it does try and is not bad in most places but I wouldn't alway s expect to see generic OO functionality in VBA.


Old November 13th, 2006, 07:11 AM
Registered User
Join Date: Nov 2006
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts

Yep, that was exactly what I was asking, thanks very much.

Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamic column names in crystal reports pankaj_daga Crystal Reports 3 January 19th, 2009 02:55 AM
generate variable names dinamycally gcorcuera Classic ASP Basics 7 May 21st, 2007 09:53 AM
Resolution of property names on window object AGS BOOK: Professional JavaScript for Web Developers ISBN: 978-0-7645-7908-0 3 August 5th, 2006 11:50 AM
Problem with variable names shenku Javascript 2 May 6th, 2005 03:46 PM
Retrieving Dynamic Field Names tdaustin Classic ASP Basics 4 January 12th, 2004 11:10 AM

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