Cant Change DropDown text while processing another DropDown - excel

I have 2 DropDowns on an excel sheet which work independently of each other. The selections on both drop down's can cause confusion during reporting generation. So I need to set DropDown#1 to "select" if DropDown2 is being used by the user and vice versa.
I am trying to use the DropDown.Text property but it does not do the trick.
Sub PDropDown_Click()
Dim DropDownP As DropDown
Dim DropDownD As DropDown
Set DropDownD = Me.DropDowns("DDropDown")
Set DropDownP = Me.DropDowns("PDropDown")
DropDownD.Text ="Select"
DropDownP.Text = DropDownP.List(DropDownP.ListIndex)
Call Report_Generator.Create_Graph(DropDownP.List(DropDownP.ListIndex))
End Sub

You will have to add the value select to you combo boxes.
Dim bExit as Boolean
Private Sub ComboBox1_Change()
If bExit = True Then
bExit = False
Exit Sub
End If
bExit = True
ComboBox2.Text = "Select"
End Sub
Private Sub ComboBox2_Change()
If bExit = True Then
bExit = False
Exit Sub
End If
bExit = True
ComboBox1.Text = "Select"
End Sub

Related

How do I enable the check box again?

I have VBA code which checks the value of a cell and should enable/disable a checkbox depending on the value:
Private Sub CheckBox2_Click()
If Range("A4").Value = "ZJ3" Then
CheckBox2.Enabled = True
ElseIf Range("A4").Value = "ZJ2" Then
CheckBox2.Enabled = False
CheckBox2.Value = False
End If
End Sub
When I select ZJ2 in cell A4, the checkbox does what is intended, and disables the value and the entry of the checkbox.
When I change the value of ZJ3 it remains greyed out when it should enable.
It might be better to use the Worksheet_Change event to control whether the checkbox is enabled or not, something like the following:
Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Me.Range("A4")) Is Nothing Then Exit Sub
Select Case Me.Range("A4").Value
Case "ZJ3"
Me.CheckBox2.Enabled = True
Case "ZJ2"
Me.CheckBox2.Enabled = False
Me.CheckBox2.Value = False
End Select
End Sub

Excel VBA code to update the hide/unhide rows functions when a new row is added within the range wanted to be hidden

