vba both checkbox trigger and dropdown triggers does work together - excel

i have an excel file like the following:
[enter image description here]enter image description here
[1]: https://i.stack.imgur.com/QVe6i.png
this checkbox is being controlled by a trigger and the code is as follows:
Sub CheckBox1_Click()
If Range("CheckBox1").Value = True Then
Range("ET").Rows.Hidden = True
Else:
Range("ET").Rows.Hidden = False
End If
If Range("CheckBox1").Value = True And Range("CheckBox1a").Value = True Then
Range("A50:A53").Rows.Hidden = False
Range("A61:A63").Rows.Hidden = False
Else:
Range("A50:A53").Rows.Hidden = True
Range("A61:A63").Rows.Hidden = True
End If
End sub
if the value of the checkbox is true, it hides rows "A50:A53" and"A61:A63" and unhides when the value is False. works perfect!
On the Same sheet, i have a dropdown that does the same stuff based on the selected option: following is the excel file:
[1]: https://i.stack.imgur.com/EvCt0.png
The same as the CheckBox, the dropdown option also, hides rows "A209:A210", if yes is selected. the triggers for the dropdown is as follows:
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
' Section 4. Testing part 1
If Target.Column = 53 And Target.row = 208 Then
If Target.Value = "Yes" Then
Range("Addition").Rows.Hidden = False
Range("A112:A113").Rows.Hidden = True
ElseIf Target.Value = "No" Then
Range("A112:A113").Rows.Hidden = False
Range("Addition").Rows.Hidden = True
End If
End If
End sub
The issue now is. When i start to work with the CheckBoxes, they work fine however the Dropdown doesn't respond and the same thing happens if the start to work with the dropdown, the checkboxes stop to respond.
Any suggestion or help is highly appreciated!

Instead of CheckBox1_Click try the CheckBox1_Change event. This will ensure the checkbox is updating every time the value changes, even if if doesn't gain or lose focus.
Other than that, you will need to have something in Workbook_Open that checks the default status of those checkboxes and ranges, hiding and showing rows as necessary.
You will also need a separate event just for CheckBox1a. Each CheckBox_Change script only triggers based on the named object in the script's title. For example CheckBox1_Change will only trigger for changes to a matching CheckBox1 object. So if you want things to happen when the user clicks on CheckBox1a then you need a CheckBox1a_Change script.
Finally, you are retrieving and comparing checkbox values using Range("CheckBox1") which is not intended. There is a built in method for accessing these objects through VBA. Each CheckBox is added to the Worksheet object as a member. In the sheet's code module you can write Me.CheckBox1.Value. In an external code module you could write Sheets("Sheet1").CheckBox1.Value.

Related

UserForm ListBox Change not Updating TextBox Visibility

I have a UserForm that has a ListBox named EIDLList and after that there's a TextBox EIDLAmountLabel and EIDLAmountText. I've written a snippet of code that is supposed to grey out both TextBoxes if the ListBox selection is set to "No" but for some reason it's not working. I've tried about 10 different variations using Case, If, UCase, etc and none work. I appreciate any input.
Private Sub EIDLList_Change()
If EIDLList.Value = "No" Then
EIDLAmountLabel.Enabled = False
EIDLAmountText.Enabled = False
Else
EIDLAmountLabel.Enabled = True
EIDLAmountText.Enabled = True
End If
End Sub
I changed the fields from ListBox to ComboBox and now the code works perfectly. Not sure how or why that matters.

VBA code to hide/unhide multiple rows in another worksheet based on cell output from another worksheet

