2013-06-06 4 views
2

그래서 나는 그것을 (5) 정수 값 가진 셀 (A2) 있습니다. 나는 셀을보고 그 셀의 값이 50000보다 큰 경우 switch 문을 사용하고 싶습니다. 값이 50000보다 작 으면 무언가를하십시오. Excel VBA 스위치 문을 셀 값을 기반으로

Private Sub Worksheet_Change(ByVal Target As Range) 

    Application.ScreenUpdating = False 

    Dim state As Integer 
    Dim threshold As Integer 

    threshold = 50000 
    state = Sheet1.Range("A2").Value 

    Select Case state 
     Case state < threshold 
      'Do something 
      Debug.Print (state) 
     Case Is >= threshold 
      'Do something 
      Debug.Print (state) 
     End Select 

End Sub 

가 어떻게이 문제를 해결할 수 있습니다 여기에

그것은 "오버플로"코드 threshold = 50000의 라인 말한다 작동하지 않는 내 코드?

답변

4

문제는 여기에 있습니다 :
50000 너무 큰 F입니다 :
Sheet1.Range.Value("$A$2")

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    Dim state As Integer 
    state = CInt(Sheet1.Range("$A$2")) 
    If Target.Value < state Then 
     Debug.Print "less than " & state 
    ElseIf Target.Value > state Then 
     Debug.Print "greater than " & state 
    Else 
     Debug.Print "Equals" 
    End If 
End Sub 

POST_EDIT이 if 문을 사용 Select Case

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    Dim state As Integer 
    state = CInt(Sheet1.Range("$A$2")) 
    Select Case state 
     Case Is > Target.Value 
      Debug.Print "less than " & state 
     Case Is < Target.Value 
      Debug.Print "greater than " & state 
     Case Else 
      Debug.Print "Equals" 
    End Select 
End Sub 

를 사용하여 또는 Integer 데이터 형식, 그래서 오버플로 내가 할 수있는

Private Sub Worksheet_SelectionChange(ByVal Target As Range) 
    Dim threshold As Long 
    threshold = 50000 
    Select Case Target.Value 
     Case Is >= threshold 
      Debug.Print "greater or equal " & threshold 
     Case Is < Target.Value 
      Debug.Print "smaller than " & threshold 
     Case Else 
      Debug.Print "Can't calculate! Error" 
    End Select 
End Sub 
+0

가 –

+1

당신이 Long'하지'Integer' –

+0

감사의 친구를 입력'로 변수를 치수를 원하는 @mehow 업데이트 된 질문을 확인, 그것은 100 % –

2

귀하의 범위 문을 사용하면 경우 상태 = 5 무엇을 언급하지 않았다

Private Sub Worksheet_Change(ByVal Target As Range) 

    Application.ScreenUpdating = False 

    Dim state As Integer 

    state = Sheet1.Range("A2").Value 

    Select Case state 
     Case Is < 5 
      'Do something 
      Debug.Print (state) 
     Case Is > 5 
      'Do something 

     End Select 

End Sub 

잘못?

+0

을 피하기 위해 Long 데이터 형식으로 치수 :'케이스> = 5'인가? –

+0

예 또는 당신은 그렇지 않은 경우 (또는 ... 그렇지 않은 경우) – JosieP

관련 문제