Results 1 to 6 of 6

Thread: Coding formating for rounding decimal place

  1. #1
    Junior Member
    Join Date
    May 12
    Location
    Houston
    Posts
    30

    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.

  2. #2
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,524

    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

  3. #3
    Junior Member
    Join Date
    May 12
    Location
    Houston
    Posts
    30

    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.

  4. #4
    PowerPoster
    Join Date
    Dec 04
    Posts
    18,524

    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

  5. #5
    Junior Member
    Join Date
    May 12
    Location
    Houston
    Posts
    30

    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

  6. #6
    Fanatic Member
    Join Date
    Oct 08
    Location
    Midwest Region, United States
    Posts
    988

    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
  •