combobox text not on list error handling VBA - vba

I have a workbook with about 130 sheets in it, which I select from a combobox on the main page. I populate the combobox using rowsource, from a named range which has the sheet names in it. It works great if you select one of the sheet names from the list, and it closes nicely if you don't enter anything and still press submit. I want to cover the case where the user types in something not on the list. I want to say something to VBA like 'if sWs is not equal to something from the named range, then show a message box which says 'error'', or whatever. Here is my code below, with my notes:
'if submit button is pressed go to selected worksheet or close the userform:
Private Sub Submit_Click()
Dim sWs As String
'the string is the text from combobox, called 'Title', selected on front page:
sWs = Me.Title.Value
'if nothing is selected then close the userform:
If sWs = ("") Then
End
End If
'If an item is selected from the dropdown list then show that worksheet and close the userform:
Worksheets(sWs).Select
End
End Sub
'If exit button is pressed close the userform:
Private Sub ExitButton_Click()
End
End Sub
I think I need to be using a Boolean, so I can specify what to do when it is false, but not sure how to write it. Sorry, I'm a bit of a noob.
Any ideas?

Try checking the list index of the item selected, -1 would indicate something has been entered that is not valid
'if submit button is pressed go to selected worksheet or close the userform:
Private Sub Submit_Click()
Dim sWs As String
'the string is the text from combobox, called 'Title', selected on front page:
If Me.Title.ListIndex = - 1 Then
MsgBox "Invalid Entry"
End
End If
sWs = Me.Title.Value
'if nothing is selected then close the userform:
If sWs = ("") Then
End
End If
'If an item is selected from the dropdown list then show that worksheet and close the userform:
Worksheets(sWs).Select
End
End Sub
'If exit button is pressed close the userform:
Private Sub ExitButton_Click()
End
End Sub

Related

How to paste clipboard value into a specific ComboBox on a UserForm?

I copy a name in the first column of the active row on a spreadsheet to the clipboard.
I launch a UserForm by the name CommandsUserForm.
The UserForm is overlaid with multiple pages or tabs, so it defaults to the first tab (this is desired).
On this tab, there is a ComboBox by the name DPComboBox.
I want to paste the value in the clipboard into the ComboBox after the userform is launched.
Userform with the ComboBox highlighted.
Sub Show_Quick_Commands()
DPName = ThisWorkbook.ActiveSheet.Cells(ActiveCell.Row, 1).Value
Set DPNameforQ = New DataObject
DPNameforQ.SetText DPName
DPNameforQ.PutInClipboard
CommandsUserForm.Show vbModeless
End Sub
I tried DPComboBox.PasteSpecial Transpose:=True, but that breaks the code and requests a debug.
For example:
Sub Show_Quick_Commands()
Dim frm As CommandsUserForm
Set frm = New CommandsUserForm
frm.DPName = ActiveCell.EntireRow.Cells(1).Value 'set before showing the form
frm.Show vbModeless
End Sub
Userform:
Option Explicit
Private m_DPName As String
Private Sub UserForm_Activate()
Dim i As Long, v
'put some dummy data in the combobox
For i = 1 To 10
Me.DPComboBox.AddItem "ClientPartner_" & Format(i, "000")
Next i
Me.DPComboBox.Text = m_DPName 'set the value
End Sub
'call this before showing the form
Property Let DPName(nm As String)
m_DPName = nm
End Property

VBA check if all userform comboboxes have an option selected

