Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Type Mismatch error when number is negative

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 324
Default Type Mismatch error when number is negative

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Type Mismatch error when number is negative

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,986
Default Type Mismatch error when number is negative

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Type Mismatch error when number is negative

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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,391
Default Type Mismatch error when number is negative

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Visual Basic Error Run Time Error, Type Mismatch Meg Partridge Excel Discussion (Misc queries) 12 September 10th 08 06:10 PM
Conditional Formatting - Run Time Error '13' Type Mismatch Error ksp Excel Programming 0 July 11th 06 07:06 AM
Help: Compile error: type mismatch: array or user defined type expected lvcha.gouqizi Excel Programming 1 October 31st 05 08:20 PM
type mismatch error Monique Excel Programming 3 July 21st 05 09:19 AM
Befuddled with For Next Loop ------ Run - Time Error '13' Type Mismatch Error rdavis7408 Excel Programming 1 August 25th 04 03:54 AM


All times are GMT +1. The time now is 10:26 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"