I have the following two codes on a button: The (first code) aims to submit value of option button into worksheet cell:
For Each FormControl In Me.Controls
'Check only OptionButtons
If TypeName(FormControl) = "OptionButton" Then
'Check the status of the OptionButton
If FormControl.Value = True Then
'Set a variable equal to the Caption of the selected OptionButton
OptionButtonValue = FormControl.Caption
'We found the selected OptionButton so exit the loop.
Exit For
End If
End If
Next
'Store input in the worksheet
Sheets("Answer Sheet").Range("E80").Value = OptionButtonValue
To ensure an option button is selected before proceeding to next form, i have
the 'following code (second code):
Dim cnt As Integer
For Each ctl In Me.Controls
If TypeName(ctl) = "OptionButton" Then
If ctl.Value = True Then cnt = cnt + 1
End If
Next ctl
If cnt = 0 Then MsgBox "Hello " & CStr(ThisWorkbook.Sheets("AccessReg").Range("D630").Value) & ", you
have not selected an answer! Please select an answer to proceed to next question. Thank you.",
vbInformation, "Please select an answer!" Else ScoreBoards.Show
Unload Me
MY CHALLENGES
Both codes above exists in my forms i.e. questions 1, 2, 3,...respectively, but can't seem to get the second code (that, which ensures an option button is selected before next form can be opened) to work by adding 'unload me' to the end of it, yet i want the form closed before proceeding to next. Adding 'unload me', pop-up the msgbox (which tells me to select an answer) but when i clicked okay on the msgbox, it closes the form (Question1) instead of returning me to same form to ensure an answer is clicked, then proceed to next form (Question2). However, when i remove the 'unload me', things work fine i.e. the msgbox popup when selection not made, returns to same form when okay on msgbox is clicked, and opens next form when selection made.
What i really want is: i want the second code above (which ensures an option button is selected before next form can be opened) to work as programmed and each form closed before proceeding to the next form.
Thank you in advance
PS:
The concept summary is:
On a userform (Question1), select an option button and submit the value to worksheet
Ensure an option button is selected:
if selected and button clicked, the next form(being Question2) should open
if not selected and button clicked, the msgbox (which tells me to select an answer), should popup
clicking okay on the msgbox, should return me to same form (Question1) so that i can select an option and proceed.
Try adapting your second code in the next way, please:
If cnt = 0 Then MsgBox "Hello " & _
CStr(ThisWorkbook.Sheets("AccessReg").Range("D630").Value) & ", you have not selected an answer! Please select an answer to proceed to next question. Thank you.", vbInformation, "Please select an answer!"
Exit For
Else
ScoreBoards.Show
Unload Me
End if
Related
I have a userform with a listbox displaying all items within a table.
the listbox has a double click event where;
if the row double clicked contains text it opens an edit form.
if the row double clicked is empty then a message box displays stating "the item is not valid for editing."
I'm looking to add a new feature to this where if a certain cell along the row contains "closed" a message box displays stating "this item is closed and not valid for editing.
I'm not very good at VBA and I'd appreciated any help that can be offered. below is the current code I have.
Private Sub RiskLogReviewListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
'Checks if the selected row is empty and outputs a message box if it is
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) = 0 Then _
MsgBox "The selected item is empty and not a valid entry for editing"
'Checks if the selected row is closed and outputs a message box if it is
If RiskLogReviewListBox.Column(11, 0) = "closed" Then _
MsgBox "The selected item is closed and not a valid entry for editing"
'Checks if the selected row is populated
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) > 0 Then
Thanks in advance :)
found a solution
Private Sub RiskLogReviewListBox_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim i As Long
'Checks if the selected row is empty and outputs a message box if it is
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) = 0 Then _
MsgBox "The selected item is empty and not a valid entry for editing"
'Checks if the selected row is closed and outputs a message box if it is
If (RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 10)) = "closed" Then
MsgBox "The selected item is closed and not a valid entry for editing"
Exit Sub
Else
'Checks if the selected row is populated
If Len(Trim(RiskLogReviewListBox.List(RiskLogReviewListBox.ListIndex, 0))) > 0 Then
Replace
If RiskLogReviewListBox.Column(11, 0) = "closed" Then _
with
If Instr(RiskLogReviewListBox.Column(11, 0), "closed") > 0 Then _
But, I suppose that your code must exist in case of the two above situation. So, I would also suggest using of Exit Sub after the messages...
I have a series of ComboBoxes that are populated based on the previous ComboBox selection. So for example, ComboBox3 is populated based on the value selected in ComboBox2. The "trigger" for populating ComboBox3 is the DropButtonClick action. I have a message box pop up when there is no value in ComboBox2. This is working successfully - code below.
If Me.ComboBox2.ListIndex = -1 Then
MsgBox "Please select all preceding comboboxes"
ComboBox3.Value = ""
Exit Sub
Else
sh.Range("B2") = Me.ComboBox2.Value
End If
My issues is once the Message Box appears (as a result of there being no value in ComboBox2) ComboBox3 still displays drops down values. Is there a way to undo the ComboBox3 DropButtonClick Event when there is no value in ComboBox2, so that ComboBox3 never drops down?
You can immediately close the drop-down window of the combo-box by simulating the "ESC" key:
MsgBox "Please select all preceding comboboxes"
ComboBox3.Value = ""
' close immediately the combo's dropdown window
SendKeys "{ESC}{ESC}"
Is this something for you?
If Me.ComboBox2.Value = "" Then
ComboBox2.SetFocus
Else
sh.Range("B2") = Me.ComboBox2.Value
End If
When you want to click on the dropbutton of combobox3 it automaticly goes back to combobox2.
Update: After more research, I found this duplicate question: Excel 2016 Workbook.BeforeClose event firing every other time bug. It seems I was using the wrong keywords, and that this is a bug, not a problem with my code. However, I cannot seem to download the version mentioned in the solution. I am running Windows 7 and using Microsoft Office 365 Pro Plus, and Office is stating that the most up to date version available is 16.0.6965.2105
I am trying to use the Workbook_BeforeClose event to test whether a checkbox is checked or not. If not, the user is prompted on whether they want to continue closing with out checking the box. If they choose "Yes," the sheet is cleared and the workbook is saved. If they choose, "No," the box is checked and, "Cancel" is set to true.
This works fine the first time the Workbook_BeforeClose event runs. However, the second time the sheet is closed, the standard Excel "Want to save your changes to..." dialogue box comes up and the Workbook_BeforeClose event does not fire. If I click cancel on the dialogue box and close the workbook a third time, the event fires. Something is being reset when, "Cancel," is clicked in the dialogue box, but I can't figure out what it is. My code is below:
Public Closing as Boolean
Sub Workbook_BeforeClose(Cancel As Boolean)
Debug.Print "Workbook_BeforeClose"
If Closing = True Then Exit Sub 'Closing is used as a switch to stop the event from looping on Application.ThisWorkbook.Close below
Closing = True
If Sheets(1).DraftCheckBox = False Then
If MsgBox("This file is not being saved as a draft. This workbook will be cleared if the draft box is not checked." & vbCr & vbCr & "Would you like to continue?", vbYesNo, "Warning") = vbYes Then
'If "Yes" selected
'Stuff happens here
Application.ThisWorkbook.Close savechanges:=True
Else
'If "No" selected
Sheets(1).DraftCheckBox = True
Cancel = True
End If
End If
Closing = False
End Sub
I know for a fact that Application.EnableEvents is set to True. I also know that the event itself is not firing because there is a stop on the very first line at "Debug.Print" This stop is activated after the first close and I can step through the code. It does not activate at all after the second close, either before or after the dialogue box. Is there anything that I should be doing to prevent the dialogue box from coming up and to force the Workbook_BeforeClose event every time the workbook is closed?
You are already closing the workbook you don't want to call close again, use Save instead.
You also don't need the extra variable if you use the code below. One big question remains...how is your user imitating the Close event? If by the red X then the code below should work. If by a button on your form what does that button code look like?
Note: This is untested code as I don't have your workbook.
Sub Workbook_BeforeClose(Cancel As Boolean)
If Sheets(1).DraftCheckBox = False Then
If MsgBox("This file is not being saved as a draft. " + _
"This workbook will be cleared if the draft box is not checked." _
& vbCr & vbCr & "Would you like to continue?", _
vbYesNo, "Warning") = vbYes Then
'If "Yes" selected
'Stuff happens here
Application.ThisWorkbook.Save
Cancel = False 'Just to be sure.
Else
'If "No" selected
Sheets(1).DraftCheckBox = True
Cancel = True
End If
End If
End Sub
HTH
The Goal:
When I switch pages in my VBA userform, I would like a certain field to be the first input value. And then I want them to switch sequentially when I tab through them.
What I have done so far:
The pages are created (for the most part, they still need an artistic mind to make it look like an engineer didn't do it), the logic is made, basically everything is done. The input fields tabIndex propertys are set starting at 20 and going up to 27 in the order I want them in. Pictures will be attached below.
The Problem:
When I push the next button, the userform automatically goes to the "Cooling system" drop down. If I push tab, it goes to the next dropdown. Then through the other options in a strange but constant order. And the weird thing is, I can type but no text appears. The cursor moves, but nothing comes behind it. After I have tabbed through everything, if I push tab again only THEN does it do what I want it to do. I have attached pictures and the code for the next button. Thanks for your help in advanced. Let me know if I can clarify anything!
The Page that works fine:
The problem page (I tried to show how nothing shows when you type and it goes to that drop down automatically):
Code for "Next" button:
Private Sub bNextSystem1_Click() 'checks to see if all parameters are entered
If IsNumeric(Me.txtS1elec.value) = True And IsNumeric(Me.txtS1NG.value) = True And IsNumeric(Me.txtS1sqft.value) = True Then
If Me.txtS1elec.value <> "" And Me.txtS1NG.value <> "" And Me.txtS1sqft.value <> "" And Me.ddS1Cooling.ListIndex > -1 And Me.ddS1Heating.ListIndex > -1 Then
NextPage
Me.txtS1elec.BackColor = vbWhite
Me.txtS1NG.BackColor = vbWhite
Me.txtS1sqft.BackColor = vbWhite
Else
MsgBox "Please check to see if all options are selected or entered."
GoTo CleanFail
End If
Else
HighlightBadCells1 'checks for incorrect cell input values
MsgBox "Please check the highlighted cells"
GoTo CleanFail
End If
Me.txtS2elec.value = Me.txtS1elec.value
Me.txtS2NG.value = Me.txtS1NG.value
CleanFail:
End Sub
Code for "NextPage" Sub routine:
Private Sub NextPage()
MultiPage1.Pages(MultiPage1.value + 1).Visible = True 'hides current page
MultiPage1.Pages(MultiPage1.value).Visible = False 'reveals next page
End Sub
Select the Multipage Page
CLick View -> Tab Oder
This will show the Tab Order Dialog.
I have an MS-Word 2013 document with several (legacy) formfields; some text boxes, some comboboxes. The first list item on all of the comboboxes is "(select one)" to let the end user know they need to make a selection (I did not draft or design this document, I've just been asked to write the VBA code for it). So I coded each to give a simple VBA message box, ran on exit, if that first selection was not changed, for example:
Public factor1 As Integer
Sub MyFormFieldFactor1()
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
Exit Sub
End If
If ActiveDocument.FormFields("cbofactor1").Result = "Yes" Then
factor1 = 1
Else
factor1 = 0
End If
End Sub
The word document automatically goes to the next formfield when you click ok on the message box. Through VBA, I want it to stay on the current formfield when "(select one)" is chosen. Bonus points if it stays on the current field and pulls up the list selection automatically.
Will this work:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
Exit Sub
End If
You can auto drop the list with something like:
SendKeys "%{down}", True
DoEvents
Full code:
If ActiveDocument.FormFields("cbofactor1").Result = "(select one)" Then
MsgBox "You must select either Yes or No."
ActiveDocument.FormFields("cbofactor1").SetFocus()
SendKeys "%{down}", True
DoEvents
Exit Sub
End If