Disable or hide options in combo box VBA Excel - excel

I have a combo box in a Excel Userform that consist of User Group Types.
Depending on the user access level, I would like to have some Option\item disable or not visible.
I don't want to use Removeitem, Because I would have to repopulate the list every time!
sub ComboBox_Enter()
accessLvl = 1
ComboBox.AddItem "0-Show"
ComboBox.AddItem "1-Hide or disable"
ComboBox.AddItem "2-Show"
ComboBox.AddItem "3-Show"
For i = 0 To 3
if accessLvl = 1 Then ComboBox.List(1).Hidden = True ' This not does work!! ''
Next
End sub
I just want it to be disabled\grayed out or not visible but still in the Combobox list!*

AFAIK, you can't do that but there is an alternative. The user will not be able to select certain items (whichever you specify) even though it will be visible and not disabled.
For this try this code
Dim boolC As Boolean
'~~> Add Sample data
Private Sub UserForm_Initialize()
ComboBox1.AddItem "Please Choose Again"
For i = 1 To 10
ComboBox1.AddItem i
Next i
End Sub
'~~> This will not let the user select items in 2nd
'~~> 3rd and 4th items
Private Sub ComboBox1_Change()
If Not boolC Then
boolC = True
Select Case ComboBox1.ListIndex
Case 1, 2, 3: ComboBox1.ListIndex = 0
End Select
boolC = False
End If
End Sub
Screenshot
Let's say your form looks like this on form start up.
The moment you select the 2nd ,3rd or the 4th item, you will get Please Choose Again

Related

Get the last selected item in multiselect box

I have add a ListBox from Active X Controls in my Excel File and made it a multi select box with checkboxes.
I have also added a selection change event in the VB script against this list box.
Sub lstMultiSelectBox_Change()
If blnCheck = False Then
CheckAll
End If
End Sub
Now what I am struggling to find is that which item was last checked. With this information I want to implement Select All and Un Select All feature in this list box.
In order to make ListBox1_Change event returning the last selected list box value, you can use the solution. It can detect the selected value, independent of its position in the list:
Create a Private variable on top of the sheet module where the list box exists (in the declarations area):
Private colS As New Collection
Then copy the next adapted event code:
Private Sub ListBox1_Change()
Dim i As Long
For i = 0 To ListBox1.ListCount - 1
If ListBox1.Selected(i) Then
If colS.Count = 0 Then
colS.Add ListBox1.List(i), ListBox1.List(i)
Else
If Not itExists(colS, ListBox1.List(i)) Then
colS.Add ListBox1.List(i), ListBox1.List(i)
End If
End If
Else
If itExists(colS, ListBox1.List(i)) Then
colS.Remove ListBox1.List(i): Exit Sub
End If
End If
Next i
If colS.Count > 0 Then MsgBox colS(colS.Count)
End Sub
If you want it triggering only if the selected value is "Select All", then replace the last event code line with something like:
If colS.Count > 0 Then
If colS(colS.Count) = "Select All" then
'do whatever you need in such a case
'but, if you try selecting all of lines, in order to avoid the event
'being triggered again, you should use 'Application.EnableEvents = False`, before selecting and 'Application.EnableEvents = True` after
End If
End If
The simplest solution should be the one suggested in the first comment:
If Listbox1.Selected(1) = True Then
'do whatever you need
End If
But, in order to make it working as it should, the line "Select All" should be the second of the list...

Unhide/Hide rows in excel with a form checkbox

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

How to add a label and textbox based on combo box selection on a userform in excel

I am currently working on a userform to create an order for users at a company to send to my dept.
At the moment i have come to a standstill as i am struggling to work out the following.
I have a combobox which has a list of products our business offers. Based on the selection i want to be able to add labels and textbox which require the user to enter data for example.
If this selection in the combo box then
Enter name, date required, location of user etc.
Also this needs to be specific to the combobox selection.
Any help would be much appreciated :)
UPDATE
Apologies, as i did not have any code for that function I did not add any Here is the code i have.
Private Sub CommandButton1_Click()
Windows("RFS User Form Mock.xlsm").Visible = True
End Sub
Private Sub LegendDefinition_Change()
LegendDefinition.Locked = True
End Sub
Private Sub RequestList_Change()
Dim i As Long, LastRow As Long
LastRow = Sheets("Definition").Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LastRow
If Sheets("Definition").Cells(i, "A").Value = (Me.RequestList) Then
Me.DefinitionBox = Sheets("Definition").Cells(i, "B").Value
End If
Next
End Sub
Private Sub RequestList_DropButtonClick()
Dim i As Long, LastRow As Long
LastRow = Sheets("Definition").Range("A" & Rows.Count).End(xlUp).Row
If Me.RequestList.ListCount = 0 Then
For i = 2 To LastRow
Me.RequestList.AddItem Sheets("Definition").Cells(i, "A").Value
Next i
End If
End Sub
Sub UserForm_Initialize()
SiteList.List = Array("Birmingham", "Bristol", "Cardiff", "Chelmsford", "Edinburgh", "Fenchurch Street", "Glasgow", "Guernsey", "Halifax", "Homeworker", "Horsham", "Ipswich", "Jersey", "Leeds", "Leicester", "Lennox Wood", "Liverpool", "Manchester", "Peterborough", "Redhill", "Sunderland", "Madrid")
End Sub
Private Sub VLookUp_Change()
VLookUp.Locked = True
End Sub
When posting a question, you are expected to provide some code showing where you're standing trying to address the problem. Here's nevertheless a short demo that will give you a starting point.
Create a new UserForm and put a combobox, a label and a textbox on it; ensure they're named ComboBox1, Label1 and TextBox1, respectively.
Then, paste this code in the form's module:
Option Explicit
Private Sub ComboBox1_Change()
Dim bVisible As Boolean
'Only show the label and the textbox when the combo list index is 1, which corresponds to "Item 2".
'Note: bVisible = (ComboBox1.Text = "Item 2") would also work.
bVisible = (ComboBox1.ListIndex = 1)
Label1.Visible = bVisible
TextBox1.Visible = bVisible
End Sub
Private Sub UserForm_Layout()
'Populate the combo.
ComboBox1.AddItem "Item 1", 0
ComboBox1.AddItem "Item 2", 1
'Note: the code below could be removed by setting the corresponding
'design-time properties from the form designer.
ComboBox1.Style = fmStyleDropDownList
Label1.Visible = False
TextBox1.Visible = False
End Sub
Then press F5 to show the form. You'll notice that the label and textbox are only visible when the combo shows "Item 2". The visibility adjustment is performed within the ComboBox1_Change event handler.
If you plan on having numerous controls shown / hidden depending on your combo's value, you could group them into one or more Frame controls, and show / hide those frames instead.

