Checkbox linked with VBA to hide a number of rows - vba

I am trying to hide and unhide a number of rows when a checkbox is ticked or otherwise. I have created the below code, which is working to hide the rows when the checkbox is unticked, however when I tick it again the rows are not visible. Can anyone assist?
Private Sub CheckBox3_Click()
'unhide all rows
Rows("1:138").EntireRow.Hidden = False
'Hide investment Advice, if checkbox is not marked
If CheckBox3 = False Then
Rows("22:29").EntireRow.Hidden = True
Else
Rows("22:29").EntireRow.Hidden = False
End If
End Sub

See https://support.microsoft.com/en-us/office/how-to-use-the-forms-controls-on-a-worksheet-in-excel-e7e33c0c-f080-4443-b565-d21b1bdbcf43
Set the cell reference for the checkbox as a G2 address or as a range name, for example VISIBLE
When a checkbox event occurs, process the value in the cell
Sub CheckBox3_Click()
Rows("22:29").Hidden = Not [Visible] ' VISIBLE i have set as name for G2 range
End Sub

Related

VBA "All" checkbox with listbox deselect loop issues

I have a check box that is used for "ALL" (selected by default) selections in a muliselect listbox that is deselected when an item in the listbox is selected. I also coded it so when "ALL" is selected, then it clears the listbox selections and checks the box. It ends up looping through the different subs and makes it annoying for the user.
For instance, when I click an item in the listbox it selects that value, then deselects the checkbox. Since the checkbox is deselected, it goes back through the listbox and deselects the selected item. It loops between the two subs a couple times and ends up only working correctly half the time.
Can I prevent entering the other sub?
Is there better logic so it won't loop as it does?
or maybe an better method to achieve this?
Multiselect listbox:
Private Sub Mkts_Change()
If Me.cheMkts.Value = True Then
Me.cheMkts.Value = False
End If
End Sub
Checkbox:
Private Sub cheMkts_Click()
Dim i As Integer
For i = 0 To Mkts.ListCount - 1
If Me.Mkts.Selected(i) = True Then
Me.Mkts.Selected(i) = False
End If
Next
End Sub
What about adding an If statement around your cheMtks_Click()?
This way when your code deselects it automatically it shouldn't trigger the loop.
Private Sub cheMkts_Click()
If Me.cheMkts.Value = True Then
Dim i As Integer
For i = 0 To Mkts.ListCount - 1
If Me.Mkts.Selected(i) = True Then
Me.Mkts.Selected(i) = False
End If
Next
End If
End Sub
Thanks for your help, Ruben. That corrects the error on the one end, but I am still having issues on the other side. When I have a selection and click the "ALL" box it deselects the check.
I came up with this code, which works beautifully in combo to your suggestion, but only when I have one item selected. If there is anything more, then it still goofs up. Figured I would post to see if you or someone else could advise a solution for multiple selections.
Private Sub Mkts_Change()
Dim i As Integer, count As Integer
For i = 0 To Mkts.ListCount - 1
If Me.Mkts.Selected(i) = False Then
count = count + 1
End If
Next
If Me.cheMkts.Value = True And count <> Mkts.ListCount Then
Me.cheMkts.Value = False
End If
End Sub

Hiding the data in rows -Excel VBA

I have three rows.I want to hide the data in those rows and display those rows in different color.I tried searching but only found Entirerow.hidden,which hides the row number as well. Is it possible to only hide the data in the rows and display it using some other color?
The below code will change the background color to Yellow for rows 1 to 3.
Sub ChangeBackColorForSpecificRows()
Rows("1:3").Interior.Color = 65535
End Sub
Change the Rows and Interior Color to suit your requirement.
Edit:-
formupahidden set to true not working neither formatting it to locked
and hidden ,is hiding the content of formula bar – Sunaina
Copy the below code and do right click on sheet tab and select view code and paste it.
Close the VBA window (Alt+Q to close VBA window) and return to that sheet and check.
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Rows("1:3")) Is Nothing Then
If Application.DisplayFormulaBar Then Application.DisplayFormulaBar = False
Else
If Not Application.DisplayFormulaBar Then Application.DisplayFormulaBar = True
End If
End Sub

How to fire on unhiding rows or columns

Some users hide columns/rows and forget to unhide them before saving a workbook. I want to catch that with unhide all columns/rows on the save event with
Sub ReInvisible()
ThisWorkbook.Worksheets(1).UsedRange.EntireRow.Hidden = False
ThisWorkbook.Worksheets(1).UsedRange.EntireColumn.Hidden = False
End Sub
this works fine but I would like to inform the user that all hidden columns/rows are now visible. Now I am looking for a way to trigger on unhiding a column/row so as soon as at least one column or row is made visible I want to fire a messagebox.
In VB.NET I would try to write my own event but in VBA I do not know how I can do a workaround. Does anyone have an idea?
Something like this should do it:
Sub ReInvisible()
Dim lVisColCount As Long
Dim lVisRowCount As Long
With ThisWorkbook.Worksheets(1).UsedRange
lVisColCount = .Rows(1).SpecialCells(xlCellTypeVisible).Count
lVisRowCount = .Columns(1).SpecialCells(xlCellTypeVisible).Count
.EntireRow.Hidden = False
.EntireColumn.Hidden = False
If .Rows(1).SpecialCells(xlCellTypeVisible).Count <> lVisColCount Then MsgBox "Columns unhidden"
If .Columns(1).SpecialCells(xlCellTypeVisible).Count <> lVisRowCount Then MsgBox "Rows unhidden"
End With
End Sub

