ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Gotta be some more Efficient code (https://www.excelbanter.com/excel-programming/402044-gotta-some-more-efficient-code.html)

Jennifer

Gotta be some more Efficient code
 
I have 10 text boxes is there a more efficient way to write this code.
Eventually there could be more textboxes and labels. Loop? Thank you so much.

If Len(Trim(Label1.Caption)) 0 Then
TextBox1.Visible = True
Else
TextBox1.Visible = False
End If
If Len(Trim(Label2.Caption)) 0 Then
TextBox2.Visible = True
Else
TextBox2.Visible = False
End If
--
Thank you,

Jennifer

Gary Keramidas

Gotta be some more Efficient code
 
this worked for me

Private Sub CommandButton1_Click()
Dim i As Long

For i = 1 To 10
If Len(Trim(Me.Controls("label" & i).Caption)) 0 Then
Me.Controls("textbox" & i).Visible = True
Else
Me.Controls("textbox" & i).Visible = False
End If
Next
End Sub

--


Gary


"Jennifer" wrote in message
...
I have 10 text boxes is there a more efficient way to write this code.
Eventually there could be more textboxes and labels. Loop? Thank you so much.

If Len(Trim(Label1.Caption)) 0 Then
TextBox1.Visible = True
Else
TextBox1.Visible = False
End If
If Len(Trim(Label2.Caption)) 0 Then
TextBox2.Visible = True
Else
TextBox2.Visible = False
End If
--
Thank you,

Jennifer




Greg Wilson

Gotta be some more Efficient code
 
Dim i As Integer
For i = 1 To 10
Controls("TextBox" & i).Visible = (Len(Controls("Label" & i).Caption) 0)
Next

I assume you don't need the Trim function because *you* will be creating the
captions and can avoid captions with only blank spaces ?

Greg


"Jennifer" wrote:

I have 10 text boxes is there a more efficient way to write this code.
Eventually there could be more textboxes and labels. Loop? Thank you so much.

If Len(Trim(Label1.Caption)) 0 Then
TextBox1.Visible = True
Else
TextBox1.Visible = False
End If
If Len(Trim(Label2.Caption)) 0 Then
TextBox2.Visible = True
Else
TextBox2.Visible = False
End If
--
Thank you,

Jennifer


Nigel[_2_]

Gotta be some more Efficient code
 
This works for a UserForm but does it work for activeX controls on the
worksheet?

--

Regards,
Nigel




"Greg Wilson" wrote in message
...
Dim i As Integer
For i = 1 To 10
Controls("TextBox" & i).Visible = (Len(Controls("Label" & i).Caption)
0)
Next

I assume you don't need the Trim function because *you* will be creating
the
captions and can avoid captions with only blank spaces ?

Greg


"Jennifer" wrote:

I have 10 text boxes is there a more efficient way to write this code.
Eventually there could be more textboxes and labels. Loop? Thank you so
much.

If Len(Trim(Label1.Caption)) 0 Then
TextBox1.Visible = True
Else
TextBox1.Visible = False
End If
If Len(Trim(Label2.Caption)) 0 Then
TextBox2.Visible = True
Else
TextBox2.Visible = False
End If
--
Thank you,

Jennifer



Greg Wilson

Gotta be some more Efficient code
 
I assume that was a rhetorical question <g

Dim i As Integer
For i = 1 To 10
With Me.OLEObjects("Label" & i).Object
Me.OLEObjects("TextBox" & i).Visible = (Len(.Caption) 0)
End With
Next


"Nigel" wrote:

This works for a UserForm but does it work for activeX controls on the
worksheet?

--

Regards,
Nigel




"Greg Wilson" wrote in message
...
Dim i As Integer
For i = 1 To 10
Controls("TextBox" & i).Visible = (Len(Controls("Label" & i).Caption)
0)
Next

I assume you don't need the Trim function because *you* will be creating
the
captions and can avoid captions with only blank spaces ?

Greg


"Jennifer" wrote:

I have 10 text boxes is there a more efficient way to write this code.
Eventually there could be more textboxes and labels. Loop? Thank you so
much.

If Len(Trim(Label1.Caption)) 0 Then
TextBox1.Visible = True
Else
TextBox1.Visible = False
End If
If Len(Trim(Label2.Caption)) 0 Then
TextBox2.Visible = True
Else
TextBox2.Visible = False
End If
--
Thank you,

Jennifer



Nigel[_2_]

Gotta be some more Efficient code
 
It was <g and thanks for the solution.

--

Regards,
Nigel




"Greg Wilson" wrote in message
...
I assume that was a rhetorical question <g

Dim i As Integer
For i = 1 To 10
With Me.OLEObjects("Label" & i).Object
Me.OLEObjects("TextBox" & i).Visible = (Len(.Caption) 0)
End With
Next


"Nigel" wrote:

This works for a UserForm but does it work for activeX controls on the
worksheet?

--

Regards,
Nigel




"Greg Wilson" wrote in message
...
Dim i As Integer
For i = 1 To 10
Controls("TextBox" & i).Visible = (Len(Controls("Label" &
i).Caption)
0)
Next

I assume you don't need the Trim function because *you* will be
creating
the
captions and can avoid captions with only blank spaces ?

Greg


"Jennifer" wrote:

I have 10 text boxes is there a more efficient way to write this code.
Eventually there could be more textboxes and labels. Loop? Thank you
so
much.

If Len(Trim(Label1.Caption)) 0 Then
TextBox1.Visible = True
Else
TextBox1.Visible = False
End If
If Len(Trim(Label2.Caption)) 0 Then
TextBox2.Visible = True
Else
TextBox2.Visible = False
End If
--
Thank you,

Jennifer





All times are GMT +1. The time now is 06:13 PM.

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