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
Related
Hi,
I want to let the UserForm to be able to detect the text in cell B4 and choose the correct option button without any extra click in the userform. May I know how should I achieve that? Thank you.
Add this into your userform module:
Private Sub UserForm_Initialize()
If Sheets("Sheet1").Range("B4") = "Profit" Then Me.ProfitOption.Value = True
End Sub
Couple things to change:
-Change the Sheet1 to whatever yours is.
-Change the ProfitOption to whatever the name of your button is.
I recommend something like this
Private Sub UserForm_Initialize()
Select Case Sheet1.Range("B4").Value 'evaluate the value of the cell
Case "Profit"
Me.OptionButton1.Value = True
Case "Loss"
Me.OptionButton2.Value = True
Case Else 'if it is none of the above then go into undefined state
Me.OptionButton1.Value = Null
Me.OptionButton2.Value = Null
End Select
End Sub
Note that this does not change the cell value if you change the option in the userform. Therefore you would need write the changed state back using either the Private Sub OptionButton1_Change() event or a "save" button.
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
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
I have an Excel UserForm, which submits data to a worksheet. I have built in data validation that requires each text box to have a value entered, e.g.:
Private Sub Button_Submit_Click()
'Data Validation
If Me.txtCVS.Value = "" Then
Me.txtCVS.SetFocus
MsgBox "'CVs Screened' is a mandatory field. Enter daily figure or zero.", vbOKOnly, "Required Field"
Exit Function
However, I don't know what code to use to require a selection from a list box - users select their name from the list box before entering in their daily figures. Some users, it seems, can't remember to click their name(!!!).
Does anybody have a piece of workable code I can use to require a selection before submitting results? It doesn't seem to work in the same way as the text box text above.
You can use the below function which iterates through all the items in the listbox and returns true/false if anything is selected or not.
Private Sub Button_Submit_Click()
If Not IsAnythingSelected(ListBox1) Then
MsgBox "Please select your name"
End If
End Sub
Function IsAnythingSelected(lBox As Control) As Boolean
Dim i As Long
Dim selected As Boolean
selected = False
For i = 1 To lBox.ListCount
If lBox.selected(i) Then
selected = True
Exit For
End If
Next i
IsAnythingSelected = selected
End Function
Try changing
For i = 1 to lBox.ListCount
to the following:
For i = 0 To lBox.ListCount - 1
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