前段时间想用VBA窗体录入信息,我做了一个,大致效果如下
点击录入之后,自动生成下表(下图为虚拟数据)
以下是制作过程及代码
首先在新建一个窗体,拖出需要的控件,给每个textbox都改成和旁边文本对应的名字
第二步,点开确定按钮在确定按钮上编写代码,以下是主要代码,如果有更好的思路可以评论或者私聊讨论哦~
Private Sub OK_Click()
If OptionButton1.Value = "" Then 'And OptionButton2.Value = "" And OptionButton3.Value = "" And OptionButton3.Value = ""
MsgBox "信息输入不完整!", 16, "错误提示"
OptionButton1.Value = ""
OptionButton2.Value = ""
OptionButton3.Value = ""
OptionButton4.Value = ""
End If
If 备注.Value = "" Or 产量.Value = "" Or 车牌号.Value = "" Then '检验信息是否输入
MsgBox "信息输入不完整!", 16, "错误提示"
备注.Value = "" '***********************确定清除原有数据
产量.Value = ""
车牌号.Value = ""
OptionButton1.Value = ""
OptionButton2.Value = ""
OptionButton3.Value = ""
OptionButton4.Value = ""
End If
Dim maxrow As Long
maxrow = ThisWorkbook.Worksheets(1).Cells(Rows.Count, 1).End(xlUp).Row
ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 1).Value = Now '*****************将文字框里的值传到单元格里
ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 3).Value = 备注.Value
ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 5).Value = 产量.Value
ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 4).Value = 车牌号.Value
If OptionButton1.Value Then ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 2) = OptionButton1.Caption
If OptionButton1.Value Then ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 6) = "10"
If OptionButton2.Value Then ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 2) = OptionButton2.Caption
If OptionButton2.Value Then ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 6) = "20"
If OptionButton3.Value Then ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 2) = OptionButton3.Caption
If OptionButton3.Value Then ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 6) = "30"
If OptionButton4.Value Then ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 2) = OptionButton4.Caption
If OptionButton4.Value Then ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 6) = "40"
ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 7).Value = "1"
If ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 6).Value = "" Then
ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 1).Value = "" '*****************将文字框里的值传到单元格里
ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 2).Value = ""
ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 3).Value = ""
ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 4).Value = ""
End If
If ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 6).Value = "" Then
MsgBox "信息输入不完整!", 16, "错误提示"
ThisWorkbook.Worksheets(1).Cells(maxrow + 1, 7).Value = ""
Else: MsgBox "您已完成输入"
End If
备注.Value = "" '***********************确定清除原有数据
产量.Value = ""
车牌号.Value = ""
End Sub