2016-09-11 4 views
0

이 코드가 있는데이 텍스트를 입력 할 때 textbox 9와 10이 필요합니다.VBA 텍스트 상자의 숫자 값

Sub FillRanges(ws As Worksheet, L As Long) 
With ws 
    .Range("C" & L).Value = (Now) 
    .Range("D" & L).Value = Me.TextBox2 
    .Range("E" & L).Value = Me.TextBox3 
    .Range("F" & L).Value = Me.TextBox4 
    .Range("G" & L).Value = Me.TextBox5 
    .Range("K" & L).Value = Me.ComboBox1 
    .Range("L" & L).Value = Me.ComboBox2 
    .Range("M" & L).Value = Me.ComboBox3 
    .Range("N" & L).Value = Me.TextBox9 
    .Range("O" & L).Value = Me.TextBox10 
    .Range("R" & L).Value = Me.TextBox39 
    .Range("P" & L).Value = Me.TextBox40 
End With 

당신은 CDbl() 같은 변환 기능을 사용할 수 있습니다

+0

은 당신이 지금까지 시도 않았다

ValidateStringInput(tb As MSForms.TextBox, validStrings() as String) 

및 좋아하는 ... : 다음과 같은 다른 서브 우퍼를 추가? 코드 게시! 네가 그것을 달렸을 때 무슨 일이 일어난거야? 대신에 당신은 무엇을 기대 했습니까? 특별히 문제가있는 것은 무엇입니까? – Robert

답변

1

내가 그것 (쓰기)를 사용하기 전에 사용자 입력의 유효성을 검사하는 것이 바람직하다고 생각이

그래서

당신은 다음과 같은 변화 이벤트 핸들러를 몇 가지 매우 간단한 사용자 입력 검증 잠수정을 작성하고 컨트롤 내에서 호출 할 수 있습니다 : 나는 Double 타입 입력을 가정

Option Explicit 

Private Sub TextBox9_Change() 
    ValidateNumericInput Me.TextBox9, 0, 10.4 '<--| as soon as this control text changes, call 'ValidateNumericInput' to validate it 
End Sub 


Private Sub ValidateNumericInput(tb As MSForms.TextBox, minVal As Double, maxVal As Double) 
    Dim errMsg As String 

    With tb 
     If Len(.Text) > 0 Then '<-- proceed only if there's some text to validate! 
      Select Case True 
       Case Not IsNumeric(.value) '<--| if not a "numeric" input 
        errMsg = "please enter a number" 
       Case CDbl(.Text) < minVal Or CDbl(.Text) > maxVal '<--| if "numeric" input exceeds passed range 
        errMsg = "please enter a number within " & minVal & " and " & maxVal 
      End Select 
      If errMsg <> "" Then '<--| if error message has been written 
       MsgBox "invalid input in " & tb.name & vbCrLf & vbCrLf & errMsg, vbCritical + vbExclamation + vbOKOnly, "Invalid input" '<--| infrm the user 
       .Text = "" '<--| delete textbox input 
      End If 
     End If 
    End With 
End Sub 

필요한 것,하지만 당신은 쉽게 그래서 다른 types

에 적용 할 수 있습니다, 당신은 할 수있다

+0

그게 정확히 내가 필요한 것! 감사 – Alex

3

최종 하위. 다음과 같은 형식이 될 수 있습니다 :

Sub FillRanges(ws As Worksheet, L As Long) 
With ws 
    .Range("C" & L).Value = (Now) 
    .Range("D" & L).Value = Me.TextBox2 
    .Range("E" & L).Value = Me.TextBox3 
    .Range("F" & L).Value = Me.TextBox4 
    .Range("G" & L).Value = Me.TextBox5 
    .Range("K" & L).Value = Me.ComboBox1 
    .Range("L" & L).Value = Me.ComboBox2 
    .Range("M" & L).Value = Me.ComboBox3 
    .Range("N" & L).Value = CDbl(Me.TextBox9) 
    .Range("O" & L).Value = CDbl(Me.TextBox10) 
    .Range("R" & L).Value = Me.TextBox39 
    .Range("P" & L).Value = Me.TextBox40 
End With 

다른 변환 기능도 있습니다. CInt() (정수), CLng() (길다) 및 CDec() (소수).

관련 문제