Home » excel » excel – Hide rows based on variable in Textfield of Userform

excel – Hide rows based on variable in Textfield of Userform

Posted by: admin May 14, 2020 Leave a comment

Questions:

i’m wondering if someone could help me with a little issue.

I have a textbox in an userform where people can add numbers for example 3. I need an macro when a checkbox is on then a few rows needs to be hidden. For example total range is B3 to B50 and if people typ 3 in the textbox B3 + 3rows needs to be visibele and the rest needs to be hidden.

Sub CommandButton1_Click()
If UserForm12.CheckBox2.Value = True Then
Rows("[B4+Karel]:B55").Hidden = True
End Sub

Could some one help me to fix this?

How to&Answers:

if people typ 3 in the textbox B3 + 3rows needs to be visibele and the rest needs to be hidden.

First hide all rows in the range and then show only the relevant rows. I am assuming that the name of the textbox is TextBox1. Change as applicable.

Is this what you are trying? (Untested). Also I have not done any error handling. I am sure you can take care of that?

Rows("3:50").Hidden = True
Rows("3:" & 3 + Val(TextBox1.Text)).Hidden = False

Note: When you are hiding/showing rows, you do not need the column names. You can directly work with row numbers as shown above.