I have a userform with many comboboxes and an "Ok" button. What I need is that when pressing that "Ok" button, VBA to check if there's no empty comboboxes. If all comboboxes have some value selected - close the userform otherwise return a message box and clicking "Ok" on that messagebox returns me to the userform with no filled values lost.
I've tried all the methods I could think of:
If PackageOuterRadius = null Then
if PackageOuterRadius is nothing Then
If PackageOuterRadius.value = 0 Then
If IsNull(PackageOuterRadius) = True Then
If IsNull(PackageOuterRadius.value) Then
What I've been trying to do is:
Private Sub Rigid_Filme_Ok_Button_Click()
If PackageOuterRadius.ListCount = 0 Then
MsgBox "Select a ""Package Outer Radius!"
End If
And absolutelly nothing actually checks if the combobox is empty and keeps returning a positive (That there's a value selected)
What could be the solution to this problem? Could someone, please, help me?
If you have few ComboBoxes only, you may insert lines underneath the OK button to check if all the ComboBoxes are filled but if you have too many ComboBoxes on UserForm, you may achieve this with the help of a Class Module and to do so, follow these steps...
Insert a Class Module and rename it to clsUserForm and the place the following code on Class Module...
Code for Class Module:
Public WithEvents mCMB As msforms.ComboBox
Public Sub CheckComboBoxes()
Dim cCtl As msforms.Control
Dim cntCBX As Long
Dim cnt As Long
For Each cCtl In frmMyUserForm.Controls
If TypeName(cCtl) = "ComboBox" Then
cntCBX = cntCBX + 1
If cCtl.Value <> "" Then
cnt = cnt + 1
End If
End If
Next cCtl
If cnt = cntCBX Then
Unload frmMyUserForm
Else
MsgBox "All the ComboBoxes are mandatory.", vbExclamation
End If
End Sub
Then place the following code on UserForm Module. The code assumes that the name of the userForm is frmMyUserForm and the name of the CommandButton is cmdOK.
Code for UserForm Module:
Dim GroupCBX() As New clsUserForm
Dim frm As New clsUserForm
Private Sub cmdOK_Click()
frm.CheckComboBoxes
End Sub
Private Sub UserForm_Initialize()
Dim i As Long
Dim ctl As Control
For Each ctl In Me.Controls
If TypeName(ctl) = "ComboBox" Then
i = i + 1
ReDim Preserve GroupCBX(1 To i)
Set GroupCBX(i).mCMB = ctl
End If
Next ctl
End Sub
And you will be good to go.

VBA: Prevent Autocomplete in combobox while

I currently have a userform that has a combobox and a listbox. Both contain the same list of items (the combobox has an extra null value). If a user selects an item in the combobox, then the same item will be selected in the list box.
The problem that I am having is coming from the combobox autocompleting when the user tries typing a value instead of choosing one from the combobox. When the user types a value, it will autocomplete what they typed to a value within the combobox. (If I type "8" then the combobox will autocomplete that to "8184123".)
If I set MatchEntry to 2 - fmMatchEntryNone, then the combobox does not autocomplete. However, the combobox does not select a value based on what the user has typed.
Is there any way to stop the combobox from autocompleting while keeping letting MatchEntry stay at 1 - fmMatchEntryComplete? Or is there anyway to implement fmMatchEntryComplete only when the value that the user enters is exactly equal to a value in the list of the combobox?
you can have ComboBox methods and properties work for you
in the form calling sub place:
Sub main()
' code that preceeeds the userform loading...
With UserForm1
'other code to set some userform or userform controls properties...
.ComboBox1.MatchEntry = fmMatchEntryNone ' <--| set this just before showing userform
.Show
End With
Unload UserForm1
' code that follows the userform closing...
End Sub
in the userform code pane place this function:
Function CheckCB(cboBox As MSForms.ComboBox) As Boolean
With cboBox
.Text = .Value '<-- This is the "trick": "refresh" the combobox text
CheckCB = .MatchFound
If Not CheckCB Then
MsgBox "Invalid entry", vbCritical
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End If
End With
End Function
despite MSDN online doc the refreshing of .Value actually has MatchEntry and MatchRequired work on it even if MatchEntry is set to fmMatchEntryNone
then you have to call CheckCB() function to prevent leaving userform until your combobox has been entered a valid value
for instance you could place it in any "exit" button click event handler
Private Sub CommandButton1_Click()
If Not CheckCB( ComboBox1 ) Then Exit Sub '<-- if ComboBox check failed then exit
' otherwise let code run ...
End Sub
or, if you wanted the user not to enter any other control until your combobox has a valid value, you must act similarly for every other userform control event handler, i.e. placing If Not CheckCB Then Exit Sub at their beginning
Try using Form1.ComboBox1.AutoWordSelect = True
Custom Autocomplete
Turn off match entry
ComboBox1.MatchEntry = fmMatchEntryNone
When the user types iterate over the combo's list. It there is only 1 possible match, select it.
Private Sub ComboBox1_KeyUp(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
Dim i As Integer, matchIndex As Integer
matchIndex = -1
For i = 0 To ComboBox1.ListCount - 1
If InStr(1, ComboBox1.List(i), ComboBox1.Value) Then
If matchIndex = -1 Then
matchIndex = i
Else
Exit Sub
End If
End If
Next
If matchIndex > -1 Then ComboBox1.ListIndex = matchIndex
End Sub

When I double click on a cell to open a form, a checkbox on the form behind my cursor is checked/unchecked. How can I prevent this from happening?

I have a form that appears on a double click event of a specific cell.
The form contains a list box with a bunch of checkboxes in it and in my _Activate() sub, the checkboxes are set to true or false based on values on the active sheet.
The trouble is that when the form opens up behind the cursor, the second click of the double click that opens the form is also checking/unchecking a checkbox in the form.
I've tried sticking "DoEvents" in the activate sub before the code sets the checkbox values but it hasn't made a difference - The checkbox behind my cursor where the form opens will be checked/unchecked.
I don't expect that the code will help much but it is essentially as below:
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
If Target = Range("aParticularRangeName") Then
frmSelectStuff.Show
End If
End Sub
Public Sub UserForm_Activate()
Dim iRegions As Integer
Dim sRecheck As Variant
Dim sRecheckList() As String
sRecheckList = Split(ActiveCell.Value, "; ")
For Each sRecheck In sRecheckList
For iRegions = 0 To lbRegionsTemp.ListCount - 1
If sRecheck = lbRegionsTemp.List(iRegions) Then lbRegionsTemp.Selected(iRegions) = True
Next
Next
End Sub
What about using Cancel = True?
If Target = Range("aParticularRangeName") Then
Cancel = True
frmSelectStuff.Show
End If

how to prompt sheet selection in excel vba

When a workbook has many sheets such as 100, you can activate the sheet quickly by right-clicking the arrows on the bottom left and then a prompts shows up by lets you choose which sheet you want to select. However, I want to be able to reach this prompt without clicking and using just the keyboard. So I want to create a simple macro where I can quickly pull up this prompt by assigning to something like ctrl+g. However, I do not know how to do this in vba.
Build your replica Activate form: Insert a userform into your project. Set its caption to "Activate". Add a label to it that says "Activate:". Add a Listbox and two buttons. Label one button Ok and the other Cancel.
In the UserForm_Initialize sub of the userform, Loop through the names of your sheets and add them to the listbox....something like:
Private Sub UserForm_Initialize()
Dim oSheet As Worksheet
For Each oSheet In Worksheets
ListBox1.AddItem oSheet.Name
Next
End Sub
Private Sub CommandButton1_Click()
Sheets(ListBox1.Text).Activate
Unload Me
End Sub
Private Sub CommandButton2_Click()
Unload Me
End Sub
Now in the ThisWorkbook section of the VBA Project, add a sub to show your userform:
Private Sub ShowActivateForm()
UserForm1.Show
End Sub
Go back to Excel and click the Macros button on the developer tab. You should see a macro named ShowActivateForm. Select it and click Options to assign it your desired hotkey.