ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Easy One - Syntax for variables in TextBox & Label names (https://www.excelbanter.com/excel-programming/402998-easy-one-syntax-variables-textbox-label-names.html)

Marty

Easy One - Syntax for variables in TextBox & Label names
 
Hello:

What is the correct syntax for including a variable (in this case "i") in
the name of a TextBox and/or Label?

I have a list of TextBoxes and Labels on a userform and I want to program a
reset button to change what is displayed back to the defaults. I need
something like this:

Private Sub CommandButton8_Click()
For i = 1 To 13
TextBox(i).Text = "0"
Label(i).Caption = "N/A"
Next i
End Sub

I'm getting a Sub or Function not defined error, and I know it's because the
syntax is wrong. Help is appreciated.

Thanks,
MARTY


JLGWhiz

Easy One - Syntax for variables in TextBox & Label names
 
Could be looking for the parent to the textbox and label. i.e.
UserForm1.TextBox(i) and UserForm1.Label(i), etc.

"Marty" wrote:

Hello:

What is the correct syntax for including a variable (in this case "i") in
the name of a TextBox and/or Label?

I have a list of TextBoxes and Labels on a userform and I want to program a
reset button to change what is displayed back to the defaults. I need
something like this:

Private Sub CommandButton8_Click()
For i = 1 To 13
TextBox(i).Text = "0"
Label(i).Caption = "N/A"
Next i
End Sub

I'm getting a Sub or Function not defined error, and I know it's because the
syntax is wrong. Help is appreciated.

Thanks,
MARTY


merjet

Easy One - Syntax for variables in TextBox & Label names
 
For i = 1 To 13
Controls("TextBox" & i).Value = "0"
Controls("Label" & i).Caption = "N/A"
Next i

Hth,
Merjet

Marty

Easy One - Syntax for variables in TextBox & Label names
 
Nope. I tried this as a test based on your respone:

Private Sub CommandButton8_Click()
For i = 9 To 13
DATAENTRY.TextBox(i).Text = "0"
Next i
End Sub

and now I get a "method or data member not found" error. The ".TextBox"
part is highlighted while the error message is up, so apparently it doesn't
like "TextBox" without an integer suffix. This is what I'm trying to do:
make a variable suffix.

Other ideas?

"JLGWhiz" wrote:

Could be looking for the parent to the textbox and label. i.e.
UserForm1.TextBox(i) and UserForm1.Label(i), etc.

"Marty" wrote:

Hello:

What is the correct syntax for including a variable (in this case "i") in
the name of a TextBox and/or Label?

I have a list of TextBoxes and Labels on a userform and I want to program a
reset button to change what is displayed back to the defaults. I need
something like this:

Private Sub CommandButton8_Click()
For i = 1 To 13
TextBox(i).Text = "0"
Label(i).Caption = "N/A"
Next i
End Sub

I'm getting a Sub or Function not defined error, and I know it's because the
syntax is wrong. Help is appreciated.

Thanks,
MARTY


Incidental

Easy One - Syntax for variables in TextBox & Label names
 
Hi

the code below is one way to do it

Option Explicit
Dim Ctrl As Control
Dim i As Integer
Private Sub CommandButton1_Click()

For i = 1 To 13

Set Ctrl = Me.Controls("TextBox" & i)

Ctrl.Value = ""

Set Ctrl = Me.Controls("Label" & i)

Ctrl.Caption = ""

Next

End Sub

Hope this helps

Steve


Marty

Easy One - Syntax for variables in TextBox & Label names
 
Perfect. Thanks.

"merjet" wrote:

For i = 1 To 13
Controls("TextBox" & i).Value = "0"
Controls("Label" & i).Caption = "N/A"
Next i

Hth,
Merjet



All times are GMT +1. The time now is 08:32 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com