MRound VBA formula setting all cell values to zero

Mikeymike_W

Board Regular
Joined
Feb 25, 2016
Messages
171
Hi All,

Thanks in advance for any help you can provide.

I have a formula in VBA which rounds the values in a range to the nearest 0.5.
It works fine but it also sets all the cell values to zero if they are blank, does anyone know a way to get past this?

My code is:

For i = 5 To 20
Sheets(1).Cells(i, 7).Value = Application.MRound(Sheets(1).Cells(i, 7).Value, 0.25)
Next

I have this located in my active sheet under worksheet_change

Thanks again,

Mike
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
For i = 5 To 20
with Sheets(1).Cells(i, 7)
if .value <> "" then
.value = Application.MRound(.Value, 0.25)
end if
end with
Next
 
Last edited:
Upvote 0
Hi,

This worked a treat thanks.

Quick question, is it also possible to incorporate an iferror function that would leave the cell blank and open a msgbox?

Thanks,

Mike
 
Upvote 0
For i = 5 To 20
with Sheets(1).Cells(i, 7)
if .value <> "" then
.value = Application.MRound(.Value, 0.25)
else
msgbox "message"
end if
end with
Next
 
Upvote 0
Hi,

Thanks again for your help.

Unfortunately it didn't really work properly, the error message shows whenever a value is entered and you need to click ok about five times to get rid of the msgbox.

Any ideas?
 
Upvote 0
If you just want the box to appear once:

Code:
Sub test()
Dim i%, x%
x = 0
For i = 5 To 20
With Sheets(1).Cells(i, 7)
If .Value <> "" Then
.Value = Application.MRound(.Value, 0.25)
Else
x = x + 1
End If
End With
Next
If x > 0 Then
MsgBox "message"
End If
End Sub

is one way, or add a countif formula for fewer steps.
 
Upvote 0
The code for the error seems to be working its way down the range of cells, so if the error is in Q12 the box will appear another nine times
 
Upvote 0
Have you tried post #6 (it was just a minute before you wrote again so probably not)? If it's not working still show me a sample, or how to fix mine:


Excel 2010
G
50.41033
60.34616
70.55644
80.571979
90.642927
100.840392
110.343535
120.337543
130.537315
140.920642
150.147249
160.344085
170.867084
180.548094
190.251324
200.48565
Sheet1
 
Upvote 0
Hi,

Well the message box is only appearing once now so that's cool but it appears whenever any value is entered in the cell.

I don't want the user to enter a range of values (17 - 25.5) rather just the one number (19.5). if the enter anything other than the one number I was wanting the message box to appear and tell them to enter the data as I want.

I had this before with data validation but if it can be done as part of this code I think it would be tidier.

Thanks for your support,

Mike
 
Upvote 0
Code:
Sub mySub()
Dim ErrorFlag as Boolean

ErrorFlag = False

For i = 5 To 20
     With Sheets(1).Cells(i, 7)
        If IsError(.Value) Then
            ErrorFlag = True
        ElseIf IsNumeric(.Value) Then
            .Value = Application.MRound(.Value, 0.25)
        End If
    End With
Next i

If ErrorFlag Then MsgBox "There was an error somewhere"

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,952
Messages
6,122,458
Members
449,085
Latest member
ExcelError

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