Results 1 to 11 of 11

Thread: Remove Decimals

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Posts
    13

    Remove Decimals

    Greetings,

    I have a macro which copies a column from worksheet A and pastes it in Worksheet B.

    How can I create code so that when it pastes the values in Worksheet B it removes all the decimals.

    I have changed the format-cells option to 0 decimal places. However, the value still reads as having decimals.

    So for example, if I copy the number 95.67. It will show up as 95, but still use 95.67 for the calculations.

    I want to get rid of the 0.67.....

    Please help

    Thanks
    Raj

  2. #2
    jim mcnamara
    Guest
    Use the Int() function in VBA
    Code:
    Dim j as Single, i as Integer
    j=95.67
    i = Int(j)
    '              i now equals 95

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    If you convert your number to a Long, it will automatically drop the decimals. Example:
    VB Code:
    1. Dim MyNum As Integer
    2. MyNum = 95.67
    3. MyNum = CInt(MyNum)
    4. 'MyNum now a whole number

  4. #4
    Hyperactive Member
    Join Date
    Nov 2000
    Location
    Mexico City
    Posts
    306
    You have two options:

    First one:
    Change the Excel´s calculation settings, but... you will lost precision in every calculus of the sheet.

    Second one (recomended):
    Change your macro for copying, to make a cycle from the first row to the last row, so you can manipulate the values and sending the integer value.

    If you need some... make a reply.
    If things were easy, users might be programmers.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333
    Drats...Jim beat me to it.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Posts
    13
    Can you show me how to make it cycle through?....and change all the values in a range to read as zero decimals?

    Thanks

    Raj

  7. #7
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Range("A1:A10").Select
    Selection.NumberFormat = "0"

    Done !
    Last edited by alex_read; Nov 28th, 2001 at 10:26 AM.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2001
    Posts
    13
    Still looking for help....need to change all the numbers in a column so that the decimals are permanently removed not just hidden.

    Please Help
    Raj

  9. #9
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Alright then :
    Code:
    Columns("A:A").Select
    Selection.NumberFormat = "0"
    if it's a column, this one will convert all the entries in column "A" as though you'd right clicked each, chosen the format cells option, selected the number category from the first tab & set the decimal points to 0. Can't see what's wrong with this & they are removed not hidden.

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  10. #10
    Evil Genius alex_read's Avatar
    Join Date
    May 2000
    Location
    Espoo, Finland
    Posts
    5,538
    Alternatively, if you want a long winded looping way, these too also work. Declare :
    Code:
    Dim intcounter As Integer, lastcol As Integer
    lastcol = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Column
    Then use either :
    Code:
    For intcounter = 1 To lastcol
        Cells(1, intcounter).Select
        Round cell.Value, 0
    Next intcounter
    Or :
    Code:
    For intcounter = 1 To lastcol
        Cells(1, intcounter).Select
        cell.Value = CInt(cell.Value)
    Next intcounter
    All 3 methods work fine

    Please rate this post if it was useful for you!
    Please try to search before creating a new post,
    Please format code using [ code ][ /code ], and
    Post sample code, error details & problem details

  11. #11
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    i will put my 2 cents into this as well
    VB Code:
    1. mydouble = 3.4
    2.  
    3. dim pos as integer
    4. pos = instr(1, mydouble, ".")
    5.  
    6. myNumberwithoutdec = mid(1, mydouble, pos -1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width