If one combobox is changed, reset the other combobox - excel

I have two activeX comboboxes on a sheet: One for month and one for quarter.
The month combobox includes a "---" option which is representative of no month selected.
I also use the "---" character in the quarter combobox.
I would like if the user selects a month, not "---", the quarter box resets to "---".
Similarly, if the user selects a quarter, the month combobox should reset to "---".
My code in the sheet ("shDashboard" is the CodeName of the sheet. The boxes are named Quarter_Combo and Month_Combo.):
Private Sub Month_Combo_Change()
MsgBox "Month_Combo"
shDashboard.Quarter_Combo.Value = "—"
End Sub
Private Sub Quarter_Combo_Change()
MsgBox "Quarter_Combo"
shDashboard.Month_Combo.Value = shDashboard.Month_Combo.List(0)
End Sub
The message boxes are there to help troubleshoot.
When I change the month, it will update the quarter combobox which in turn will update the month combo box.
The result is that both boxes display "---".
I tried Application.EnableEvents = False.

Application.EnableEvents doesn't work in this case. Here is a workaround
Private pbDisableComboEvents As Boolean
Private Sub Month_Combo_Change()
If Not pbDisableComboEvents Then
MsgBox "Month_Combo"
pbDisableComboEvents = True
Me.Quarter_Combo.Value = "-"
pbDisableComboEvents = False
End If
End Sub
Private Sub Quarter_Combo_Change()
If Not pbDisableComboEvents Then
MsgBox "Quarter_Combo"
pbDisableComboEvents = True
Me.Month_Combo.Value = "-"
pbDisableComboEvents = False
End If
End Sub

Related

Using text box value if user selects combobox value?

I have made a combo box within a userform:
private sub Userform_Activate
cmbLocation.AddItem "Field"
cmbLocation.AddItem "Remote"
cmbLocation.AddItem "Other"
end sub
when a user selects Other I'd like a text box to populate for free form text input. When the user does this, I'd like that text to be the .value that is populating into the worksheet table.
is this even possible?
Create a textbox and set it to .visible = False when the userform is activated. Then using an if statement you can output the data from either the combobox or the textbox.
Private Sub UserForm_Activate()
Me.txtLocation.Visible = False
Me.cmbLocation.AddItem "Field"
Me.cmbLocation.AddItem "Remote"
Me.cmbLocation.AddItem "Other"
End Sub
Private Sub cmbLocation_Change()
If Me.cmbLocation.Value = "Other" Then
Me.txtLocation.Visible = True
Else
Me.txtLocation.Visible = False
End If
End Sub
Private Sub CommandButton1_Click()
If Me.txtLocation.Visible = True Then
ThisWorkbook.Sheet1.Range("A1").Value = Me.txtLocation.Value
Else
Thisworkbook.Sheet1.Range("A1").Value = Me.cmbLocation.Value
End If
End Sub
Modify sheet and range as necessary

Transfer Dates from Userform to Database (Including Day Name)

This may or may not be possible but i have two textboxes (textBox20 and Textbox21... yes, i will change their names soon) and after i click a button, a third textbox displays the difference between both dates on days format. (All good so far.)
What i want is to be able to insert inside the database or... in another textbox and send it later to the same database, the day name which corresponds to each of both (textbox20 and textbox21), for example, if the day is: 20/02/2020, in the data base, it should show: thursday, 20/02/2020 or thursday, 20 of feb of 2020. If i do this in the same box, i can´t use the datediff function, and its a mess... I want something simple to transfer to the database, so later i can search for it and display it on another textbox later on... (im doing a little database)
Private Sub TextBox20_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'check date format
With TextBox20
If IsDate(TextBox20.Text) Then 'Format as desired.
TextBox20.Text = Format(TextBox20.Text, "dd/mm/yyyy")
Else
TextBox20.Text = "" 'Clear the TextBox
MsgBox "Please use the following format: DD/MM/YYYY."
Cancel = True
Exit Sub
End If
End With
End Sub
Private Sub TextBox21_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'check date format
With TextBox21
If IsDate(TextBox21.Text) Then 'Format as desired.
TextBox21.Text = Format(TextBox21.Text, "dd/mm/yyyy")
Else
TextBox21.Text = "" 'Clear the TextBox
MsgBox "Please use the following format: DD/MM/YYYY."
Cancel = True
Exit Sub
End If
End With
End Sub
Private Sub cmdCal_Click()
TextBox22.Value = DateDiff("d", (TextBox20.Text), (TextBox21.Text))
End Sub

Excel 2013 64 bit - VBA Userform - Add 28 days to date of textbox and display in another textbox

