VBA to replace negative values with zero's

Rollnation

New Member
Joined
Jan 17, 2017
Messages
17
Hello all, I am trying to figure out the VBA to replace all negative values in a specific range, on a specific worksheet "MonteCarlo", Range "S3:AMD163". I have tried unsuccessfully couple of times with the following code:

Amy help from a more seasoned VBA writer would be appreciated!

Sub Replace_Negatives()

'Dim ws As Worksheet
'Dim rng As Range




'Application.ScreenUpdating = False


'Set ws = ThisWorkbook.Sheets("MonteCarlo")
'Set rng = ws.Range("S3:AMD163")


'For Each rng In rng.Cells
'If cell.Value < 0 Then cell.Value = 0

'Application.ScreenUpdating = True
'End Sub

Sub Second_Try()


'Dim RowNum As Long, ColNum As Long
'Application.ScreenUpdating = False
'For ColNum = 19 To Cells(1, Columns.Count).End(xlToLeft).Column
'For RowNum = 3 To Cells(Rows.Count, ColNum).End(xlUp).Row
' If Val(Cells(RowNum, ColNum)) And Cells(RowNum, ColNum) < 0 Then Cells(RowNum, ColNum) = 0
'Next RowNum
'Next ColNum
'Application.ScreenUpdating = True
'End Sub
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
I am half way there with this code but i get a runtime error 13 once it see's some cells with #Value . Also I wat this code to run only on sheet "montecarlo"


Sub Replace Zero()
Dim rng As Range
For Each rng In Range("S3:AMD163") ' substitute your range here
If rng.Value < 0 Then
rng.Value = 0
End If
Next
End Sub
 
Upvote 0
Very similar, in fact I am not sure your first one wont work if you change ThisWorkbook to Activeworkbook

Code:
Sub Replace_Negatives()


Dim ws As Worksheet
Dim rng As Range
Dim cell As Range




Application.ScreenUpdating = False




Set ws = ActiveWorkbook.Sheets("MonteCarlo")
Set rng = ws.Range("S3:AMD163")




For Each cell In rng
If cell.Value < 0 Then cell.Value = 0
Next cell
Application.ScreenUpdating = True


End Sub
 
Upvote 0
I am half way there with this code but i get a runtime error 13 once it see's some cells with [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=VALUE]#VALUE [/URL] . Also I wat this code to run only on sheet "montecarlo"
Code:
Sub Replace Zero()
Dim rng As Range
    For Each rng In Range("S3:AMD163") ' substitute your range here
        If rng.Value < 0 Then
           rng.Value = 0
        End If
    Next
End Sub
Does the fact that you have #VALUE ! errors mean your cells are filled with formulas? If so, you want to replace the formulas displaying negative numbers with hard-coded constants (zero values)? If so, should the formulas remain formulas or did you want them converted to constants as well?
 
Upvote 0
Does the fact that you have #VALUE ! errors mean your cells are filled with formulas? If so, you want to replace the formulas displaying negative numbers with hard-coded constants (zero values)? If so, should the formulas remain formulas or did you want them converted to constants as well?


The array is not formulas but part of another montecarlo VBA that i have written. Once that VBA is finished all of the values in the array will be constants.

So yes I would like to hard code all negatives as constants
 
Upvote 0
That change worked! However, toward then end of the array I have some #Value cells. Is there a way to modify the code above to only run on cells that are numeric?
 
Upvote 0
The array is not formulas but part of another montecarlo VBA that i have written. Once that VBA is finished all of the values in the array will be constants.

So yes I would like to hard code all negatives as constants
See if this macro works for you...
Code:
[table="width: 500"]
[tr]
	[td]Sub MakeNegativeValuesZero()
  [montecarlo!S3:AMD163] = [IF(montecarlo!S3:AMD163<0,0,IF(montecarlo!S3:AMD163="","",montecarlo!S3:AMD163))]
End Sub[/td]
[/tr]
[/table]
 
Upvote 0
See if this macro works for you...
Code:
[TABLE="width: 500"]
<tbody>[TR]
[TD]Sub MakeNegativeValuesZero()
  [montecarlo!S3:AMD163] = [IF(montecarlo!S3:AMD163<0,0,IF(montecarlo!S3:AMD163="","",montecarlo!S3:AMD163))]
End Sub[/TD]
[/TR]
</tbody>[/TABLE]


This worked too, thank you. Any benefit of this code over the code above?
This seemed to run pretty quickly.
 
Upvote 0
This worked too, thank you. Any benefit of this code over the code above?
This seemed to run pretty quickly.
I am guessing my code would be faster given that the other code has to iterate 161,000 cells, examining each one individually, whereas my code lets Excel perform the looping behind the scenes using its optimized cell processing routines to do this.
 
Upvote 0

Forum statistics

Threads
1,224,822
Messages
6,181,165
Members
453,021
Latest member
Justyna P

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