乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > excel文本框-VBA|使用窗体控件05:接收用户输入的文字框

excel文本框-VBA|使用窗体控件05:接收用户输入的文字框

作者:乔山办公网日期:

返回目录:excel表格制作

文字框控件主要用来接收用户输入的信息,也可用其显示信息供用户修改。文字框控件可接收用户输入的文本、数字、单元格引用或公式等类型的数据。


下面以一个“用户信息登记窗体”的实例来说明文字框控件的使用:


1 新建窗体并插入如下控件:


2 输入调用窗体代码

Sub 数据输入()


frmInput.Show


End Sub


2 在frmInput窗体编写如下代码:

Sub add()


Dim intRow As Integer


With Sheets("登记表")


intRow = .Range("A1").CurrentRegion.Rows.Count '获取已有数据行数


intRow = intRow + 1 '将用户窗体上的数据填充到新行上


.Cells(intRow, 1) = txtName.Value '姓名


If optMan.Value Then


.Cells(intRow, 2) = "男"


Else


.Cells(intRow, 2) = "女"


End If


.Cells(intRow, 3) = txtNation.Value '民族


.Cells(intRow, 4) = txtBirthday.Value '出生年月


.Cells(intRow, 5).NumberFormatLocal = "@" '设置身份证列为文本格式


.Cells(intRow, 5) = txtID.Value '身份证号码


.Cells(intRow, 6).NumberFormatLocal = "@" '设置联系电话为文本格式


.Cells(intRow, 6) = txtPhone.Value '联系电话


.Cells(intRow, 7) = txtAddress.Value '通信地址


.Cells(intRow, 8) = txtMemo.Value '备注


End With


End Sub


Private Sub cmdSave_Click()


If txtName.Value = "" Then


MsgBox "请输入姓名!", , "提示"


txtName.SetFocus


Exit Sub


End If


add


End Sub


Private Sub txtBirthday_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)


If Not IsDate(txtBirthday.Value) Then


MsgBox "请输入正确的出生年月!", , "提示"


txtBirthday.SelStart = 0


txtBirthday.SelLength = Len(txtBirthday.Value)


Cancel = True


End If


End Sub


Private Sub txtID_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)


Dim strId As String '暂存身份证号


strId = txtID.Value


If Len(strId) <> 15 And Len(strId) <> 18 Then


MsgBox "身份证号码位数错误,只能为15位或18位!", , "提示"


txtID.SelStart = 0


txtID.SelLength = Len(strId)


Cancel = True


End If


End Sub


Private Sub cmdClose_Click()


Unload Me '卸载窗体


End Sub


运行过程“数据输入”后,调用“输入数据”窗体,输入数据单击“登记”后,即可将数据插入到工作表”登记表“。


-End-


本文标签:excel文本框(80)

相关阅读

关键词不能为空

ppt怎么做_excel表格制作_office365_word文档_365办公网