Converting decimal values ​​to zeros

shimaa01234

Active Member
Joined
Jun 24, 2014
Messages
446
I want to convert these numbers as in the example:
FromTo
10.1610.00
15.0915.00
19.9919.00

<colgroup><col style="width:48pt" span="2" width="64"> </colgroup><tbody>
</tbody>
 
Hi Peter

I think I've worked it out!. Either method of identifying the range works, though the specialcells method identifies al cells within the starting range that meet the condition, thus you may end up with a set of discontinuous ranges. This can be handled by converting the returned range into a string, splitting it into an array and using each array element as the argument for your 'with range(adr)' statement. I failed to do anything clever with the Evaluate statement, and clearly I need to do more work in that area. The code below searches columns A:C reports which cells have constants and formulas and then converts the constants to integers.

Code:
S
Sub more_stuff()
    Dim cRng As Range
    Dim fRng As Range
    Dim rng As Range
    Dim adr As Variant    Dim n As Integer
    
    On Error Resume Next
    
    Set fRng = Range("A:C").SpecialCells(xlCellTypeFormulas)
    Set cRng = Range("A:C").SpecialCells(xlCellTypeConstants, xlNumbers)
    On Error GoTo 0
    
    If cRng Is Nothing And fRng Is Nothing Then
        MsgBox "Your data contains neither constants nor formulas"
        Exit Sub
    End If
        
    If Not cRng Is Nothing Then
        MsgBox "Constants are found at: " & cRng.Address
    End If
    If Not fRng Is Nothing Then
        MsgBox "functions are found at: " & fRng.Address
    End If
    
    adr = cRng.Address
    If Len(adr) - Len(Replace(adr, ",", "")) > 0 Then
        adr = Split(adr, ",")
    End If
    
    For n = 0 To UBound(adr)
        With Range(adr(n))
            .Value = Evaluate("IF(LEN(" & adr(n) & "),TRUNC(" & adr(n) & "),"""")")
            .NumberFormat = "0.000"
        End With
    Next
End Sub

Think I might have got a bit carried away with this one, but an interesting exercise nevertheless.

Regards
 
Upvote 0

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.

Forum statistics

Threads
1,215,143
Messages
6,123,274
Members
449,093
Latest member
Vincent Khandagale

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