vba建立窗體例子

一、窗體設計

image.png

二、窗體代碼

Option Explicit


Private Sub UserForm_Initialize()
    '設置複合框的條目內容爲"男"和"女"
    性別.List = Array("男", "女")
End Sub

Private Sub 肯定_Click()
'判斷信息是否輸入完整
    If 姓名.Value = "" Or 性別.Value = "" Or 出生年月.Value = "" Then
        MsgBox "信息輸入不完整,請從新輸入!", vbExclamation, "錯誤提示"
        Exit Sub                '退出執行程序
    End If
    Dim xrow As Integer
    xrow = Range("A1").CurrentRegion.Rows.Count + 1    '求第一條空行行號
    '將姓名、性別、出生年月寫入第一條空行
    Cells(xrow, "A") = 姓名.Value
    Cells(xrow, "B") = 性別.Value
    Cells(xrow, "C") = 出生年月.Value
    '內容寫進工做表後,將控件中的內容清除
    姓名.Value = ""
    性別.Value = ""
    出生年月.Value = ""
End Sub

Private Sub 退出_Click()
     Unload Me      '卸載錄入窗體
End Sub

三、模塊代碼

Option Explicit
Sub XianShi()
    Load 錄入      '裝載「錄入」窗體
    錄入.Show      '顯示「錄入」窗體
End Sub
相關文章
相關標籤/搜索