for loop

  toga 09:50 03 Apr 2010
Locked

Hello, I am using a form in excell 97 and I am trying to use a for loop (via a user form) to loop through a number of text boxes; altering the value of each text box.

dim textbox as control
dim i as integer

i = 0

Private Sub Command1_Click()
for each textbox in cintrols
me.textbox(i).text = i
i = i + 1
next
End Sub

The textboxes are named txtbox1, txtbox2 and txtbox3. I was trying to use i to alter the name. I get a message stating that an object is not declared.

I have also looked to see if I could set the index property to 1, 2 and 3 respectively but can't find where to do this.

Can anyone help?

Many thanks

  VoG II 10:15 03 Apr 2010

Not really sure what you are trying to do. If you are trying to set the textboxes' values try


Private Sub CommandButton1_Click()
Dim ct As Control, i As Integer
For Each ct In Me.Controls
If TypeName(ct) = "TextBox" Then
i = i + 1
ct.Value = i
End If
Next ct
End Sub

  toga 10:22 04 Apr 2010

That is exactly what I was truing to do

Thanks very much

PS I was using help on MS web site, a visual basic book and an old excell book but still could not solve this issue. What, if any, material are you using to learn about this kind of stuff.

Many thanks

  OTT_B 11:21 04 Apr 2010

"What, if any, material are you using to learn about this kind of stuff"

I've not used (or seen) this book, but i've got, and still sometimes use, the Excel 2000 version as a bible.
click here

  lotvic 12:54 04 Apr 2010

"What, if any, material are you using to learn about this kind of stuff."
VoG's Excel Tips and Tricks click here

  VoG II 19:06 04 Apr 2010

I'm not. I rarely need to consult a reference work these days. I doubt that you'd find a boilerplate example of what you wanted to do anywhere. The key is practise, practise, practise ... or as one of the MVPs on MrExcel.com has as his signature 'Try, try and try again - the way of the coder'. In my case this is aided by early nights and a fish diet :o)

You may find click here of interest. It will certainly help you to learn but, as far as I know, doesn't contain an example of *exactly* what you were trying to do.

Keep Excelling!

  toga 10:05 07 Apr 2010

Many thanks for all the replies and help, they were all much appreciated.

This thread is now locked and can not be replied to.

Elsewhere on IDG sites

OnePlus 5 review

See the work of famous artists playing with toys

iPad Pro 10.5in (2017) review

Comment faire une capture d’écran sur un Mac ?