If condition with range

AlexCS

Board Regular
Joined
Oct 31, 2011
Messages
78
Hi all,

I am trying to put together an IF function that does something everytime a variable ranges between -5 and 5..I am however having an issue with the condition..how can I write the below correctly in VBA?

If ActiveSheet.Cells(s,4).Value >=-5 And <=5 Then...

Please help!

Thank you,

Alex
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try:

Code:
If ActiveSheet.Cells(s,4).Value >=-5 And ActiveSheet.Cells(s,4).Value <=5 Then

With multiple If conditions you need to specify the item to check each time.
 
Upvote 0
Try

Code:
If ActiveSheet.Cells(s, 4).Value >= -5 And ActiveSheet.Cells(s, 4).Value <= 5 Then
 
Upvote 0
Like:
Code:
If ActiveSheet.Cells(s,4).Value >=-5 And ActiveSheet.Cells(s,4).Value<=5 Then
 
Upvote 0
Hi Adam, Peter and Taurean,

Many thanks for advising me regarding this matter!

Would any of you know why the If condition does not identify values like
-0.0037 for example, although it is supposed to find any value between -5 and 5?

Kind regards,

Alex
<TABLE style="WIDTH: 69pt; BORDER-COLLAPSE: collapse" border=0 cellSpacing=0 cellPadding=0 width=92><COLGROUP><COL style="WIDTH: 69pt; mso-width-source: userset; mso-width-alt: 3364" width=92><TBODY><TR style="HEIGHT: 15pt" height=20><TD style="BORDER-BOTTOM: #d4d0c8; BORDER-LEFT: #d4d0c8; BACKGROUND-COLOR: transparent; WIDTH: 69pt; HEIGHT: 15pt; BORDER-TOP: #d4d0c8; BORDER-RIGHT: #d4d0c8" class=xl71 height=20 width=92 align=right></TD></TR></TBODY></TABLE>
 
Upvote 0
I placed -0.0037 in Cell A1 and then tested the code as below:
Code:
Public Sub Test()
If ActiveSheet.Cells(1, 1).Value >= -5 And ActiveSheet.Cells(1, 1).Value <= 5 Then
MsgBox "Fits in range!"
End If
End Sub
And it worked! I tried changing the cell formatting but it doesn't seem to have any bearing on this.

I could not reproduce your situation. Sorry!
 
Upvote 0

Forum statistics

Threads
1,215,459
Messages
6,124,947
Members
449,198
Latest member
MhammadishaqKhan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top