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>
 
shimaa,

The easy method would be to insert a dummy column to the right of the column you wish to change, use Special-K99's formula in the new column. Then copy and paste values into the original cells and format as required. Alternatively try running this:

Code:
Sub stuff()
    Dim rng As Range
    Dim cell As Range
    
    Set rng = Selection
    For Each cell In rng
        cell = Int(cell)
        cell.NumberFormat = "0.00"
    Next
       
End Sub

Simply highlight the cells you want to convert then run the macro.

Regards
 
Upvote 0

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Try this in a copy of your workbook.
Rich (BB code):
Sub Convert_Data()
  Dim adr As String
  
  adr = "A2:A" & Range("A" & Rows.Count).End(xlUp).Row '<- Change column & start row to suit
  With Range(adr)
    .Value = Evaluate("IF(LEN(" & adr & "),TRUNC(" & adr & "),"""")")
    .NumberFormat = "0.00"
  End With
End Sub
 
Upvote 0
You might also like to try a manual approach?

1. Select the column by clicking its heading label.

2. Ctrl+H to bring up the Replace dialog.

3. Find what: .* -> Replace with: Leave blank -> Options -> Ensure 'Match entire cell contents' is not checked -> Replace All -> OK -> Close

4. Format the column to show 2 decimal places.
 
Upvote 0
Hi Peter,

Always interested to learn of new techniques, but not sure I understand the benefit of the code above. Additionally in other posts I've seen the rng.specialcells(xlcelltypeconstants) method use, but couldn't get it to work with the evaluate method you've used - any suggestions or ideas would be gratefully received:

Code:
Sub Convert_Data()
  Dim adr As String
  Dim rng As Range
  
  Set rng = Range(A4", Range("A4").End(xlDown))
  
  With rng.SpecialCells(xlCellTypeConstants)
    .Value = Evaluate("Int(.Value)")
    .NumberFormat = "0.000"
End With

Regards

Peter
 
Upvote 0
Hi Peter,

Always interested to learn of new techniques, but not sure I understand the benefit of the code above. Additionally in other posts I've seen the rng.specialcells(xlcelltypeconstants) method use, but couldn't get it to work with the evaluate method you've used - any suggestions or ideas would be gratefully received:

Code:
Sub Convert_Data()
  Dim adr As String
  Dim rng As Range
  
  Set rng = Range(A4", Range("A4").End(xlDown))
  
  With rng.SpecialCells(xlCellTypeConstants)
    .Value = Evaluate("Int(.Value)")
    .NumberFormat = "0.000"
End With

Regards

Peter
Peter

To be honest, I hadn't seen your post #11 when I was composing & posting mine. However, I'd say a benefit is that my code processes the whole range at once, rather than looping through a cell at a time.

In relation to your attempted 'SpecialCells/Evaluate' code, all I'd say is that, for me at least, 'Evaluate' is a tricky beast to get to know. I'd suggest that you search the forum for some more threads that have used 'Evaluate' in suggested code. In recent times I've seen Rick Rothstein use it quite a bit to good effect so you might try to find some of his threads incorporating this.
 
Upvote 0
You always wonderful Mister "Peter"
This is an honor for me
Thank you very much Sir
(Especially if you are referring to me 'Peter' rather than the other Peter 'Peter' :)) Glad to help. Did you like the code or the manual method?
 
Upvote 0
Hi Peter,

It was in one of Rick's posts that I saw the specialcells method hence my attempted approach. I shall dig some more as i think the both your defining adr and the specialcells methods simply identify the relevant range, hence the evaluate ought to work in my solution also - though I note you've defined adr as a string. I'm off to investigate Evaluate further!! If I think I've found anything useful I'll post again

Regards
 
Upvote 0

Forum statistics

Threads
1,215,136
Messages
6,123,243
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