rounding a whole column

Krokolv

New Member
Joined
Jan 3, 2021
Messages
12
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I tried to round a all column into 2 decimals, but I keep getting the same mistake. So this is the code on the internet:

Sub rounding()
For Each cell In [X:X]
If cell = "" Then Exit Sub
cell.Value = WorksheetFunction.Round(cell.Value, 2)
Next cell
End Sub

But I keep getting the same error

Type mismatch (Error 13)​

1640619469352.png

What I want to do is actually select the from X2 to "where is values" down and then rounding to 2 decimals, copying and paste on the W column. But if you would guide at least why there is this error, eventually I would figure the rest of the steps out.

Thanks for helping!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Since the Round function takes a numeric data type there is likely something in column X that is non-numeric giving you a type mismatch error.

If you only care about how the number is displayed you can use

VBA Code:
Sub rounding()
    Range("X2:X" & [X1048576].End(xlUp).Row).NumberFormat = "0.00"
End Sub

If you do actually want ot overwrite the original value or fomula with a two decimal version of itself use:

VBA Code:
Sub Rounding1()
    Dim X As Range, i As Long, rng, r
    Set X = Range("X2:X" & [X1048576].End(xlUp).Row)
    rng = X
    For Each r In rng
        i = i + 1
        If IsNumeric(r) Then rng(i, 1) = Round(r, 2)
    Next r
    With X
        .Value = rng
        .NumberFormat = "0.00"
    End With
End Sub
 
Upvote 0
How about
VBA Code:
Sub Krokolv()
   With Range("X2", Range("X" & Rows.Count).End(xlUp))
      .Offset(, -1).Value = Evaluate(Replace("if(@="""","""",if(isnumber(@),round(@,2),@))", "@", .Address))
   End With
End Sub
This will put the rounded values into col W
 
Upvote 0
Hi,

both
How about
VBA Code:
Sub Krokolv()
   With Range("X2", Range("X" & Rows.Count).End(xlUp))
      .Offset(, -1).Value = Evaluate(Replace("if(@="""","""",if(isnumber(@),round(@,2),@))", "@", .Address))
   End With
End Sub
This will put the rounded values into col W
Worked perfectly, is it too much to ask some comments? I am finding trouble to understand what is @.
 
Upvote 0
Since the Round function takes a numeric data type there is likely something in column X that is non-numeric giving you a type mismatch error.

If you only care about how the number is displayed you can use

VBA Code:
Sub rounding()
    Range("X2:X" & [X1048576].End(xlUp).Row).NumberFormat = "0.00"
End Sub

If you do actually want ot overwrite the original value or fomula with a two decimal version of itself use:

VBA Code:
Sub Rounding1()
    Dim X As Range, i As Long, rng, r
    Set X = Range("X2:X" & [X1048576].End(xlUp).Row)
    rng = X
    For Each r In rng
        i = i + 1
        If IsNumeric(r) Then rng(i, 1) = Round(r, 2)
    Next r
    With X
        .Value = rng
        .NumberFormat = "0.00"
    End With
End Sub
Worked perfectly, thanks a lot, would you mind to add some comments to your code?
 
Upvote 0
The Replace function replaces the @ with .Address so that you get something like
VBA Code:
      .Offset(, -1).Value = Evaluate("if(X2:X100="""","""",if(isnumber(X2:X100),round(X2:X100,2),X2:X100))")
and the evaluate function then calculates that formula & puts the result in column W.
HTH
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,588
Members
449,039
Latest member
Arbind kumar

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