Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 2148|回复: 5

[已解决]怎么样对单元格输入内容进行有效性验证

[复制链接]
发表于 2017-3-21 10:51 | 显示全部楼层 |阅读模式
本帖最后由 Lydia_Li 于 2017-3-27 08:53 编辑

求助贴 ,相对单元格输入内容进行有效性验证,请看附件~谢谢各位

Sub Button1_Click()
       Dim Entry1 As Variant, Entry2 As Variant
       Dim Msg1 As Variant, Msg2 As Variant
       Dim NextRow As Variant
       NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
       Msg1 = "Enter the name"
       Msg2 = "Enter the amount"
  Do
       Entry1 = InputBox(Msg1)
       Entry2 = InputBox(Msg2)
       If Entry1 = "" Then Exit Sub
       If Entry2 = "" Then Exit Sub
       If IsNumeric(Entry1) Then
             Msg1 = "Your previous is INVALID"
             Msg1 = Msg1 & vbNewLine
             Msg1 = Msg1 & "Enter the name"
       End If
       If Not IsNumeric(Entry2) Then
             Msg2 = "Your previous is INVALID"
             Msg2 = Msg2 & vbNewLine
             Msg2 = Msg2 & "Enter the amount"
       End If
            Cells(NextRow, 1) = Entry1
            Cells(NextRow, 2) = Entry2
    Loop
End Sub

最佳答案
2017-3-22 14:43
Lydia_Li 发表于 2017-3-22 13:47
非常感谢老师的耐心解答。我才开始学习VBA,您写的程序暂时我还有些接受困难,

能否请老师再修改下程 ...

其实不难懂,我注释一下
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输入的全部是字母,没有别的办法,只有用正则,我也不懂,抄的爱疯的帖子中的语句。

求助-进行有效性验证.zip

14.52 KB, 下载次数: 3

发表于 2017-3-21 15:54 | 显示全部楼层
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]+$"
       NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
       Msg1 = "Enter the name"
       Msg2 = "Enter the amount"
  
  Do
    Do
       Entry1 = Application.InputBox(prompt:=Msg1, Type:=2)
       If Entry1 = False Then Exit Sub
      
    Loop Until reg.Test(Entry1)
       Entry2 = Application.InputBox(prompt:=Msg2, Type:=1)
       If Entry1 = "" Then Exit Sub
       If Entry2 = "" Then Exit Sub
       If IsNumeric(Entry1) Then
             Msg1 = "Your previous is INVALID"
             Msg1 = Msg1 & vbNewLine
             Msg1 = Msg1 & "Enter the name"
       End If
       If Not IsNumeric(Entry2) Then
             Msg2 = "Your previous is INVALID"
             Msg2 = Msg2 & vbNewLine
             Msg2 = Msg2 & "Enter the amount"
       End If
            Cells(NextRow, 1) = Entry1
            Cells(NextRow, 2) = Entry2
    Loop
End Sub
回复

使用道具 举报

发表于 2017-3-21 15:58 | 显示全部楼层
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]+$"
       NextRow = Cells(Rows.Count, 1).End(xlUp).Row + 1
       Msg1 = "Enter the name"
       Msg2 = "Enter the amount"
  
  Do
    Do
       Entry1 = Application.InputBox(prompt:=Msg1, Type:=2)
       If Entry1 = False Then Exit Sub
      
    Loop Until reg.Test(Entry1)
       Entry2 = Application.InputBox(prompt:=Msg2, Type:=1)
      
       If Entry2 = False Then Exit Sub

            Cells(NextRow, 1) = Entry1
            Cells(NextRow, 2) = Entry2
    Loop
End Sub

优化版
回复

使用道具 举报

发表于 2017-3-21 16:20 | 显示全部楼层
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
       Entry1 = Application.InputBox(prompt:=Msg1, Type:=2)
       If Entry1 = False Then Exit Sub
      
    Loop Until reg.Test(Entry1)
       Entry2 = Application.InputBox(prompt:=Msg2, Type:=1)
      
       If Entry2 = False Then Exit Sub

            Cells(NextRow, 1) = Entry1
            Cells(NextRow, 2) = Entry2
    Loop
End Sub
修改一下
回复

使用道具 举报

 楼主| 发表于 2017-3-22 13:47 | 显示全部楼层
wenzili 发表于 2017-3-21 16:20
Sub Button1_Click()
       Dim Entry1 As Variant, Entry2 As Variant
       Dim Msg1 As Variant, Ms ...

非常感谢老师的耐心解答。我才开始学习VBA,您写的程序暂时我还有些接受困难,

能否请老师再修改下程序,用if-then 结构,验证性函数IsNumeric等,这种简单的语句比较适合初学者,谢谢您~
回复

使用道具 举报

发表于 2017-3-22 14:43 | 显示全部楼层    本楼为最佳答案   
Lydia_Li 发表于 2017-3-22 13:47
非常感谢老师的耐心解答。我才开始学习VBA,您写的程序暂时我还有些接受困难,

能否请老师再修改下程 ...

其实不难懂,我注释一下
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输入的全部是字母,没有别的办法,只有用正则,我也不懂,抄的爱疯的帖子中的语句。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-5-23 23:55 , Processed in 0.344766 second(s), 10 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表