其实不难懂,我注释一下
Sub Button1_Click()
Dim Entry1 As Variant, Entry2 As Variant
Dim Msg1 As Variant, Msg2 As Variant
Dim NextRow As Variant
Dim reg, s$
CreateObject ("vbscript.regexp")
Set reg = CreateObject("vbscript.regexp")
reg.Pattern = "^[A-Za-z]+$" ‘上面三句为正则,是为了判断输入值是否全部为字母
Msg1 = "Enter the name"
Msg2 = "Enter the amount"
Do
NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
Do ‘这个do语句一直运行,要求输入,直到reg.Test(Entry1)值为true
Entry1 = Application.InputBox(prompt:=Msg1, Type:=2) ’不用原来的函数,用inputbox方法,输入框点取消时entry值为false
If Entry1 = False Then Exit Sub ' 点取消退出程序
Loop Until reg.Test(Entry1) ’ 判断输入值是否全部为字母,不是的话,再次运行要求输入。
Entry2 = Application.InputBox(prompt:=Msg2, Type:=1) ’inputbox方法,type=1为只接受数字输入,不是数字会要求重输。
If Entry2 = False Then Exit Sub ‘点取消退出程序。
Cells(NextRow, 1) = Entry1
Cells(NextRow, 2) = Entry2
Loop
End Sub
主要问题是判断entry1输入的全部是字母,没有别的办法,只有用正则,我也不懂,抄的爱疯的帖子中的语句。