Limit to only 1 selected checkbox

I have imported a table with check-boxes from Access to Excel. Is it possible to set the check-boxes where only one check-box can be selected from that imported table when using Excel?
In the comments Jeeped made an excellent point that radio buttons already have the functionality that you are looking for. On the other hand -- if you prefer the aesthetics of checkboxes then you can certainly use them. I created a userform with two checkboxes in a frame (and no other controls in the frame) and also included a label for displaying the chosen option. The following code deselects all other checkboxes in the frame when one is selected. I used a non-local Boolean variable to circumvent the other checkbox's event handlers while they were being changed to avoid a sort of echo effect I ran into where the events were firing when I didn't want them to (perhaps there is a less kludgy way to do that). The code easily extends to any number of checkboxes in a grouping frame.
Dim selecting As Boolean 'module level variable
Private Sub SelectOne(i As Long)
Dim c As Control
selecting = True
For Each c In Frame1.Controls
If c.Name <> "CheckBox" & i Then c.Value = False
Next c
DoEvents
Label1.Caption = i & " selected"
selecting = False
End Sub
Private Sub CheckBox1_Click()
If Not selecting Then SelectOne 1
End Sub
Private Sub CheckBox2_Click()
If Not selecting Then SelectOne 2
End Sub
I think this works best and its much easier - at least for a few boxes - for more you could write some formulas in excel and drag down then copy as values and copy paste text from excel into vba. Anyway, here it's how I did it:
I went and created code under each button - quite basic
Private Sub DateCheckBox1_Click()
If DateCheckBox1.Value = True Then
DateCheckBox2.Value = False
DateCheckBox3.Value = False
End If
End Sub
Private Sub DateCheckBox2_Click()
If DateCheckBox2.Value = True Then
DateCheckBox3.Value = False
DateCheckBox1.Value = False
End If
End Sub
Private Sub DateCheckBox3_Click()
If DateCheckBox3.Value = True Then
DateCheckBox2.Value = False
DateCheckBox1.Value = False
End If
End Sub

Unhiding very hidden sheet Excel VBA

I am trying to create a user form that will unhide a specific worksheet based on the value of the combo box on the user form. There are 3 different worksheets that are " very hidden" in the workbook. The combo box is populated with 3 choices, one for each hidden sheet. I am using select case to make the correct sheet visible (Eventually there will be many more than 3 sheets/options. Sample code follows (located in the user form code window):
Private Sub NextButton_Click()
Select Case ComboBox
Case ComboBox.ListIndex = 0
Sheets(1).Visible = True
Case ComboBox.ListIndex = 1
Sheets(2).Visible = True
Case ComboBox.ListIndex = 2
Sheets(3).Visible = True
End Select
Unload UserForm
End Sub
I click the next button, the userform unloads, but the sheets does not become visible. VBA brings up no errors either. Please let me know if I need to provide any more information.
Nik
Your case statement is incorrect. You should tell it what value to test in the first part, then specify the values later. See Tech on the Net article about Case in VBA.
Private Sub NextButton_Click()
Select Case ComboBox.ListIndex
Case 0
Sheets(1).Visible = True
Case 1
Sheets(2).Visible = True
Case 2
Sheets(3).Visible = True
End Select
Unload UserForm
End Sub
This is how case statements in most programming languages work.
However, since ComboBox.ListIndex is an int, and you're telling it what sheet to show based on that, you could simplify the whole thing and drop the case statement. This presumes the indexes match up, which they do in your example.
Private Sub NextButton_Click()
Sheets(ComboBox.ListIndex+1).Visible = True
Unload UserForm
End Sub

Resources