+ Reply to Thread
Results 1 to 7 of 7

Syntax for formatting a number with a comma for thousands

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Syntax for formatting a number with a comma for thousands

    I'm using this code to add a column total, what do I need to add to centre the number in the cell and to format it using a comma for thousands?


            LastRow = Range("D65536").End(xlUp).Offset(2, 0).Row
            Range("D" & LastRow) = "=Sum(D1:D" & LastRow - 1 & ")"
            Range("D" & LastRow).Font.Bold = True
            Range("C" & LastRow) = "Total"
            Range("C" & LastRow).Font.Bold = True
    Many thanks

  2. #2
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Syntax for formatting a number with a comma for thousands

    If you record it with the macro recorder you will get the code.
    Notice my main language is not English.

    I appreciate it, if you reply on my solution.

    If you are satisfied with the solution, please mark the question solved.

    You can add reputation by clicking on the star * add reputation.

  3. #3
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Syntax for formatting a number with a comma for thousands

    Hi oeldere,

    Okay, I now have this, which works, but is there a more 'efficient' way to write this, with fewer lines of code or is this the best way? I basically want the last row only to be formatted as bold and with the numeric value in column D to include the comma for thousands and be centred, but the word 'Total' in column C to be left ranged.

    Many thanks

            LastRow = Range("D65536").End(xlUp).Offset(2, 0).Row
            Range("D" & LastRow) = "=Sum(D1:D" & LastRow - 1 & ")"
            Range("D" & LastRow).Font.Bold = True
            Range("D" & LastRow).HorizontalAlignment = xlCenter
            Range("D" & LastRow).NumberFormat = "#,##0"
            Range("C" & LastRow) = "Total"
            Range("C" & LastRow).Font.Bold = True

  4. #4
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Syntax for formatting a number with a comma for thousands

    more 'efficient' way to write this, with fewer lines of code or is this the best way?

    Probably there is, but I don't have the solution for that.

    But fewer lines are not always a better code.

    A code you can understand is more usefull, than a code, which is shorter, but is magic for you.

  5. #5
    Forum Contributor
    Join Date
    06-21-2010
    Location
    -
    MS-Off Ver
    Excel 2010
    Posts
    1,211

    Re: Syntax for formatting a number with a comma for thousands

    Okay, that's good to know. The code works, which is the key point, so many thanks for your help...

  6. #6
    Forum Expert
    Join Date
    05-30-2012
    Location
    The Netherlands
    MS-Off Ver
    Office 365
    Posts
    14,987

    Re: Syntax for formatting a number with a comma for thousands

    Thanks for the reply.

    Glad I could help.

  7. #7
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Syntax for formatting a number with a comma for thousands

    You can use With blocks but it's not more efficient
            LastRow = Range("D65536").End(xlUp).Offset(2, 0).Row
            With Range("D" & LastRow)
                .formula = "=Sum(D1:D" & LastRow - 1 & ")"
                .Font.Bold = True
                .HorizontalAlignment = xlCenter
                .NumberFormat = "#,##0"
           End with
          with Range("C" & LastRow)
              .value = "Total"
              .Font.Bold = True
           End with
    Josie

    if at first you don't succeed try doing it the way your wife told you to

+ 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