I want to use VBA code for the following: based on a cell output (either "TRUE" or "FALSE" in cell W20, which changes based on whether a user checks a box or not), I want rows to be hidden/unhidden on another worksheet called "Analysis" worksheet based on the code below. I right clicked the "Summary" tab where cell W20 and the checkbox are located -> view code -> wrote the code below, but it's not working. I am very new to VBA, please help, it would be very appreciated, thanks in advance
Sub Worksheet_Change(ByVal Target As Range)
Set Target = Range("$W$20")
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = False
End If
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("94:95").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("94:95").EntireRow.Hidden = False
End If
If Target.Value = "FALSE" Then
Sheets("Analysis").Rows("134:135").EntireRow.Hidden = True
Else
Sheets("Analysis").Rows("134:135").EntireRow.Hidden = False
End If
End If
End Sub
Excel doesn't recognize the modification of a cell value caused by the manipulation of form controls. I assume this is because Excel links a form control to a value and applies the changes itself, therefore not considering it a user change in the spreadsheet.
In order to make your code work, you will have to move your code into a module as a click event of your form control. Click the Developer tab and then Visual Basic. Once Visual Basic is open, you will notice on the left pane that you have a list of your worksheets within a VBA Project bearing the name of your workbook and your macro is currently within one of them. Once you found it, delete the code of your macro. Then, right-click on the name of your workbook and then select Insert > Module...
On the coding pane, insert this modified code:
Sub NameOfYourFormControlCheckBox_Click()
Dim MyRange As Range
Set MyRange = Range("$W$20")
If MyRange = False Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = True
ElseIf MyRange = True Then
Sheets("Analysis").Rows("54:55").EntireRow.Hidden = False
End If
End Sub
Make sure that the name of the sub is the name of your form control (checkbox) followed by an underscore and then "click".
Hope this helps!

excel vba: show message on dropdown list selection change if changed more than once?

I am trying to create a piece of vba code which will generate a message to the user when a user selects a value from a dropdown list/validation list in excel.
So far my script does this fine. However, I want the user to be able to select their first value from the dropdown list without getting a message, and then on the second, third or fourth time etc, if they should change their selection in the dropdown list I want the message to display.
Can someone please show me a way of doing this? Thanks in advance
'Check number of times a user has changed their selection
Dim rM As Range
Set rM = Range("M" & ActiveCell.Row).Cells.SpecialCells(xlCellTypeAllValidation)
If Intersect(Target, rM) Is Nothing Then
Else
MsgBox "changed"
End If
Add ..
Private BooRangeSelected as Boolean
... at the very top of the VBA code.
Set this value to False in the Initialize event.
Set this value to True when the dropdown list is updated.
If the value is already True when the dropdown list is selected then issue the message.
I'm assuming this macro is attached to a Workbook rather than a Form stored in "personal.xlsb". In Excel / view code double click the "ThisWorkbook" icon (see below
Private Sub Workbook_open()
Private BooRangeSelected as Boolean
BooRangeSelected = False
End Sub
This sets the variable to False once the Worksheet is opened. You can then set this to True once the dropdown has been selected
BooRangeSelected = True

EntireColumn.Hidden in Worksheet_Change()

Using Excel 2010 I am editing an existing unprotected workbook and have created EntireColumn.Hidden and EntireRow.Hidden in commands in the Worksheet_Change() event to fire when a Data Validation cell is changed, but they don't work.
Private Sub Worksheet_Change(ByVal Target As Range)
With Application
.ScreenUpdating = False
.EnableEvents = False
End With
If Not Intersect(Target, Range("$C$2")) Is Nothing Then
Select Case Target.Value
Case "NO"
MsgBox "You just changed to HIDE" '<= Proves it fires
Range("$C$3").Value = "Invisible" '<= Does change cell
Columns("N:O").EntireColumn.Hidden = True '<= Doesn't hide
Case "YES"
MsgBox "You just changed to UNHIDE" '<= Proves it fires
Range("$C$3").Value = "Visible" '<= Does change cell
Columns("N:O").EntireColumn.Hidden = False '<= Doesn't unhide
End Select
End If
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
The event is firing as I have MsgBoxes to prove it. I can change cell values etc., but not the hiding/unhiding of the column/row.
I've copied my code to a new book and it works. So I copied it back into the original book but as a fresh, blank sheet and it works. It still doesn't work in the original, sizable sheet.
When I copied this into a simple macro it does work as required, hiding the correct columns, but at the push of a button:
Sub HideThem()
Columns("N:O").EntireColumn.Hidden = True '<= DOES work
End Sub
I need this to update automatically based on the value of a single cell. I've even tried to call this mini Sub from within the Worksheet_Change() event but that didn't work either.
Are there any known conflicts with other commands/events, on-sheet buttons, images, merged cells etc. that could be preventing the columns/rows from hiding?
I tried to use a CheckBox instead of a YES/NO Data Validation cell to fire the code (as that could be acceptable) but when I try to insert an ActiveX CheckBox it says Cannot insert object, even in a brand new blank book. Could this be a related problem?
I suppose you have a drop-down list in cell C3 with two items, viz "Visible" and "Invisible".
The following code will hide Columns N and O when you change the value of Range C3 from blank / "Visible" to "Invisible". Prior to this action, you will have to read the message and click OK. Changing from "Invisible" to "Visible" will present you with a message box. Click OK and see the hidden columns reveal themselves.
Private Sub Worksheet_Change(ByVal Target As Range)
If Range("C3") = "Invisible" Then
MsgBox ("You just changed to HIDE")
Columns(14).Hidden = True
Columns(15).Hidden = True
Else
If Range("C3") = "Visible" Then
MsgBox ("You just changed to UNHIDE")
Columns(14).Hidden = False
Columns(15).Hidden = False
End If
End If
End Sub