In my Userform I have a textbox "ADD_Inc_DATE_TXT" which the user manually enters the date of the incident.
I have another textbox "TxT_SWIRL_DueDate" which should display the result of the adding of 28 days to the date entered in the "ADD_Inc_DATE_TXT" textbox or even just add 1 month to the date of the incident.
I have tried this in a module named "Mod_SWIRL_Due_Date":
Sub SWIRL_ExpiryDate()
TxT_SWIRL_DueDate = CDate(ADD_Inc_DATE_TXT)
ADD_Inc_DATE_TXT = DateAdd("m", 1, ADD_Inc_DATE_TXT)
End Sub
But this doesn't seem to do anything at all.
I would like the "TxT_SWIRL_DueDate" to display the expiry date when the "ADD_INC_Time_TXT" textbox is selected.
And in anticipation of the User amending the Incident Date, I'd like it to update the TxT_SWIRL_DueDate with the amended date.
Further Information:
I have included below all the code I have relating to dates. I have 4 Date Pickers/Popup Calendars ... and none of the dates come up in the dd/mm/yyyy format. (having some problem with putting the code in the 'code sample' )
Private Sub Calendar1_Click()
ADD_Inc_DATE_TXT.value = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.LBL_Inc_Day_Type.Caption = Format(ADD_Inc_DATE_TXT.Text, "ddd")
End If
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.ADD_Inc_DATE_TXT.Text = Format(ADD_Inc_DATE_TXT.Text, "dd/mm/yyyy")
End If
End Sub
Private Sub Calendar2_Click()
TXT_AssetMgr_DATE = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.TXT_AssetMgr_DATE.Text = Format(TXT_AssetMgr_DATE.Text, "dd/mm/yyyy")
End If
End Sub
Private Sub Calendar3_Click()
TXT_LastUserDATE = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.TXT_LastUserDATE.Text = Format(TXT_LastUserDATE.Text, "dd/mm/yyyy")
End If
End Sub
Private Sub Calendar4_Click()
ADD_Date_ServiceJobLogged_TXT = CalendarForm.GetDate
If IsDate(ADD_Inc_DATE_TXT.Text) Then
Me.ADD_Date_ServiceJobLogged_TXT.Text = Format(ADD_Date_ServiceJobLogged_TXT.Text, "dd/mm/yyyy")
End If
End Sub
‘Under Private Sub UserForm_Initialize() I have the following date related code:
Private Sub UserForm_Initialize()
Me.ADD_Date_Recorded_TXT.value = Format(Now, "dd/mm/yyyy") ‘ this works perfectly (correct format is returned)
Me.ADD_Time_Recorded_TXT.Text = Format(Now(), "HH:mm")
‘***Note: my system date is dd/mm/yyyy and in the spreadsheet cells, the format is set to dd/mm/yyyy
Use this code in the form's code module (right click the text box, "View code"):
Private Sub ADD_Inc_DATE_TXT_AfterUpdate()
TxT_SWIRL_DueDate.Value = Format(DateAdd("m", 1, CDate(ADD_Inc_DATE_TXT.Value)),"dd/mm/yyyy")
End Sub
I recommend useing the AfterUpdate event, as the Change event fires every time the value in the textbox gets changed - eg also while typing. This only fires when the user has finished typing, and moved to the next tiem on the form (clicks somewhere else).
Also consider using some form of date control instead of textbox control. There are quite a few out there, and depending on the Excel version you are using, you may only be able to use a select few of those.

before_print macro check required fields are not blank

I have the following macro to execute a before_print check. There are certain fields that must be populated in order for the user to print the template. The macro works fine but the message box will appear as many times as there is a blank field. Meaning if 3 of the 5 fields are blank then the message box will appear (3) times which means the user will have to close each message box.
Question: I would like to see what I would need to modify so that the message box only appears once regardless of how many of the required fields are left blank. All I care about is if any of the fields are blank to show the message box and cancel the print job.
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ActiveSheet.Name = "Template" Then
Dim jRange As Range
Set jRange = Sheets("Template").Range("C4,C5,B9,B10,B11")
For Each cell In jRange
If cell.Value = "" Then
MsgBox ("Cannot leave Invoice Number, Invoice Date or Vendor Name blank."), vbCritical
Cancel = True
End If
Next
End If
End Sub
Revised macro after assistance:
Private Sub Workbook_BeforePrint(Cancel As Boolean)
If ActiveSheet.Name = "Template" Then
Dim jRange As Range
Set jRange = Sheets("Template").Range("C4,C5,B9,B8,B10")
Dim ReqFields As Boolean
For Each cell In jRange
If cell.Value = "" Then
ReqFields = True
End If
Next
If ReqFields Then
MsgBox ("Cannot leave Invoice Number, Invoice Date or Vendor Name blank."), vbCritical
Cancel = True
End If
End If
End Sub
Instead of showing a MsgBox each time through the loop, set a Boolean variable to "True". After the loop, if the Boolean is true, then you know that there was at least one field blank. At that point, show your error message and set "Cancel = True".

Disable or hide options in combo box VBA 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

Resources