+ Reply to Thread
Results 1 to 8 of 8

How can use round in a macro to get rid of the decimals???

  1. #1
    Registered User
    Join Date
    02-20-2007
    Posts
    12

    How can use round in a macro to get rid of the decimals???

    Hi

    I try to round a bunch of numbers like 832.3536367 to 832. So I want get rid of the decimals not just formatting to show less decimals

    I want to do this in a macro by using a loop to go through each cell in a column until there are no more entrys (and it should carry on if the cell is empty) I've come up with the code below but this doesn't cut it even if all cells are NOT empty

    Column A is filled with ref numbers like 1,2,3,4 etc
    Column b is filled with the values like 4.1468 ; 540.4844; 1001.8811

    Thanks in advance

    Science boy

    Please Login or Register  to view this content.

  2. #2
    Valued Forum Contributor
    Join Date
    04-11-2006
    Posts
    407
    Give this a try:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    02-20-2007
    Posts
    12
    Thank you very much, Ikaabod

    It works!!


    Can you also (or anyone else) tell me what is wrong with my code?

  4. #4
    Valued Forum Contributor
    Join Date
    04-11-2006
    Posts
    407
    I. You never assign "number" a value so it defaults to zero.
    ____i. Thereby assigning "number2" the value "number" rounded, which is still zero
    ____ii. Then assigning the cell value "number2"'s value which is, again, zero

    II. Your Do/Loop ends when it finds a blank cell in Column A
    ____i. Therefore, cells in Column B that were empty, now become zero if the corresponding Column A is not empty
    ____ii. If you have one empty cell in your list of data in Column A the macro may end prematurely.

    III. By defining "number" and "number2" as Integers you may run into errors with larger numbers. The largest an integer may be is approximately +/- 32,800. Long and/or Double are able to be much larger. Be aware that Long and Double function differently than Integer and are not restricted to whole numbers.
    Last edited by Ikaabod; 11-28-2007 at 08:48 PM.

  5. #5
    Registered User
    Join Date
    02-20-2007
    Posts
    12
    I thought that

    Please Login or Register  to view this content.
    would assign the value in the cell to number.


    There will always be a continuous series of numbers in Column A so when there are no numbers there the macro is allowed to stop.

  6. #6
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You could also use Int to return the integer value of the number, eg

    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  7. #7
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229
    Quote Originally Posted by Science Boy
    I thought that

    Please Login or Register  to view this content.
    would assign the value in the cell to number.
    That code puts the value of number into the cell.

    If you want number to get its value from the cell, this would be the syntax
    Please Login or Register  to view this content.

  8. #8
    Registered User
    Join Date
    02-20-2007
    Posts
    12
    thanks mike,

    That's what happens when you try things when you really should be sleeping


    All,

    Thanks all suggestions were of use and as far as i'm concerned this can be locked

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1