Home |
Search |
Today's Posts |
|
#1
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Hello,
Need a little VBA help. dim var1 as integer var1 = countA - countB (for example) If var1 0 then This if statement works fine when var1 is a possitive number. When var1 is a negative number I get a "Type Mismatch" error. Should I not be able to compare a negative value with another number? I've tried declaring 0 as integer variable to ensure both variables being compared in the If statement are of the same type, but that didn't help. Any suggestions? -- D.Farns |
#2
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Did you try "If var1 <0 then"?
-- Best wishes, Jim "D.Farns" wrote: Hello, Need a little VBA help. dim var1 as integer var1 = countA - countB (for example) If var1 0 then This if statement works fine when var1 is a possitive number. When var1 is a negative number I get a "Type Mismatch" error. Should I not be able to compare a negative value with another number? I've tried declaring 0 as integer variable to ensure both variables being compared in the If statement are of the same type, but that didn't help. Any suggestions? -- D.Farns |
#3
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Jim,
To my surprise "var1 < 0" did NOT produce the type mismatch error. However, I want the if/then to evaluate to true only if var1 is 1 or higher. var1 =1 or var1 0 is the logic I need, but they both generate the error. I tried this if/then earlier in the code to handle when var1 is negative. if IsNumeric(var1) = false then var1 = 0 end if The hope was that when var1 is negative, the isnumeric would test false and I could set it to 0 so I wouldn't get Type Mismatch in the next if/then, but IsNumeric(var1) with var1 containing negative value evaluates to true. Kind of strange that IsNumeric evaluates to true, but can't be compared to a number? any other thoughts? -- D.Farns "Jim Jackson" wrote: Did you try "If var1 <0 then"? -- Best wishes, Jim "D.Farns" wrote: Hello, Need a little VBA help. dim var1 as integer var1 = countA - countB (for example) If var1 0 then This if statement works fine when var1 is a possitive number. When var1 is a negative number I get a "Type Mismatch" error. Should I not be able to compare a negative value with another number? I've tried declaring 0 as integer variable to ensure both variables being compared in the If statement are of the same type, but that didn't help. Any suggestions? -- D.Farns |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
It is sort of a bandaid type thing, but immediately after var1 value is
established, you could use an if statement: If var1< 0 Then var1 = 0 End If Unless var1 value is used later in a calculation that would produce an erroneous answer if converted to 0 value, it should cure your type mismatch problem. "D.Farns" wrote: Jim, To my surprise "var1 < 0" did NOT produce the type mismatch error. However, I want the if/then to evaluate to true only if var1 is 1 or higher. var1 =1 or var1 0 is the logic I need, but they both generate the error. I tried this if/then earlier in the code to handle when var1 is negative. if IsNumeric(var1) = false then var1 = 0 end if The hope was that when var1 is negative, the isnumeric would test false and I could set it to 0 so I wouldn't get Type Mismatch in the next if/then, but IsNumeric(var1) with var1 containing negative value evaluates to true. Kind of strange that IsNumeric evaluates to true, but can't be compared to a number? any other thoughts? -- D.Farns "Jim Jackson" wrote: Did you try "If var1 <0 then"? -- Best wishes, Jim "D.Farns" wrote: Hello, Need a little VBA help. dim var1 as integer var1 = countA - countB (for example) If var1 0 then This if statement works fine when var1 is a possitive number. When var1 is a negative number I get a "Type Mismatch" error. Should I not be able to compare a negative value with another number? I've tried declaring 0 as integer variable to ensure both variables being compared in the If statement are of the same type, but that didn't help. Any suggestions? -- D.Farns |
#5
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
Yes you can compare to negative number. But I suspect the type mismatch is
in the assignment of the value to var1; - When countA - countB is positive a number is returned - When countA - countB is negative, a non-integer value is returned Private Sub CommandButton1_Click() Dim var1 As Integer var1 = -2 Debug.Print var1 If var1 0 Then MsgBox "Positive" Else MsgBox "Negative" End If End Sub NickHK "D.Farns" wrote in message ... Hello, Need a little VBA help. dim var1 as integer var1 = countA - countB (for example) If var1 0 then This if statement works fine when var1 is a possitive number. When var1 is a negative number I get a "Type Mismatch" error. Should I not be able to compare a negative value with another number? I've tried declaring 0 as integer variable to ensure both variables being compared in the If statement are of the same type, but that didn't help. Any suggestions? -- D.Farns |
#6
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
For clarity:
Yes you can compare to negative number. But I suspect the type mismatch is in the assignment of the value to var1; OK - var1=<Some calculation that results in a +ve number Err - var1=<Some calculation that results in a non-integer value NickHK "NickHK" wrote in message ... Yes you can compare to negative number. But I suspect the type mismatch is in the assignment of the value to var1; - When countA - countB is positive a number is returned - When countA - countB is negative, a non-integer value is returned Private Sub CommandButton1_Click() Dim var1 As Integer var1 = -2 Debug.Print var1 If var1 0 Then MsgBox "Positive" Else MsgBox "Negative" End If End Sub NickHK "D.Farns" wrote in message ... Hello, Need a little VBA help. dim var1 as integer var1 = countA - countB (for example) If var1 0 then This if statement works fine when var1 is a possitive number. When var1 is a negative number I get a "Type Mismatch" error. Should I not be able to compare a negative value with another number? I've tried declaring 0 as integer variable to ensure both variables being compared in the If statement are of the same type, but that didn't help. Any suggestions? -- D.Farns |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
Visual Basic Error Run Time Error, Type Mismatch | Excel Discussion (Misc queries) | |||
Conditional Formatting - Run Time Error '13' Type Mismatch Error | Excel Programming | |||
Help: Compile error: type mismatch: array or user defined type expected | Excel Programming | |||
type mismatch error | Excel Programming | |||
Befuddled with For Next Loop ------ Run - Time Error '13' Type Mismatch Error | Excel Programming |