Excel VBA: Can't deselect ListBox items from listbox

I have a command button that processes selected items in a ListBox (ListBox4). While i CAN deselect all the items in that command's Click() procedure, I wanted to, if the user clicked in the ListBox, to THEN deselect everything in the ListBox, prior to their selecting again.
I have code like the following, but it never seems to get called:
Private Sub ListBox4_Click()
If Apply_Format_Occurred Then
For i = 0 To ListBox4.ListCount - 1
ListBox4.Selected(i) = False
Next i
End Sub
Do i need an outside command, etc to do this? I was hoping to be able to do it like how i described.
Any help is greatly appreciated!
thanks,
Russ
You can use the GotFocus event of the ListBox so the code runs when the ListBox receives focus from the user.
Here is an example showing coding for the button and the ListBox:
Dim Apply_Format_Occurred As Boolean
Private Sub CommandButton1_Click()
'<other processes>
Apply_Format_Occurred = True
End Sub
Private Sub ListBox4_Change()
If Apply_Format_Occurred Then
For i = 0 To ListBox4.ListCount - 1
ListBox4.Selected(i) = False
Next i
Apply_Format_Occurred = False
End If
End Sub
I see this thread is old and maybe there is an easy or more elegant way to un-select a list box item. But, I figured out a way for my needs. In my case I wanted the list box to un-select only if the same item was clicked. If a new item was clicked, it would be selected as normal. I had to use two static variable (a boolean and "old selection" placeholder).
There's probably a much easier way. But, I'm new to it and and couldn't find it. Hope this helps. Setting selectedindex to -1 unselects everything. Altenatively and as effective is listboxName.ClearSelected()
Private Sub lstPendingQuotes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstPendingQuotes.Click
Static blnSelectable As Boolean = True 'use static boolean to retain value between calls
Static objOldSelected As Object = lstPendingQuotes.SelectedIndex 'use old selected in case a different index is selected
'if nothing is already selected (as in first form open) allow the selection but change boolean to false and set the OldSelected variable to the current selection
If blnSelectable Then
blnSelectable = False
objOldSelected = lstPendingQuotes.SelectedIndex
ElseIf lstPendingQuotes.SelectedIndex = objOldSelected Then 'if an item is selected and the same item is clicked un-select all and reset boolean to true for a new possible selection
lstPendingQuotes.SelectedIndex = -1 'can substitute lstPendingQuotes.ClearSelected()
blnSelectable = True
Else 'If a different index is chosen allow the new selection and change boolean to false so if the same item is clicked it will un-select
objOldSelected = lstPendingQuotes.SelectedIndex
blnSelectable = False
End If
End Sub
I had an onclick event for my listbox. I tried listbox.listindex=-1 at the end of the code in that event. It wouldn't work.
I created a separate AfterUpdate event with the single line:
listbox.ListIndex = -1
works like a charm

Resources