VBA "All" checkbox with listbox deselect loop issues - vba

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

Related

Checking and Unchecking Checkboxes in Access

I have a form in MS Access with multiple checkboxes which I want to use to fill up one textbox. If one of the checkboxes gets unchecked, I want its value to be deleted from the textbox without deleting other values. I'm new at using Access and coding in VBA (been reading ebooks for the past 3 weeks) and although I've tried to do research online it's been difficult for me to find the right code.
This is what I have so far:
First code found
Private Sub cb_click()
If Me.cb1 = True Then
Me.txtComentarios.Value = "INACTIVO;"
Else
Me.txtComentarios.Value = Null
End If
End Sub
Second code found
Private Sub cb2_Click()
If Me.cb2 = -1 Then
Me.[txtComentarios] = [txtComentarios] & "DISCREPANCIA"
Else
Me.[txtComentarios] = ""
End If
Exit Sub
End Sub
Also I would like for the checkboxes to fill the textbox in the same order the chechboxes are displayed.
Ex.
cb1; cb2; cb3
If, cb2 gets unchecked and its value gets deleted, I should have "cb1; cb3" but if I re-check cb2 I should get "cb1; cb2; cb3" again.
I just hope someone could guide me in. Thank you in advance.
Luz
You don't need events for each checkbox. Just create one procedure, which creates full text depending on checkboxes state and puts this text to the textbox. To call this function after each click on checkbox set After Update property of all checkboxes to =MyFunctionToUpdateTextbox instead of [Event Procedure]
Private Function MyFunctionToUpdateTextbox()
Dim strText As String
If Me.cb1 = True Then
strText = strText & "INACTIVO;"
End If
If Me.cb2 = True Then
strText = strText & "DISCREPANCIA;"
End If
If Me.cb3 = True Then
strText = strText & "Text for cb3"
End If
Me.txtComentarios = strText
End Function

VBA Checkbox to Listbox (uncheck option to remove array)

I am struggling with a simple thing and cannot resolve it. I have a userform that user can populate from a textbox manually. I decided to add a checkbox as well to allow the user to populate the same listbox with a specific list of items. To do it,I made a simple checkbox with array. It works perfectly fine. But obviously keeps adding the items every time you check and uncheck it.
Private Sub Checkbox1_Click()
Dim mylist(7) As String
Dim i As Long
mylist(0) = "time"
mylist(1) = "hour"
mylist(2) = "how"
mylist(3) = "test"
mylist(4) = "number"
mylist(5) = "sent"
mylist(6) = "memo"
mylist(7) = "value"
For i = 0 To 7
If CheckBox1.Value = True Then
Finallist.AddItem mylist(i)
End If
Next i
End Sub
I can populate the list when the checkbox is checked with the code above, but struggling to remove the array items from the list when the user unchecks the listbox. I simply need to remove the same items from listbox when user unchecks the same checkbox.
I tried the following solution after the code, but seem to be making something very wrong with it, I understand. Just totally stuck....Could someone help me please?
If checkobx.value=false then
For i = 0 To 7
For j = 0 To FinalList.ListCount - 1
If InStr(Final.List(j), mylist(i)) > 0 Then
Finallist.RemoveItem mylist(i)
End If
Next j
Next i
end if
Try this (explanations in comments and untested):
If CheckBox1.Value Then ‘ if checkbox checked
For i = 0 To 7
NegKeyList.AddItem interrlist(i)
Next
Else ‘otherwise
Dim i As Long
For i = NegKeyList.ListCount - 1 To 0 Step -1 ‘ loop through listbox items from last one backwards
If Not IsError(Application.Match(NegKeyList.List(i), interrlist,0)) Then NegKeyList.RemoveItem i ‘ if current listbox item matches any interrlist array item then remove it from listbox
Next
End If

(Excel Userform) Check if all checkboxes in a Userform are checked

Never tried UserForm checkboxes before so I don't even know how to point to the Checkboxes in a Userform.
This is what I have at the moment....and I know, I know, it is completely wrong. Please help?
Private Sub Step1_Confirm_Click()
Dim i As Byte
Dim Done As Boolean
For i = 1 To 4
If Step1_(i).value = True Then
Done = True
End If
Next i
If Not Done = True Then
MsgBox "Please make sure you have done all"
End If
End Sub
Basically I have:
A Userform called IOSP_Acc_R_Approval_Step1
4 checkboxes called Step1_1; Step1_2; Step1_3; Step1_4
A button called Step1_Confirm
I want the button to show Error, if not all checkboxes are checked - meaning that all checkboxes have to be checked....(in case my English is too bad to convey my meaning)
Try the code below (explanations inside the code as comments):
Private Sub Step1_Confirm_Click()
Dim i As Long
Dim Ctrl As Control
' loop through all user_form control
For Each Ctrl In IOSP_Acc_R_Approval.Controls
If TypeName(Ctrl) = "CheckBox" Then ' check if control type is Check-Box
If Ctrl.Value = True Then ' check if check-box is checked
i = i + 1
End If
End If
Next Ctrl
If i < 4 Then ' not all 4 check-boxes are checked
MsgBox "Please make sure you have done all"
End If
End Sub
You can do this by:
assume that all checkboxes are checked by setting a flag to True
iterate the checkboxes and if one is not checked set the flag to False and exit
at the end of the loop, if all checkboxes were checked then the flag is still True
You can refer to the checkboxes dynamically by using the Me.Controls collection and pass in the name of the checkbox like "Step1_" & i.
Example code:
Option Explicit
Private Sub Step1_Confirm_Click()
Dim i As Long '<-- use Long, not Byte
Dim blnResult As Boolean
' set a flag to assume that it is true that all checkboxes are checked
blnResult = True
' get the value of each check box
For i = 1 To 4
If Me.Controls("Step1_" & i).Value = False Then
blnResult = False
Exit For '<-- skip loop if at least one box not checked
End If
Next i
' check the value of the flag
If blnResult = False Then
MsgBox "Please make sure you have done all"
Else
' all boxes checked ...
MsgBox "All checked"
End If
End Sub
Done=true
For i = 1 To 4
Done = Done*Step1_(i).value
Next i
if done `then`
msgbox "All checkboxes are checked"
end if

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.