We help IT Professionals succeed at work.

VBA Excel: code for checking if a number in a cell is >0 but <50

lossiemouth asked
Hi Experts,

what code should I use to check if a number in an excel sheet cell,  is > 0 but < 50?

Many thanks

Watch Question

Can you please explain the question in deep? Actually what do you want to do if a value of a particular cell is >0 and <50?
Select the cell(s) you want.
Go to the data tab, and click data validation.
Under settings wher it says allow, select whole number.
Under data, select between.
In minimum type 0.
In maximum type 50.

Apologies, I meant:
In minimum type 1.
In maximum type 49.



Thanks for your replies.

I am using VBA to step through a range of numbers.
If a number lies between 0 and 50, I will then take action on that row.
If Worksheets(Sheet1).Cells(myRowinput, myColumn) > o and < 50  Then................

The code above does not work, but this is the thing I am trying to achieve.
Sub CheckValue()
    Dim ShObj As Worksheet
    Dim lngMyRowInput As Long
    Dim lngMyColInput As Long
    lngMyRowInput = 1
    lngMyColInput = 1
    Set ShObj = ThisWorkbook.Worksheets(1) 'OR Set ShObj = ThisWorkbook.Worksheets("Sheet1")
    If ShObj.Cells(lngMyRowInput, lngMyColInput).Value > 0 And ShObj.Cells(lngMyRowInput, lngMyColInput).Value < 50 Then
        'Do your work here
    End If
End Sub

Explore More ContentExplore courses, solutions, and other research materials related to this topic.