How to avoid negative number as result of macro?

boznian

Board Regular
Joined
Mar 25, 2003
Messages
167
I use the following code to find and then reduce the value of a particular cell by one. The problem is that occasionally the value is already "0", so it goes to "-1" which messes up other calculations. Can anyone share with me a way to test for this before running the statement or a way to rewrite it so that it still reduces the value by one when appropriate, but can't go negative?

Code:
Dim Found As Range
Set Found = Sheets("By Model").Columns("B").Find(What:=Model, LookIn:=xlValues, LookAt:=xlWhole)
If Not Found Is Nothing Then
    
    Range("H" & Found.Row).Activate
    crowt = ActiveCell.Row

 Cells(crowt, "H:H").Value = Cells(crowt, "H:H").Value - 1
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Try

Code:
Cells(crowt, "H:H").Value = WorksheetFunction.Max(0, Cells(crowt, "H:H").Value - 1)
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,292
Members
452,902
Latest member
Knuddeluff

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