Coding formating for rounding decimal place
I am working on a coding for rounding decimal place (for two decimals place), the code that I did works well, but it on a specify range of cells, I want do the coding works for a range of cells in a column that could change, now I have in the column where I want to do rounding of 2 decimal place from cell I2 thru cell I2986, but if I have more or less the coding does not work:
I did the following:
Sub Rounding_TwoDecimals ()
Dim cell As Range
Range("I2").Select
For Each cell In Range("I2:I2986")
cell.value = worksheetFunction.Round(cell.value, 2)
Next Cell
End Sub
I have tried with Dim lastrow As Long, also Dim rowEnd as Long, and I cannot to find the right coding, Can anyone tell me what I need to add to this code?
Thanks in advance.
Re: Coding formating for rounding decimal place
if you want the entire column from 2 to bottom, try
Code:
range("i2:i" & rows.count)
not this method truncates the values in the cells, if you only want to format the display, set then numberformat instead
Code:
range("i2:i" & rows.count).numberformat = "#.00"
Re: Coding formating for rounding decimal place
westconn1, Can you add your code to my code or write the entire code, because I tried with you code but does not work, I do not know if I am missing anything else.
Thanks in advance.
Re: Coding formating for rounding decimal place
Quote:
Can you add your code to my code or write the entire code
why not post the code as you have it, to see if we can see error
Re: Coding formating for rounding decimal place
Here go
Sub Rounding_TwoDecimals ()
Dim cell As Range
Range("I2").Select
For Each cell In Range("I2:I" & rows.count")
cell.value = worksheetFunction.Round(cell.value, 2)
Next Cell
End Sub
Re: Coding formating for rounding decimal place
Code:
Sub format_col()
Dim rngFormat As Range
Dim lastRow As Long
Dim cell As Range
lastRow = Range("i2").End(xlDown).Row
Set rngFormat = Range("i2", "i" & lastRow)
For Each cell In rngFormat
cell.Value = Application.WorksheetFunction.Round(cell.Value, 2)
Next cell
End Sub
or sub in numberformat per westconn's suggestion...