在阅读了多个论坛并多次尝试后,我仍然无法实现这一目标。
我是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列最后一个条目下的总支出。
如果有更多的选择,以下部分是不切实际的...对于这项工作我很幸运只有4月6月9月和11月...任何改善这条线的建议都非常受欢迎..
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
非常感谢您的建议!
答案 0 :(得分:2)
首先,我建议重命名TextBoxes和ComboBoxes。
TextBox1
是一个非常糟糕的名字,因为没有人知道这意味着什么。最好使用txtInputDay
和cmbInputMonth
之类的内容,这样更容易阅读/理解和维护。
如果您检查日期是否有效,则需要编写大量代码,并且它不反映闰年,其中2月只有29天而不是28天。我建议让用户输入整个日期,如果是有效日期,请使用IsDate Function进行检查。
如果您仍然希望自己测试,我建议将所有这些消息减少为“您输入的日期无效日期”。这样就更容易为所有内容提供单独的消息(为空,是一个数字,在1到28/29/30/31之间)。
例如
Dim InputDay As Long
InputDay = Val(Me.TextBox1.Value)
Dim InputMonth As String
InputMonth = Me.ComboBox1.Value
Dim MaxDaysInMonth As Long
'get the max days in the selected month
Select Case InputMonth
Case "April", "June", "September", "November" 'Month with only 30 days
MaxDaysInMonth = 30
Case "February" '28 days or 29 on leap years
If Month(DateSerial(Year(Date), 2, 29)) = 2 Then 'is it a leap year
MaxDaysInMonth = 29
Else
MaxDaysInMonth = 28
End If
Case Else 'all other months have 31 days
MaxDaysInMonth = 31
End Select
'check if input exceeds max days of month
If InputDay < 1 Or InputDay > MaxDaysInMonth Then
MsgBox "Date is not valid"
Exit Sub
End If
如果您希望不同工作表中的每个数据都可以访问
这样的表格Dim MonthSheet as Worksheet
Set MonthSheet = Worksheets(InputMonth) 'take name of the selected month as sheet name
Dim n As Long
n = MonthSheet.Range("A" & MonthSheet.Rows.Count).End(xlUp).Row 'find last used row in month sheet
MonthSheet.Range("A" & n + 1).Value = … 'fill in some data