+ Reply to Thread
Results 1 to 14 of 14

How do I get a right justified column of numbers

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    319

    How do I get a right justified column of numbers

    I have a column of numbers ranging from about 1 to about 999 (1 to 3 digits). I want these values to appear in a column in a report, right-justified.

    <Disclaimer> Yes - I know Excel is not a report writer, and Yes - I know that if I want to think in COBOL I should go back to the dim and distant past </Disclaimer>

    BUT

    How do I get a column of numbers in a report (a series of strings) to show up right justified?

    I've tried all sorts of format codes, and none of them work (they all shift the value to the left). I've worked around this in the past by prepending a string (comprised of spaces) of the maximum length adding the numerical value and then taking the rightmost characters:

    a=1
    
    result1=right(string(6," ") & a,6)
    
    a=999
    
    result2=right(string(6," ") & a,6)
    t hi s seem slik a pre tty st o o o PID wa yt od ot hi ng s!


    Can Excel VBA produce this rather basic request without requiring these contortions?

    Tony (Head Against Wall Mode Again)
    Last edited by tfurnivall; 04-09-2025 at 02:38 PM.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,987

    Re: How do I get a right justified column of numbers

    I am baffled by your request, because numeric values are justified to the right by default.

    However, first you said "numbers" then you mentioned "strings." Are you using text strings to represent numbers? That's the first thing to be fixed.

    Well, regardless, I am still baffled because you can simply select the column and click the "right justify" format button.

    Can you please attach a sample file that illustrates your problem? See yellow banner at the top of the page.

    right.jpg
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    319

    Re: How do I get a right justified column of numbers

    I obviously did not make it clear - I'm after a set of strings containing numeric values, where I want the column to be right justified. This is NOT a question of Excel columns or cells, it is simply the way the data is formatted.

    And no, by default it seems that all strings created using Format (number, formatstring) are left justified. Why this should be so I don't know, but that's my problem.

    Thanks,

    Tony

  4. #4
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    30,881

    Re: How do I get a right justified column of numbers

    Try

    Columns(1).HorizontalAlignment = xlRight
    Column A formatted as TEXT
    Attached Images Attached Images
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,987

    Re: How do I get a right justified column of numbers

    Using a huge font does not make your question easier to understand.

    Give us a sample file showing the problem.

  6. #6
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    319

    Re: How do I get a right justified column of numbers

    OK

    Here is some code:
    Sub TestFormats()
    
    Const fmtnum1 = "###0"
    Const fmtnum2 = "###0 "
    Const fmtnum3 = "###0: "
    
    Dim i As Long
    Dim j As Long
    
    For i = 1 To 4
        j = Int(10 ^ (i - 1) * Rnd()) '   Get a (relatively) random integer
    
        Debug.Print i & ": " & j, fmtnum1 & ">>" & Format(j, fmtnum1), Right(String(Len(fmtnum1), " ") & Format(j, fmtnum1), Len(fmtnum1)),
        Debug.Print fmtnum2 & ">>" & Format(j, fmtnum2), Right(String(Len(fmtnum2), " ") & Format(j, fmtnum2), Len(fmtnum2)),
        Debug.Print fmtnum3 & ">>" & Format(j, fmtnum3), Right(String(Len(fmtnum3), " ") & Format(j, fmtnum3), Len(fmtnum3))
        
    Next i
    
    
    End Sub
    We set up three different numeric formats - with some additional characters (as literals).

    We loop through the process of
    Get a random digit between 1 and 999
    Format that value using each of the three format codes. (None of them work)
    The only way I can find to get right justified columns is the ghastly construct in each debug.print line, viz:
    Right(String(Len(fmtnum1), " ") & Format(j, fmtnum1), Len(fmtnum1))
    Careful examination will reveal that there are no references to any cells on any spreadsheets! Yes - I know that Excel as a workbook can do all this in its sleep. However, I want to be able to get a series of numbers right justified within a fixed width string. Why do I want that? Just cos. It's a requirement, and I'm sufficiently pleased with VBA as a general purpose tool that I can live with most of its eccentricities. However this seems a very torturous method of getting what I can accomplish (in COBOL) with "PIC 9(4)" or PIC Z(3)9".

    My simple request is "Is there a better way? If not, I can live with it (grumbling the while), but if there is a better way I'd love to learn it! (Unless it involves writing the whole report as a spreadsheet, in which case I will assume that there is no such alternative!)

    Thanks, again, and sorry for the rants!

    Tony

  7. #7
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,438

    Re: How do I get a right justified column of numbers

    I, like it appears all of the other respondents, were assuming that you were talking about writing your data to Excel, then using Excel's formatting to get the data to be right justified in a spreadsheet cell. This latest post suggests to me that you are not wanting to write data to Excel at all. What you are trying to do is create a text output (write to a text file??) and use something that looks like fixed width fields and have the data right justified in those text fields. Is that an accurate description of what you want?

    If this is correct, I found this stackoverflow q&a that addresses this: https://stackoverflow.com/questions/...from-excel-vba Note that the strategy is to combine the number (as text) with a suitable number of space characters to get the desired text field (which is similar to what you are doing but without using a Format() function). The responder also mentions that the Print# statement has some toggles that can control how text is positioned within the overall print action.

    Before wasting a lot of time chasing this particular wild goose, can you confirm that I have correctly understood the problem?
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  8. #8
    Forum Contributor
    Join Date
    11-15-2012
    Location
    Buffalo, NY
    MS-Off Ver
    Office 365
    Posts
    319

    Re: How do I get a right justified column of numbers

    Yes.

    I am using VBA to write a text file which contains lots (possibly over 1000 lines) of lines with several right-justified (and possibly even decimal point aligned) numerical fields in each line.

    That's all I want to do, and because sometimes the format of the numeric data gets a little hairy, I'm committed to using the format codes. However, the apparent inability of Excel VBA (not the spreadsheet the programming language) to handle this is what I was trying to solve.

    I'm sorry if my repeated attempts to point out that I was not using a spreadsheet, simply a programming language didn't come across clearly. Thanks for using the alternate construct "Writing to a text file using VBA). That's exactly what I'm trying to do.

    I suspect my kluge is the solution (using a custom routine is likely not going to provide the generality I need, and the layout of each line is different), I just don't like it - that's all!

    Thanks,

    Tony

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,987

    Re: How do I get a right justified column of numbers

    Quote Originally Posted by tfurnivall View Post
    I have a column of numbers ranging from about 1 to about 999 (1 to 3 digits).
    Oh, well that should be pretty straightforward....
    Quote Originally Posted by tfurnivall View Post
    several right-justified (and possibly even decimal point aligned) fields
    ....oh, wait.

  10. #10
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,961

    Re: How do I get a right justified column of numbers

    Sub TestFormats()
        Dim i           As Long
        Dim j           As Long
        Dim MaxDigits   As Long
        Dim TestDigits  As Long
        Dim LeftPads   As String
        Dim Str As String
        
        MaxDigits = 7   'min of one space and up to 6 digit num
        LeftPads = WorksheetFunction.Rept(" ", MaxDigits)
        Randomize
        For i = 1 To 4
            j = Int(10 ^ (i - 1) * Rnd())
            TestDigits = Len(CStr(j))
            Str = Left(LeftPads, MaxDigits - TestDigits) & j
            Debug.Print i & ": " & j, Str, Left(LeftPads, MaxDigits - TestDigits) & j
        Next i
    End Sub
    Ben Van Johnson

  11. #11
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,987

    Re: How do I get a right justified column of numbers

    The answer to this will depend on how you are presenting the resulting text file. Your example shows writing to the Immediate Window. Are you actually writing to an ASCII text file?

    Because you said you are printing strings (not numeric values) you can use
    Format(s, "@@@")
    to insert spaces to the left to pad out the string s to three characters.

    Padding with spaces will work only if you are presenting this using a fixed-width font. If you are using a proportional font, this is going to be difficult/impossible.

  12. #12
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,998

    Re: How do I get a right justified column of numbers

    It seems to me that you should just use RSet
    Everyone who confuses correlation and causation ends up dead.

  13. #13
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,961

    Re: How do I get a right justified column of numbers

    Forgot about RSet and LSet. Can't remember last time I saw them used...

  14. #14
    Forum Expert
    Join Date
    10-06-2017
    Location
    drevni ruchadlo
    MS-Off Ver
    old
    Posts
    2,270

    Re: How do I get a right justified column of numbers

    ???
    ... it's alive! ... it's alive! ...
    Sub allee_kiha()
        Dim i&, RndNbr$, strtxt$
        
        strtxt = String(44, ".")
        Debug.Print strtxt
        Randomize
        For i = 1 To 6
            RndNbr = Format(Round((Int(90 - 9 + 1) * Rnd + 9) / (Int(9 - 2 + 1) * Rnd + 2), 2), "#,##0.00")
            RSet strtxt = RndNbr
            Debug.Print strtxt
        Next
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 1
    Last Post: 01-15-2016, 07:05 PM
  2. Replies: 24
    Last Post: 08-08-2013, 08:30 PM
  3. Replies: 4
    Last Post: 04-30-2013, 06:46 PM
  4. [SOLVED] =IF(F2=0,"0",$D$2/$F$2) is formatting left justified I need it right justified but how?
    By Dark Prognosis in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-18-2012, 10:39 PM
  5. Right Justified
    By carley465 in forum Excel General
    Replies: 3
    Last Post: 10-19-2006, 06:00 PM
  6. [SOLVED] rows top-justified & bottom-justified in same sheet looks bad
    By Jim 007 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-08-2006, 07:00 PM
  7. Replies: 1
    Last Post: 03-27-2005, 09:06 PM

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