PDA

View Full Version : validation for Negative number



rajraj9876
10-27-2013, 06:00 AM
Hi Experts,
I have to write a VBA code to validate for all active cell with negative values. In case of a negative value entered, a message box thrown " No negative values allowed" and the that particular cell should become red color. Again on refresh the red color validadation should disappear.
Thanks
Raj

Aussiebear
10-27-2013, 02:36 PM
Surely all you need to do is data validation for the range of cells and if its value is less than zero turn the value red. However, if you persist with reinventing the wheel, simply record your steps with the macro recorder and edit the code to suit.

rajraj9876
10-27-2013, 06:54 PM
thanks a lot, but still am stuck with one feature when the cell is input , the negative validation should work with the input cell becoming "red" how do I bring this feature in the validation and this for all active cell. This is the macro code below

With Selection.Validation
.Delete
.Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
Operator:=xlGreater, Formula1:="0"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = ""
.ErrorTitle = "no "
.InputMessage = ""
.ErrorMessage = "nooo"
.ShowInput = True
.ShowError = True
End With
Range("G9").Select
With Selection.Validation
.Delete
.Add Type:=xlValidateWholeNumber, AlertStyle:=xlValidAlertStop, _
Operator:=xlGreater, Formula1:="0"
.IgnoreBlank = True
.InCellDropdown = True
.InputTitle = "no"
.ErrorTitle = "no "
.InputMessage = "no values below 0"
.ErrorMessage = "nooo"
.ShowInput = True
.ShowError = True
End With
Range("G9").Select
End Sub