+ Reply to Thread
Results 1 to 8 of 8

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

Hybrid View

Science Boy How can use round in a macro... 11-28-2007, 07:45 PM
Ikaabod Give this a try: Sub... 11-28-2007, 07:59 PM
Science Boy Thank you very much, Ikaabod... 11-28-2007, 08:12 PM
Ikaabod I. You never assign "number"... 11-28-2007, 08:43 PM
Science Boy I thought that Cells(i... 11-29-2007, 02:26 AM
royUK You could also use Int to... 11-29-2007, 02:41 AM
  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

    Sub roundnumbers()
    
    Dim number As Integer
    Dim number2 As Integer
    
    
    i = 1
    
    Do
    
     
    Cells(i + 1, 2).Value = number
    number2 = Round(number, 0)
    Cells(i + 1, 2).Value = number2
    
    i = i + 1
    
    
    Loop Until Cells(i + 1, 1).Value = ""
    
    
    
    End Sub

  2. #2
    Valued Forum Contributor
    Join Date
    04-11-2006
    Posts
    407
    Give this a try:
    Sub roundNumbers()
        Application.ScreenUpdating = False
        Dim rng As Range
        For Each rng In Range("B:B")
            If rng.Value <> "" Then rng.Value = Round(rng, 0)
        Next rng
        Application.ScreenUpdating = True
    End Sub

  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

     Cells(i + 1, 2).Value = number
    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

    MsgBox Int(123.456)
    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

     Cells(i + 1, 2).Value = number
    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
    number = Cells(i + 1, 2).Value

+ 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