我正在开发一个自创的 VBA 表单。它有提交按钮,并接受目的地、时间和预算等条目。
我有一个名为“巴厘岛”的 Excel 工作表。它包含目的地可用的航空公司、时间和预算。
我想创建一种方法,当用户单击提交按钮时,可以获取插入的目的地,例如 bali,然后匹配它以在另一个新工作表或表单中显示名为“bali”的工作表的内容。
有人可以提供分步代码来帮助我吗?
答案1
这是我所拥有的简单表单的参考代码。
Private Sub cmdCancel_Click() 卸载我 End Sub
Private Sub ResetComboBox_Click() Dim ctl As Control For Each ctl In Me.Controls 如果 TypeName(ctl) = "TextBox" 或 TypeName(ctl) = "ComboBox" 则 ctl.Value = "" ElseIf TypeName(ctl) = "CheckBox" 则 ctl.Value = False End If Next ctl End Sub
Private Sub SubmitComboBox_Click() 如果 Me.DestinationComboBox.Value = "" 则 MsgBox "请选择目的地。", vbExclamation, "旅行目的地" Me.DestinationComboBox.SetFocus Exit Sub End If
If Me.DateTextBox.Value = "" Then
MsgBox "Please select a date.", vbExclamation, "Travel Date"
Me.DateTextBox.SetFocus
Exit Sub
End If
If Not IsDate(Me.DateTextBox.Value) Then
MsgBox "The Date field must contain a date.", vbExclamation, "Travel Date"
Me.DateTextBox.SetFocus
Exit Sub
End If
If Me.BudgetTextBox.Value = "" Then
MsgBox "Please insert your budget.", vbExclamation, "Travel Budget"
Me.BudgetTextBox.SetFocus
Exit Sub
End If
If Not IsNumeric(Me.BudgetTextBox.Value) Then
MsgBox "The budget field must contain number.", vbExclamation, "Travel Budget"
Me.BudgetTextBox.SetFocus
Exit Sub
End If
Dim RowCount As Long RowCount = Worksheets("Parameters").Range("A1").CurrentRegion.Rows.Count With Worksheets("Parameters").Range("A1") .Offset(RowCount, 0).Value = Me.DestinationComboBox.Value .Offset(RowCount, 1).Value = Me.DateTextBox.Value .Offset(RowCount, 2).Value = DateValue(Me.DateTextBox.Value) .Offset(RowCount, 3).Value = Me.BudgetTextBox.Value .Offset(RowCount, 4).Value = Me.BudgetTextBox.Value .Offset(RowCount, 5).Value = Format(Now, "dd/mm/yyyy hh:nn:ss") End With
Dim ctl As Control For Each ctl In Me.Controls 如果 TypeName(ctl) = "TextBox" 或 TypeName(ctl) = "ComboBox" 则 ctl.Value = "" ElseIf TypeName(ctl) = "CheckBox" 则 ctl.Value = False End If Next ctl End Sub