|
-
Sep 23rd, 2012, 01:37 PM
#1
Thread Starter
Junior Member
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.
-
Sep 23rd, 2012, 04:09 PM
#2
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"
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 23rd, 2012, 04:55 PM
#3
Thread Starter
Junior Member
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.
-
Sep 24th, 2012, 04:39 AM
#4
Re: Coding formating for rounding decimal place
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
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
Sep 24th, 2012, 07:17 AM
#5
Thread Starter
Junior Member
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
-
Sep 24th, 2012, 08:58 AM
#6
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...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|