Unhide/Hide rows in excel with a form checkbox - excel

I have a form created in excel which has rows [10:48] hidden and I want to make so that when you click a checkbox rows [10:48] are unhidden. I assigned a macro to the checkbox and using this formula:
Private Sub CheckBox45_Click()
If CheckBox45 = True Then
[10:48].EntireRow.Hidden = False
Else: [10:48].EntireRow.Hidden = True
End If
End Sub
When I click the checkbox nothing happen, but when I unhide the rows and click the checkbox it hides the rows. Which makes me think that only one of the actions is working. Is there a way to fix this?
Thanks in advance for the help.
Don't know if this matters but the form checkbox is in column D row 6

This assumes you are hiding/unhiding rows on Sheet 1 and the checkbox belongs to sheet 1 of the workbook, then:
Private Sub CheckBox30_Click()
If ThisWorkbook.Sheets(1).CheckBoxes("Check Box 30").Value = 1 Then
ThisWorkbook.Sheets(1).Rows("10:48").Hidden = true
Else
ThisWorkbook.Sheets(1).Rows("10:48").Hidden = false
End If
End Sub

Here is another approach.
The statement ws.CheckBoxes("Check Box 30") = 1 will either return TRUE or FALSE which will either hide, or unhide, your target rows.
Private Sub CheckBox30_Click()
Dim ws As Worksheet: Set ws = ThisWorkbook.Sheets("Sheet1")
ws.Range("A10:A48").EntireRow.Hidden = ws.CheckBoxes("Check Box 30") = 1
End Sub

Related

Linked checkboxes in Userform and Worksheet

I have a table format questionnaire to be filled on Userform. There are some sections that to be answered only by check-boxes.Those check-boxes are exist in both worksheet and userform. When the user click the box in the userform it need to be ticked in worksheet as well(I don't know if it is possible).
You can write some If statements to assign the same value to the worksheet checkbox on the click or change event of the UserForm checkbox.
Something like:
Private Sub CheckBox1_Click()
If Me.CheckBox1.Value = True Then
ThisWorkbook.Sheets(1).CheckBox1.Value = True
ElseIf Me.CheckBox1.Value = False Then
ThisWorkbook.Sheets(1).CheckBox1.Value = False
End If
End Sub
1) Link your checkboxes on your sheet to a cell (whichever cell, even on another sheet)
2) Here is the code you could alter to your liking. Make sure to use it as a clickevent on your checkboxes on your userform:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then Range("D3").Value = True
If CheckBox1.Value = False Then Range("D3").Value = False
End Sub
3) Outcome will be like:

Allowing user to hide columns when sheet is locked

I am using the following VBA script to provide users certain permissions when a sheet in a workbook is locked. I cannot figure out how to add a line that would also allow the user to hide and unhide columns. Any suggestions?
Sub EnableOutlining()
'Update 20140603
Dim xWs As Worksheet
Set xWs = Application.ActiveSheet
Dim xPws As String
xPws = Application.InputBox("Password:", xTitleId, "", Type:=2)
xWs.Protect Password:=xPws, Userinterfaceonly:=True
xWs.EnableOutlining = True
xWs.EnableOutlining = True
xWs.EnableAutoFilter = True
xWs.EnableFormatConditionsCalculation = True
End Sub
Going to work on a way to check that last user action was in fact hide / unhide and nothing else. But for now should allow user to hide / unhide.
For columns:
xWs.protect Password:= "1234",AllowFormattingColumns:= true
For rows:
xWs.protect Password:= "1234",AllowFormattingRows:= true
This script should help limit user activity to only adjusting column widths. (lastAction describes hiding columns as adjust width, must be that hiding columns is really just a function that minimizes column width, not some special action)
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim lastAction As String
lastAction = Application.CommandBars("Standard").Controls("&Undo").List(1)
If lastAction <> "Column Width" Then
Application.EnableEvents = False
Application.Undo
MsgBox "PLEASE ONLY HIDE OR UNHIDE COLUMNS"
Application.EnableEvents = True
End If
End Sub
To do this you just need to change the sections your allowing users to work on when locking the sheet. I selected everything but "select locked cells" and it would then work. This meant the code was still hidden in cells I didn't want to be edited but the hide expand button based on my VBA code was working.

It is correct for hide or unhide a control by VBA in Excel 2010?

I have a sheet named "MainSheet" in a Workbook in Excel 2010.
this sheet included an activeX control named "OptionButton1". when the cell's value of "C18" is "2" then this control should be hide.
I wrote below code but does not work.
► a more question: can I have three activeX (Radio Button) related to one cell with three different value like radio button in form control?
Any advice is appreciated. :)
Sub MS_Method()
If Range("C18").Value = 2 Then
ActiveSheet.MainSheet("OptionButton1").Visible = False
ElseIf Range("C18").Value = 1 Then
ActiveSheet.MainSheet("OptionButton1").Visible = True
End If
End Sub
Assuming your ActiveX OptionButton is Named "OptionButton1", the code below (tested) will work:
Option Explicit
Sub MS_Method()
Dim Sht As Worksheet
' modify "MainSheet" to your sheet name (where you have your OptionButton)
Set Sht = ThisWorkbook.Sheets("MainSheet")
If Range("C18").Value = 2 Then
Sht.OLEObjects("OptionButton1").Visible = False
ElseIf Range("C18").Value = 1 Then
Sht.OLEObjects("OptionButton1").Visible = True
End If
End Sub

Excel: combobox and checkbox interference?

