【发布时间】:2018-06-08 09:57:50
【问题描述】:
阅读了多个论坛并尝试了很多次,我仍然无法做到这一点。
我是 VBA 新手,所以请忍受丑陋的代码。 非常欢迎任何改进代码的建议
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
因此调查参与者的输入将像这样存储:
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
但我不想要这个列表。
我希望 VBA 根据它们各自的月份(即 1 月、2 月、3 月、4 月......)将这些记录发送到不同的工作表。
在第 3 列最后一个条目下总结总支出。
-
如果有更多选择,以下部分不实用...对于这项工作,我很幸运只有四月六月九月和十一月...非常欢迎任何改进这条线的建议..
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
非常感谢您的建议!
【问题讨论】: