Results 1 to 3 of 3

Thread: VBA textbox name property

  1. #1
    Join Date
    Feb 2007
    Location
    Brisbane
    Posts
    4,497
    Total Downloaded
    0

    VBA textbox name property

    Guru needed

    I've got an Excel form with 10 textboxes named tbBox1, tbBox2, etc.

    In my VBA code I want to run a loop that looks through a sheet and when it finds a value, populate that value into tbBox1, keep looking and when it finds then next value populate tbBox2, etc, etc.

    What I'm after is a way to dynamically change the textbox I'm referring to instead of individually naming them.

    ie
    For i= 1 to 10
    MyName = "tbBox" & i
    MyName.Value = Worksheets("Stock List").Cells(j, Stock_Column).Value


    Of course, this doesn't work. Any ideas please?

  2. #2
    dmdigital's Avatar
    dmdigital is offline OldBushie Vendor

    Gold Subscriber
    Join Date
    Jun 2006
    Location
    Arnhem Land, NT
    Posts
    8,492
    Total Downloaded
    0
    Yes there are a couple of ways, either use a switch/case statement based on the value of i to direct to the correct object or use the translate object name (can't remember the exact syntax) based on "tbBox" & i to load the correct textbox for storing to.

    Don't have Excel at home now only at work, so can't look it up.
    MY15 Discovery 4 SE SDV6

    Past: 97 D1 Tdi, 03 D2a Td5, 08 Kimberley Kamper, 08 Defender 110 TDCi, 99 Defender 110 300Tdi[/SIZE]

  3. #3
    Join Date
    Feb 2007
    Location
    Perth
    Posts
    3,919
    Total Downloaded
    0
    Assuming the only text boxes on your form are the 10 text boxes in question then loop through the controls collection on your form

    j=0
    For Each cntl in Me.Controls
    If TypeName(cntl) = "TextBox" Then
    cntl.Value = Worksheets("Stock List").Cells(j, Stock_Column).Value
    j=j+1
    End If
    Next cntl
    Last edited by Ferret; 24th October 2008 at 05:50 PM. Reason: syntax
    2024 RRS on the road
    2011 D4 3.0 in the drive way
    1999 D2 V8, in heaven
    1984 RRC, in hell

Bookmarks

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 AULRO.com ONLY!
Search All the Web!