VBA Excel Toggle Button: With 10 rows, how do I unhide each row separately and not in a group?

I'm working in VBA and Excel 2007 with active control toggle button, which I'm trying to figure out how to get to function the way I need it to. Will someone please help me out?
This works for only unhiding a single row at a time for two hidden rows:
Private Sub ToggleButton1_Click()
If ToggleButton1 Then
Rows(76).EntireRow.Hidden = False
Else
Rows(77).EntireRow.Hidden = False
End If
End Sub
This does not work for only unhiding a single row at a time for more than two hidden rows:
Private Sub ToggleButton1_Click()
If ToggleButton1 Then
Rows(76).EntireRow.Hidden = False
Else
Rows(77).EntireRow.Hidden = False
Else
Rows(78).EntireRow.Hidden = False
End If
End Sub
What do I need to do to get this to work? This is all I need the toggle button to do. Each row has identical information (text fields, field names, etc.), but I need each row to only become visible upon clicking just one toggle button. I know multiple toggle buttons will works like a breeze, but I really am wanting to just use one toggle button to unhide each row, one at a time. By default, the rows will be hidden first, too.
You can use a counter and then count down and you should be able to do what you are looking for. In this example if the counter is 0 we hide the rows 1-3 and then show each row untill we are back to 0 and then hide them again when we click the button.
Dim counter As Integer
Private Sub ToggleButton1_Click()
If counter = 0 Then
Rows(1).EntireRow.Hidden = True
Rows(2).EntireRow.Hidden = True
Rows(3).EntireRow.Hidden = True
counter = 3
Else
Rows(counter).EntireRow.Hidden = False
counter = counter - 1
End If
End Sub
Hope it helps
//KH
Try this:
Private Sub ToggleButton1_Click()
Dim rng As Range
Dim myrow As Range
Set rng = Me.Rows("75:85")
If Me.ToggleButton1 Then
For Each myrow In rng
If Not myrow.Hidden Then
myrow.Hidden = True
If myrow.row = 85 Then
myrow.Offset(-10, 0).Hidden = False
Else
myrow.Offset(1, 0).Hidden = False
End If
Exit For
End If
Next
End If
End Sub
This hides and unhides Rows76-85 simultaneously.
You start with all 10 Rows hidden.
After 1st click, Row 76 will appear and so on.
All will be hidden again after Row 85 was shown.
I used 10 rows based on your question title, adjust it to suit your needs.
Edit1:
Private Sub ToggleButton1_Click()
Dim rng As Range
Dim myrow As Range
Set rng = Me.Rows("76:85")
If Me.ToggleButton1 Then
For Each myrow In rng
If myrow.Hidden Then
myrow.Hidden = False
Exit Sub
End If
Next
rng.Hidden = True
End If
End Sub
Above will work as you want it.
Although you already accepted an answer, I still would want to correct my code.
At least for you and others reference.

Select a option from a filtered cell

I am new in VBA so i am unable to do this.
I have 21 sheets in a workbook. I want to select a cell in 3rd sheet (which contains a Pivot Table) which I am able to do. This cell B3 contains a filter and I can select from the filtered drop menu on how to sort my data. It contains whether I want to filter by first name OR last name OR all.
My usual routine is first to select by first name, then copy the filtered data and paste it on another sheet. Then come back to the same sheet and filter by last name and then copy the filtered data and paste it on the sheet I pasted the earlier data.
What I need help with is the following:
If any or all check boxes are selected then deselect them.
Select the first_name checkbox in the filter drop down
De-select the first_name box and select the last_name box
Finally deselect last_name and then select all checkbox
I have used the following code
Public Sub Open_Sheet3()
Workbooks("MASTER.xlsx").Activate
ActiveWorkbook.Sheets("Sheet3").Activate
ActiveSheet.PivotTables("PivotTable1").PivotFields("Technology").CurrentPage = _
"(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Technology")
.PivotItems("Mobility").Visible = False
.PivotItems("(blank)").Visible = False
.PivotItems("Enterprise Messaging Tech").Visible = False
End With
End Sub
This code should do what you want. Just replace the names of worksheet/pivot table/pivot field and fill in the copyStuff sub.
Private Sub YourProcedure()
SelectItem "first_name"
CopyStuff
SelectItem "last_name"
CopyStuff
SelectItem "all" 'in case you have an element called "all"
CopyStuff
SelectAll 'In case you mean the "(All)" 'element', i.e. include everything
CopyStuff
End Sub
Private Sub SelectItem(strItemName As String)
Dim i As Integer
'Change to your worksheet/pivot talbe/pivot field name!
With Worksheets("Sheet 1").PivotTables("PivotTable1").PivotFields("a")
.PivotItems(1).Visible = True 'at least one item always needs to be visible
For i = 2 To .PivotItems.Count
.PivotItems(i).Visible = False
Next
.PivotItems(strItemName).Visible = True
If .PivotItems(1).Name <> strItemName Then .PivotItems(1).Visible = False
End With
End Sub
Private Sub SelectAll()
Dim i As Integer
'Change to your worksheet/pivot talbe/pivot field name!
With Worksheets("Sheet 1").PivotTables("PivotTable1").PivotFields("a")
For i = 1 To .PivotItems.Count
.PivotItems(i).Visible = True
Next
End With
End Sub
Private Sub CopyStuff()
'Your code goes here
End Sub
Some explanation:
If you want to deselect items of a pivot field, you need to make sure that there's at least on item selected at any time. Therefore you cannot unselect all and then select your desired item - but rather select the first item, unselect all other, select your item and unselect the first item unless it is your item. That's what SelectItem is doing