Show or Hide Textbox advice

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
Afternoon.
I have a userform as shown below in screen shot below.
I have also supplied code in use.

My goal is the following.

When userform opens i would like to see the Label "HONDA TAG NUMBER" & the TextBoxes 1-8 "TOP ROW" Only
The Label "BITING FOR CUTTING" & the TextBox 9-16 "BOTTOM ROW" should not be visible.
I enter numerical value in TextBoxes 1-8

I then press the command button "RUN TOOL"
Now i would like the Label "HONDA TAG NUMBER" & TextBoxes 1-8 to hide & "BITING FOR CUTTING" & TextBox 9-16 to be visible.
TextBoxes 1-9 take there value from TextBoxes 1-8

Currently both labels & all 16 TextBoxes are shown when userform opens.
I am able to enter values in TextBox 1-8
I use the RUN TOOL button & bothe labels are still shown & all TextBoxes show values.



EaseUS_2023_11_12_14_13_24.jpg


Rich (BB code):
Private Sub ClearCells_Click()
Label1.Visible = True

Dim x As Integer
For x = 1 To 16
Me.Controls("textbox" & x).Value = ""
Next x

End Sub

Private Sub RunCode_Click()
TextBox9.Value = TextBox8.Value
TextBox10.Value = TextBox7.Value
TextBox11.Value = TextBox6.Value
TextBox12.Value = TextBox5.Value
TextBox13.Value = TextBox4.Value
TextBox14.Value = TextBox3.Value
TextBox15.Value = TextBox2.Value
TextBox16.Value = TextBox1.Value


End Sub

Private Sub UserForm_Initialize()

    TextBox1.SetFocus
    
    '-------------------------------
    Me.StartUpPosition = 0
    Me.Top = Application.Top + 250  ' MARGIN FROM TOP OF SCREEN
    Me.Left = Application.Left + Application.Width - Me.Width - 200 ' LEFT / RIGHT OF SCREEN
   
    '-------------------------------
End Sub
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Start by putting the form in design view. Set the visible property for textboxes 9 to 16 to false. Makes more sense to open the form as you want to see it at that time.
You may want to use validation, because without it, if you click the button the other textboxes will show regardless if they are all filled in or not. This can be quite easy if when in design view you make use of the Tag property for the textboxes. I'd suggest you make that the same value for each set of 8. The tag can be anything, but let's use "1" for the first 8 for illustration and "2" for the other 8. Don't actually use quotes in the Tag property. Then your code would loop over the textboxes and if its tag = 1 then check if it has a value. If any don't, message and exit, else loop over the controls again and if the tag = 2, make it visible and make the others not visible.

That's all a bit generalized and I find that once an idea is put into action things change slightly. I might put the loop in another sub and just reverse the visibility of the controls, such as
VBA Code:
Dim ctl As Control
For Each ctl in userform.Controls
   If ctl.Tag = "1" Or ctl.Tag = "2" Then  ctl.Visible = Not ctl.Visible
Next
Note: you could also have made this easier if textbox 1 went into 9, 2 into 10, 3 into 11 and so on. Then it's simply a matter of looping and adding 8 to the numerical part of the textbox name.
 
Upvote 0
Im sorry but your replies atre to advanced for me.
Not only can i not work out the issue i have i then try to understand what you wrote.
 
Upvote 0
You cannot make the form design changes at least, for a start?
 
Upvote 0
Im sorry but your replies atre to advanced for me.

Hi,
Not tested but see if this update to your codes will do what you want

Place ALL codes in your Userform code page

Rich (BB code):
Private Sub ClearCells_Click()
    EnterData True
End Sub

Private Sub RunCode_Click()
    EnterData
End Sub


Sub EnterData(Optional ByVal HideTextbox As Boolean)
    Dim i             As Long
    Dim Box(1 To 2)   As MSForms.TextBox
    
    For i = 1 To 8
        Set Box(1) = Me.Controls("TextBox" & (9 - i))
        Set Box(2) = Me.Controls("TextBox" & i + 8)
        Box(1).Visible = HideTextbox: Box(2).Visible = Not HideTextbox
        If Not HideTextbox Then Box(2).Value = Box(1).Value Else _
        Box(1).Value = "": Box(2).Value = ""
        Erase Box
    Next i
    'Show/Hide Labels
    Me.Label1.Visible = HideTextbox
    Me.Label2.Visible = Not HideTextbox
    
End Sub

Private Sub UserForm_Initialize()
    EnterData True
    
    'other code
    
    '-------------------------------
    Me.StartUpPosition = 0
    Me.Top = Application.Top + 250  ' MARGIN FROM TOP OF SCREEN
    Me.Left = Application.Left + Application.Width - Me.Width - 200 ' LEFT / RIGHT OF SCREEN
   
    '-------------------------------
End Sub

You will need to change the Label Names shown in BOLD as required

Note: there is no data entry checking for blank textboxes

Dave
 
Upvote 0
I have now done this if i understand correctly BUT i see no change.

When i open the form i only need to see Label1 & Textboxes 1 to 8

I then use command button RUN TOOL
"Apart from the values moving from Textboxes 1 to 8 / 9 to 16"
I expect Label1 & Texboxes 1 to 8 HIIDEN
I expect Textboxes 1 to 8 Hidden


I expect Label2 & Texboxes 9 to 16 SHOWN
At present all labels & textboxes are shown.




Rich (BB code):
Private Sub ClearCells_Click()
Label1.Visible = True

Dim x As Integer
For x = 1 To 16
Me.Controls("textbox" & x).Value = ""
Next x
TextBox1.SetFocus

End Sub
Private Sub RunCode_Click()
TextBox9.Value = TextBox8.Value
TextBox10.Value = TextBox7.Value
TextBox11.Value = TextBox6.Value
TextBox12.Value = TextBox5.Value
TextBox13.Value = TextBox4.Value
TextBox14.Value = TextBox3.Value
TextBox15.Value = TextBox2.Value
TextBox16.Value = TextBox1.Value


End Sub

Private Sub EnterData(Optional ByVal HideTextbox As Boolean)
    Dim i             As Long
    Dim Box(1 To 2)   As MSForms.TextBox
    
    For i = 1 To 8
        Set Box(1) = Me.Controls("TextBox" & (9 - i))
        Set Box(2) = Me.Controls("TextBox" & i + 8)
        Box(1).Visible = HideTextbox: Box(2).Visible = Not HideTextbox
        If Not HideTextbox Then Box(2).Value = Box(1).Value Else _
        Box(1).Value = "": Box(2).Value = ""
        Erase Box
    Next i
    'Show/Hide Labels
    Me.Label.Visible = HideTextbox
    Me.Labe2.Visible = Not HideTextbox
    
End Sub

Private Sub UserForm_Initialize()

    TextBox1.SetFocus
    
    '-------------------------------
    Me.StartUpPosition = 0
    Me.Top = Application.Top + 250  ' MARGIN FROM TOP OF SCREEN
    Me.Left = Application.Left + Application.Width - Me.Width - 200 ' LEFT / RIGHT OF SCREEN
   
    '-------------------------------
End Sub
 
Upvote 0
I have now done this if i understand correctly BUT i see no change.

Sorry you have not followed my instruction

Place ALL codes in your Userform code page

If you look again at solution I posted there are 4 Codes - you have only copied 1 code

You need to delete all existing codes in your userform that have the same name as codes I have published & then copy all 4 codes (without any changes) in post 5# & place them in your userforms code page

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,224,813
Messages
6,181,109
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top