2016-04-01 32 views
0

我正在實現一個用戶窗體,並希望在運行用戶窗體之前在輸入數據中包含一些檢查。特別是,檢查Userform文本框中的所有輸入都是數字,儘管它是有效的,但文本框爲空或爲空。我試圖執行以下操作:Userform文本框是數字(和空值)

Select Case KeyAscii 
    Case 0, 46, 48 To 57 
    Case Else 
    MsgBox "Only numbers allowed" 
    End Select 

但這不起作用。 請問,想法? 非常感謝你!!!!!!!!!

回答

0

也許有點冗長 - 我通常使用類模塊和控件上的標籤屬性來決定可以在文本框中輸入的內容。

用四個文本框創建一個表單。
給文本框,這些標籤:

  • 1; CDBL
  • 2; CINT
  • 3; CSTR
  • 4; CSENTENCE

的數字是粘貼的列值保存到表單時(我沒有在這裏描述過)。
該文本描述了可以在文本框中輸入的內容 - CDBL是2位小數的數字,CINT是數字,0位小數,CSTR用於正確文本,CSENTENCE用於文本文本。

創建一個名爲clsControlText的課程模塊。
該代碼添加到類模塊:

Public WithEvents txtBox As MSForms.TextBox 

Private Sub txtBox_Change() 
    Static LastText As String 
    Static SecondTime As Boolean 
    Const MaxDecimal As Integer = 2 
    Const MaxWhole As Integer = 1 

    With txtBox 
    If InStr(.Tag, ";") > 0 Then 
     Select Case Split(.Tag, ";")(1) 
      Case "CDBL", "CCur" 
       'Allow only numbers with <=2 decimal places 
       If Not SecondTime Then 
        If .Text Like "[!0-9.-]*" Or Val(.Text) < -1 Or _ 
         .Text Like "*.*.*" Or .Text Like "*." & String$(1 + MaxDecimal, "#") Or _ 
         .Text Like "?*[!0-9.]*" Then 
         Beep 
         SecondTime = True 
         .Text = LastText 
        Else 
         LastText = .Text 
        End If 
       End If 
       SecondTime = False 
      Case "CINT" 
       'Allow only whole numbers. 
       If .Text Like "[!0-9]" Or Val(.Text) < -1 Or .Text Like "?*[!0-9]*" Then 
        Beep 
        .Text = LastText 
       Else 
        LastText = .Text 
       End If 
      Case "CSTR" 
       'Convert text to proper case. 
       .Text = StrConv(.Text, vbProperCase) 
      Case "CSENTENCE" 
       'Convert text to sentence case (capital after full-stop). 
       .Text = ProperCaps(.Text) 
      Case Else 
       'Allow anything. 
     End Select 
    End If 
    End With 
End Sub 

Private Function ProperCaps(strIn As String) As String 
    Dim objRegex As Object 
    Dim objRegMC As Object 
    Dim objRegM As Object 
    Set objRegex = CreateObject("vbscript.regexp") 
    strIn = LCase$(strIn) 
    With objRegex 
     .Global = True 
     .ignoreCase = True 
     .Pattern = "(^|[\.\?\!\r\t]\s?)([a-z])" 
     If .Test(strIn) Then 
      Set objRegMC = .Execute(strIn) 
      For Each objRegM In objRegMC 
       Mid$(strIn, objRegM.firstindex + 1, objRegM.Length) = UCase$(objRegM) 
      Next 
     End If 
     ProperCaps = strIn 
    End With 
End Function 

將此代碼添加到用戶窗體:

Private colTextBoxes As Collection 

Private Sub UserForm_Initialize() 

    Dim ctrlSelect As clsControlText 
    Dim ctrl As Control 


    Me.Caption = ThisWorkbook.Name 

    Set colTextBoxes = New Collection 
    For Each ctrl In Me.Controls 
     Select Case TypeName(ctrl) 
      Case "TextBox" 
       Set ctrlSelect = New clsControlText 
       Set ctrlSelect.txtBox = ctrl 
       colTextBoxes.Add ctrlSelect 
     End Select 
    Next ctrl 

End Sub 

注:並非所有的代碼都是我的。我發現了ProperCaps和本網站其他地方的CDBL代碼 - 或者也許是MrExcel。

+0

這是一個非常有趣的去了解它的方式!非常感謝! –

+0

我不知道是否有辦法用KeyAscii做到這一點?一定有辦法?!? –

0

你可以使用一個基本LIKERegexp

Sub Test() 
Debug.Print StrCheck("") 
Debug.Print StrCheck("hello kitty") 
Debug.Print StrCheck("4156") 
End Sub 

功能

Function StrCheck(strIn As String) As String 
Dim objRegex As Object 
Set objRegex = CreateObject("vbscript.regexp") 
objRegex.Pattern = "\d+" 
'vaidate empty string 
If Len(Trim(strIn)) = 0 Then 
    StrCheck = True 
Else 
'validate whether non-empty string is numeric 
    StrCheck = objRegex.Test(strIn) 
End If 
End Function 
相關問題