After reading multiple forums and tried many times I still couldn't achieve this.
I'm new to VBA, so please bear with the ugly code. Any advice to improve the code is very much welcome
Private Sub CommandButton1_Click()
''''''validation''''''
Me.ComboBox1.Style = 2
If Me.TextBox1.Value = "" And Me.TextBox2.Value = "" And Me.TextBox3.Value = "" Then
MsgBox "You're entering an empty form."
Exit Sub
End If
If Me.TextBox1.Value = "" Then
MsgBox "Don't forget to enter the day of the month.", vbCritical
Exit Sub
End If
If Me.TextBox1.Value > 31 Then
MsgBox "Please enter the correct date", vbCritical
Exit Sub
End If
If Me.TextBox1.Value > 29 And Me.ComboBox1.Value = "February" Then
MsgBox "there's no 30 or 31 days in February", vbCritical
Exit Sub
End If
If Me.TextBox1.Value = 31 And Me.ComboBox1.Value = "April" Then
MsgBox "there is only 30 days in this month", vbCritical
Exit Sub
End If
If Me.TextBox1.Value = 31 And Me.ComboBox1.Value = "June" Then
MsgBox "there is only 30 days in this month", vbCritical
Exit Sub
End If
If Me.TextBox1.Value = 31 And Me.ComboBox1.Value = "September" Then
MsgBox "there is only 30 days in this month", vbCritical
Exit Sub
End If
If Me.TextBox1.Value = 31 And Me.ComboBox1.Value = "November" Then
MsgBox "there is only 30 days in this month", vbCritical
Exit Sub
End If
If VBA.IsNumeric(Me.TextBox1.Value) = False Then
MsgBox "Please enter a number for date.", vbCritical
Exit Sub
End If
If Me.ComboBox1.Value = "" Then
MsgBox "Don't forget the month.", vbCritical
Exit Sub
End If
If Me.TextBox2.Value = "" Then
MsgBox "Don't forget to enter the amount of money you've spent.", vbCritical
Exit Sub
End If
'''set that the sheet will find the next empty row'''
n = sh.Range("A" & Application.Rows.Count).End(xlUp).Row
'''set that the sheet will find the next empty row'''
sh.Range("A" & n + 1).Value = Me.TextBox1 & Me.ComboBox1 & Me.Label3
If Me.OptionButton1.Value = True Then sh.Range("b" & n + 1).Value = "Cash"
If Me.OptionButton2.Value = True Then sh.Range("b" & n + 1).Value = "Card"
sh.Range("c" & n + 1).Value = Me.TextBox2
sh.Range("d" & n + 1).Value = Me.TextBox3
''''clear the box after submission'''''
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.ComboBox1.Value = ""
Me.OptionButton1.Value = False
Me.OptionButton2.Value = False
MsgBox "Thank You!"
''''clear the box after submission'''''
End Sub
Private Sub CommandButton2_Click()
Me.TextBox1.Value = ""
Me.TextBox2.Value = ""
Me.TextBox3.Value = ""
Me.ComboBox1.Value = ""
Me.OptionButton1.Value = False
Me.OptionButton2.Value = False
End Sub
Private Sub UserForm_Activate()
With Me.ComboBox1
.Clear
.AddItem ""
.AddItem "January"
.AddItem "February"
.AddItem "March"
.AddItem "April"
.AddItem "May"
.AddItem "June"
.AddItem "July"
.AddItem "August"
.AddItem "September"
.AddItem "October"
.AddItem "November"
.AddItem "December"
End With
End Sub
So the input by survey participants will be stored like this:
12-Feb-2018 Cash 12.99 Food
13-Feb-2018 Cash 4.95 Train
14-Feb-2018 Card 19.99 Movie
14-Feb-2018 Cash 36.95 Clothes
1-Mar-2018 Cash 18.99 Grocery
29-Mar-2018 Cash 20.00 Petrol
2-Apr-2018 Card 49.99 Hardware
But i do not want this list.
I want the VBA to send these records to different worksheets based on their respective months i.e. January, February, March, April....
Sum up the total spending beneath the last entry of the 3rd column.
the following part is not practical if there are a lot more options... for this work I'm lucky there are only April June September and November...any suggestion to improve this line is very much welcome..
If Me.TextBox1.Value = 31 And Me.ComboBox1.Value = "April" Then MsgBox "there is only 30 days in this month", vbCritical Exit Sub End If
Thanks so much for your advice!