+ Reply to Thread
Results 1 to 2 of 2

HALP! Creating a number of variables based on a variable.

Hybrid View

  1. #1
    Registered User
    Join Date
    09-08-2014
    Location
    -
    MS-Off Ver
    -
    Posts
    68

    HALP! Creating a number of variables based on a variable.

    Hallo!
    So I have a variable, NumToGen (Integer); I want to create multiple text strings based on NumToGen
    So for example,
    Dim Header(1 To NumToGen) As String
    However, when I attempt to do this, it insists that NumToGen must be a constant; I know a constant cannot be based upon a variable as constants are defined when the macro compiles. Is the situation the same with defining variables?
    Any solution to this? I mean, I could theoretically:
    Dim Header (1 to 100) As String
    and then only actually refer to the variables Header(1 to NumToGen) when I'm coding - for example, via a loop as so:
    For iTemp = 1 To NumToGen
         Header(iTemp) = "Marmalade is such a fabulous food item. In fact, " & Range("A" & x + iTemp).Value & " agrees with me!"
    Next iTemp
    But this means I have a bunch of defined variables floating around which are useless and it seems a bit inefficient.
    Any suggestions?
    Thanks!
    Lady Marmalade

  2. #2
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: HALP! Creating a number of variables based on a variable.

    You need to know the number of elements when declaring an array

    You could declare an basic array first
    Dim Header() as String  '// Brackets indicate this is an array
    Then, when you know how many elements to go in...
    NumtoGen = 100
    ReDim Header(NumToGen)
    That will Redim the array with 100 elements, numbered from 0 to 99. Be careful declaring the lower boundary - it won't take long before you get totally confused with your array starting with 1 and VBAs default lower bound which is 0 - it's the old 'Does the Index start at 0 or 1?' question.
    Last edited by cytop; 10-23-2014 at 10:12 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Creating combo box with different number of variables
    By moi in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 09-06-2005, 04:05 AM
  2. [SOLVED] Creating combo box with different number of variables
    By fullers in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 02:05 AM
  3. Creating combo box with different number of variables
    By fullers in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 01:05 AM
  4. Creating combo box with different number of variables
    By fullers in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-06-2005, 12:05 AM
  5. [SOLVED] Creating combo box with different number of variables
    By fullers in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-04-2005, 03:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1