I am making an Excel form with some activeX controls and am having a problem incorporating the following functionallity:
I wish for the users to select a number in ComboBox11. If the number be 0, the form changes so, that comboboxes 9 and 10 become disabled (using VBA code) and the table below 'fades out' (using conditional formatting), informing the user not to fill it out.
On the other hand if the user selects a number larger than 0, the form stays as it is. Under the table is a checkbox (checkbox1) used to expand the table (unhiding previously hidden rows) if data needeed to be put in the form is larger than the table size.
The VBA code behind combobox 11 is:
Private Sub ComboBox11_change()
Dim ws As Worksheet
Set ws = Sheets("Form")
If Not Me.ComboBox11.Text = "" Then ws.Range("J24") = CInt(Me.ComboBox11.Text) 'to write integer instead of text into linked cell
If Me.ComboBox11.Value = 0 Then
Me.ComboBox9.Enabled = False
Me.ComboBox10.Enabled = False
If Me.CheckBox1.Value = True Then Me.CheckBox1.Value = False 'if combobox11 is 0 than the table doesn't need to be expanded
Me.CheckBox1.Enabled = False
Else
Me.ComboBox9.Enabled = True
Me.ComboBox10.Enabled = True
Me.CheckBox1.Enabled = True
End If
End Sub
And the code behind CheckBox1 is:
Private Sub CheckBox1_Change()
Dim ws As Worksheet
Set ws = Sheets("Form")
If CheckBox1.Value = False Then ws.Rows("46:71").Hidden = True
If CheckBox1.Value = True Then ws.Rows("46:71").Hidden = False
End Sub
So, if I select 0 in combobox11 the result is:
So far so good. But if I select something larger than 0, say 1 in combobox11 and then try to expand the table by clicking on the checkbox1, the table expands, but I get an error message:
Run-time error '1004': Not possible to set properties: enabled class:
OLEObject
(not sure about the exact error text, since I'm not using English MSOffice)
When pressing the Debug button, the following line in Sub ComboBox11_Change() lights up:
Me.ComboBox9.Enabled = True
The Strange thing is, that this error does not appear when the combobox11 is left blank ( a value is not selected).
I have no idea why the checkbox would interact with the other comboboxes. I am bewildered and any help would be much appreciated.
To reproduce this:
Have a sheet like this:
A1:A6 is the ListFillRange of the ComboBox.
Then hiding any row between 1:6 using VBA code in CheckBox1_Change() will throw the error.
Private Sub CheckBox1_Change()
If CheckBox1.Value = False Then Me.Rows("7:13").Hidden = True
If CheckBox1.Value = True Then Me.Rows("7:13").Hidden = False
'If CheckBox1.Value = False Then Me.Rows("6:13").Hidden = True 'ERROR
'If CheckBox1.Value = True Then Me.Rows("6:13").Hidden = False 'ERROR
End Sub
Private Sub ComboBox1_Change()
If Me.ComboBox1.Value = 0 Then
If Me.CheckBox1.Value = True Then Me.CheckBox1.Value = False
Me.CheckBox1.Enabled = False
Else
Me.CheckBox1.Enabled = True
End If
End Sub
If we create a name named "list" which is related to a whole column like this:
and use this "list" as the ListFillRange of the ComboBox, then the error occurs ever if rows in this sheet were hidden from code. This is independent on whether the hidden rows are within the real active "list" rows 1-6 or not.
If we not reference a whole column in the name but for example only rows 1-10 like:
=INDEX(Sheet1!$A$1:$A$10;1):INDEX(Sheet1!$A$1:$A$10;6)
then the error only occurs if the code hides rows from 1 to 10 but not if it hides rows from 11 upwards.
Edit:
To continue my example:
Moved the numbers from Sheet1!A:A to Sheet2!A:A and created a named range "list" related to
=Sheet2!$A$1:INDEX(Sheet2!$A$1:$A$40;COUNTIF(Sheet2!$A$1:$A$40;"<>"))
Then the following code to set Sheet1.ComboBox1.ListFillRange to "list" will work if it is within the Sheet2 class module:
Private Sub Worksheet_Change(ByVal Target As Range)
ThisWorkbook.Worksheets(1).ComboBox1.ListFillRange = "list"
End Sub
It will produce the error if setting the ListFillRange is tried from within the Sheet1 class module.
Because comments are too short to explain whad I did to solve the issue, I am posting this answer.
First of all thanks to Axel Richter, who took his time to elaborate on my problem.
The Combobox11 was filled with a generated ListFillRange in the name manager:
The described error stopped appearing, as soon as I changed the ListFillRange. At first I tried a simple alternative: "=list!AU1:AU40" Although I don't understand the problem it is now solved!
Afterwards I used the following code to create a dynamic list for combobox11.
Dim zadnji As Integer
zadnji = Sheets("Form").Range("T9").Value + 1
Me.OLEObjects("combobox11").ListFillRange = "=lists!AU1:AU" & zadnji

VBA + checkboxes + autofilter

I have a list in excel with an autofilter and where every row has a checkbox.
I have made a button with a macro to select all the checkboxes. This works fine. But when I filter my rows my 'select all' has to select only the visible checkboxes.
With my code it still selects all the checkboxes. Has anybody an idea to solve this?
My code:
Sub SelectAll()
Dim chk As CheckBox
If Worksheets("Summary").FilterMode = True Then
MsgBox "Filter mode is on"
Else
MsgBox "Filter mode is off"
For Each chk In Worksheets("Summary").CheckBoxes
chk.Value = Checked
Next
End If
End Sub
Thanks in advance
I'm taking it that your checkboxes are being hidden by the filtering.
This might help, inside your loop:
Dim chkRng As Range
Set chkRng = chk.TopLeftCell
Let addr = chkRng.Address ' for debugging to verify the cell the checkbox is associated with
Dim visr As Range
Set visr = chkRng.SpecialCells(xlCellTypeVisible)
Set ans = Intersect(visr, chkRng)
If Not ans Is Nothing Then
MsgBox ("visible")
End If

Resources