+ Reply to Thread
Results 1 to 5 of 5

Excel VBA: Can I refer to a textbox as a variable?

Hybrid View

  1. #1
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127

    Excel VBA: Can I refer to a textbox as a variable?

    Quick question about textboxes and For-Next loops.

    I have a series of textboxes in a userform, used chronologically (ie, textbox1, textbox2, ... textboxN)

    Lets say I have a For Next loop, 1 to N, how can I refer to the textboxes? I have tried (and failed) to use code like below:

    For i = 1 to N
         Range("A" & i) = textbox & i
    Next i
    I am 99% sure I have done something like this before, and I just cannot remember how.

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello BigBas,

    Like this...
    For i = 1 to N
         Range("A" & i) = Controls("TextBox" & i).Value
    Next i
    Sincerely,
    Leith Ross

  3. #3
    Forum Expert
    Join Date
    01-12-2007
    Location
    New Jersey
    Posts
    2,127
    ^ Thank you. Exactly what I needed.

  4. #4
    Registered User
    Join Date
    03-12-2013
    Location
    rexburg, idaho
    MS-Off Ver
    Excel 2010
    Posts
    2

    Re: Excel VBA: Can I refer to a textbox as a variable?

    I am trying to do something similar, but it isn't working as shown above

    for i = 1 to 6
    controls("form.textbox" & i).value = formatcurrency(0, 2)
    Next

    I get the "sub or function not defined" error.

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Excel VBA: Can I refer to a textbox as a variable?

    Thorn29,

    Welcome to the Forum.

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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