是否可以让输入框需要5个数字?

时间:2016-10-26 18:15:59

标签: vba excel-vba excel-2010 excel

我目前正在设计一个需要来自用户的3个输入的宏,我想知道我是否可以以某种方式要求数字输入需要5位序列号。并限制任何大于或等于五位的内容。到目前为止,这是我的代码:

Sub MRN_numbers()

  Dim s, e As Integer
  Dim m As String

  s = InputBox("Please enter the starting 5 digit MRN number")
  m = InputBox("please enter the material type")
     If Not m = "ebara" Or m = "mirra" Or m = "300mm" Then
     MsgBox ("Please enter valid material name!")
     m = InputBox("Please enter the material type")
     End If

  e = InputBox("pleae enter the ending 5 digit MRN number")

Range("D1").Activate
Range("D65536").End(xlUp).Offset(1, 0).Activate

For i = s To e
If m = "ebara" Or m = "mirra" Or m = "300mm" Then

    If m = "ebara" Then
        For l = 1 To 5
            ActiveCell.Value = i & "-" & l
            ActiveCell.Offset(1, 0).Activate
            ActiveCell.Offset(-1, -1).Value = "Ebara"
        Next l

    End If

    If m = "mirra" Then
        For r = 1 To 6

            ActiveCell.Value = i & "-" & r
            ActiveCell.Offset(1, 0).Activate
            ActiveCell.Offset(-1, -1).Value = "Mirra"
        Next r
    End If

    If m = "300mm" Then
        For y = 1 To 4
            ActiveCell.Value = i & "-" & y
            ActiveCell.Offset(1, 0).Activate
            ActiveCell.Offset(-1, -1).Value = "300mm"
        Next y
    End If


End If


  Range("D65536").End(xlUp).Offset(1, 0).Activate






Next i

End Sub

我不知道从哪里开始,我观看视频但没有发现任何有用的信息,我将继续寻找并尝试不同的方式,但是对此有任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:5)

在Do循环中包装每个条目:

Do
    s = InputBox("Please enter the starting 5 digit MRN number")
    If Not (IsNumeric(s) And Len(s) = 5) Then MsgBox s & " is not a 5 digit number"
Loop Until IsNumeric(s) And Len(s) = 5

所以e

Do
    e = InputBox("Please enter the ending 5 digit MRN number")
    If Not (IsNumeric(e) And Len(e) = 5) Then MsgBox e & " is not a 5 digit number"
Loop Until IsNumeric(e) And Len(e) = 5