I currently have a code as follows to hide a range of cells when an activeX checkbox is clicked:
Version 1:
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
Rows("14:30").Hidden = False
Else
Rows("14:30").Hidden = True
End If
End Sub
Private Sub CheckBox2_Click()
If CheckBox2.Value = True Then
Rows("32:38").Hidden = False
Else
Rows("32:38").Hidden = True
End If
End Sub
Private Sub CheckBox3_Click()
If CheckBox3.Value = True Then
Rows("40:54").Hidden = False
Else
Rows("40:54").Hidden = True
End If
End Sub
Version 2:
Private Sub CheckBox1_Click()
[14:30].EntireRow.Hidden = Not CheckBox1
End Sub
Private Sub CheckBox2_Click()
[32:38].EntireRow.Hidden = Not CheckBox2
End Sub
Private Sub CheckBox3_Click()
[40:54].EntireRow.Hidden = Not CheckBox3
End Sub
THE PROBLEM:
Both versions work fine BUT the problem is that when a new row is added within the ranges specified, the row specifications obviously do not update as they are not variables.
NOTE: There are more than 3 ActiveX checkboxes. I've got about 19.
QUESTION
I know the ranges need to be put to an integer or a variable but I am only new to VBA and not even a programmer (studied a few programming applications in college 4 years ago so can read code a bit) so I have no idea how to do it. Currently working on automating an excel file at work and WOULD LOVE YOUR HELP PLEASEEEE! I have been struggling for days on this :(
Excel file looks like this:
Thank you in advance!!
You need to dynamically determine the starting row and end row for each section. This should work for the top A - General section.
Option Explicit
Private Sub CheckBox1_Click()
Dim m1 As Variant, m2 As Variant
m1 = Application.Match("A -*", Columns(1), 0)
m2 = Application.Match("B -*", Columns(1), 0)
If IsError(m1) Or IsError(m2) Then Exit Sub
With Range(Cells(m1 + 1, "A"), Cells(m2 - 1, "A"))
.EntireRow.Hidden = Not CheckBox1.Value
End With
End Sub

Excel Group of image hiding on another sheet checkbox

I'm trying to hide a bunch of images i have placed into a group via a checkbox, I can do this via the same sheet but no on the sheet the textbox is.
Sub hideimages()
If ActiveSheet.CheckBoxes("Check Box 1").Value = 1 Then
ActiveSheet.Shapes("Group 21").Visible = True
Else: ActiveSheet.Shapes("Group 21").Visible = False
End If
End Sub
But i can't seem to figure out the right syntax to get it to affect another sheet for the group I can do it for a singular image:
Sub CheckBox33_Click()
Dim obj As Shape
Set obj = Worksheets("sheet3").Shapes("picture 2")
If obj.Visible Then
obj.Visible = True
Else
obj.Visible = False
End If
How could i merge these? the ways i have tried are not happy!
Sub hidaway()
If Worksheets("sheet1").CheckBoxes("Check Box 34").Value = 1 Then
Worksheets("sheet3").group("Group 21").Visible = True
Else: Worksheets("sheet3").group("Group 21").Visible = False
End If
End Sub
Your checkbox returns True/False so you just need to feed this value to your group visible property:
Private Sub CheckBox1_Click()
ThisWorkbook.Worksheets("Sheet3").Shapes("Group 21").Visible = Me.CheckBox1.Value
End Sub

Excel Select all items in Listbox Vba

I am stumped I tried the following and for the life of me I cant figure this out....Need to use a comand button on a listbox on an excel vba form.
Iniatilizing the form on load.....it load fine
Sub UserForm_Initialize()
UserForm1.LbNumbers.RowSource = "Sheet2!A1:A3"End Sub
Items display in listbox fine
I have a command button under the list box to select all the code
Sub CbSelectall_Click()
For i = 0 To LbNumbers.ListCount - 1
LbNumbers.Selected(i) = True
Next i
End Sub
If I click on button it jumps to the last line but it doesnt select all the numbers in the listbox. Can someone tell me how can I rectify it to select all the numbers in the listbox.Thank you
Private Sub lbTraderId_Change()
If ResetListBox(lbTraderId) Then
Exit Sub
ElseIf lbTraderId.Selected(0) Then
For i = 1 To lbTraderId.ListCount - 1
If lbTraderId.Selected(i) = False Then
lbTraderId.Selected(i) = True
End If
Next i
ElseIf lbTraderId.Selected(0) = False Then
For i = 1 To lbTraderId.ListCount - 1
If lbTraderId.Selected(i) = True Then
lbTraderId.Selected(i) = False
End If
Next i
End If
End Sub
Private Function ResetListBox(lbx As MSForms.ListBox) As Boolean
Dim x As Long
Static bExit As Boolean
If Not bExit Then
x = lbx.ListIndex
If x >= 0 And Not lbx Is Me.lbTraderId Then
bExit = True
lbx.Selected(x) = Not lbx.Selected(x)
bExit = False
ResetListBox = True
End If
End If
End Function

Excel form listbox issue when using locked property

I'm having an issue with the way a listbox behaves on an Excel form. Steps to reproduce the issue:
Create a user form with one listbox control
Use following code with this user form:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.ListBox1.Locked = True
Me.ListBox1.Locked = False
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
When the form is first shown, I am able to navigate the list box normally, using arrow keys and page keys. However, after the double-click event is triggered, all keyboard navigation has no effect, including tabbing to other controls (if they're on the form). Clicking on the listbox does seem to work, and the focus outline is shown correctly, but there's something wrong with the way the focus is handled after the listbox is locked and then unlocked. What is going on?
Using Office 2013 32-bit edition.
I managed to replicate this issue, and setting the focus somewhere else before locking and unlocking the listbox worked for me:
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Me.TextBox1.SetFocus 'or some other control.
Me.ListBox1.Locked = True
Me.ListBox1.Locked = False
Me.ListBox1.SetFocus
End Sub
2 solutions I tested that allows you to use Arrow Keys to navigate.
Given that there isn't a single click event handler, try call the Single Click Event after the DblClick (works all the time):
Private Sub ListBox1_Click()
Debug.Print "ListBox1_Click()"
End Sub
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "ListBox1_DblClick()"
With Me.ListBox1
.Locked = True
.Locked = False
End With
ListBox1_Click
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
Setting the Cancel = False at the end of DblClick. (Sometimes does not work!)
Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Debug.Print "ListBox1_DblClick()"
With Me.ListBox1
.Locked = True
.Locked = False
End With
Cancel = False
End Sub
Private Sub UserForm_Initialize()
Dim i As Integer
For i = 1 To 10
Me.ListBox1.AddItem i
Next i
End Sub
Changing the zoom in the sheet that has the listbox worked for me.
Private Sub Worksheet_Activate()
Dim temp As Double
Application.ScreenUpdating = False
'Change worksheet zoom setting for the active window
temp = ActiveWindow.Zoom
ActiveWindow.Zoom = temp + 10
ActiveWindow.Zoom = temp
Application.ScreenUpdating = True
End Sub

Resources