2013-02-11 15 views
8

मेरे कोडVBA एक्सेल, के रूप में पूर्णांक

Dim a As Integer 
a = InputBox("Enter the number", "Program", "", 7000, 6000) 
If a = Empty Then 
    ' do code... 
Else 
    MsgBox "Enter the number." 
End If 

InputBox के लिए बेमेल अगर मैं एक खाली मैदान छोड़ कर, एक्सेल एक Type Mismatch त्रुटि देता है। मैं एक संदेश प्रदर्शित करना चाहता हूं।

उत्तर

11

a के बाद से एक Integer है, यह एक String नहीं हो सकते या Empty हो। एक Variant का प्रयोग करें और फिर देखने के लिए लौटा दिया गया है की जाँच करें:

Dim a As Variant 
Dim b As Integer 

a = InputBox("Enter the number", "Program", "", 7000, 6000) 

If Not IsNumeric(a) Then 
    'a is not a number 
Else 
    'a is a number and can be converted to Integer 
    b = CInt(a) 
End If 
4

आपके पास aInteger के रूप में परिभाषित किया गया है। Integer खाली नहीं हो सकता है। Integer के बजाय Variant का उपयोग करें:

Dim a As Variant 
a = InputBox("Enter the number", "Program", "", 7000, 6000) 
If a = Empty Then 
    ' do code... 
Else 
    MsgBox "Enter the number." 
End If 
+1

पहले सही जवाब के लिए +1 - यद्यपि आप 'चाहते हैं IsEmpty (क) Then' – brettdj

+0

मेरी +1 ब्रेट को तो) –

+0

@brettdj क्यों डॉन क्या आप अपना जवाब पोस्ट नहीं करते? :) –

संबंधित मुद्दे