+ Reply to Thread
Results 1 to 5 of 5

Center data in a fixed length text field

  1. #1
    Registered User
    Join Date
    08-04-2009
    Location
    wisconsin
    MS-Off Ver
    Excel 2003
    Posts
    3

    Question Center data in a fixed length text field

    I know how to add in characters to the beginning or end of a cell to get it to a fixed length, but I would like to center the cell text with spaces around it.

    An example of this would be if cell A1=Bob and I wanted to pad this cell to 10 characters with the cell text in the middle and spaces around it.

    | Bob |

    How can I do this?

  2. #2
    Registered User
    Join Date
    08-04-2009
    Location
    wisconsin
    MS-Off Ver
    Excel 2003
    Posts
    3

    Re: Center data in a fixed length text field

    I guess the post auto-deletes excess spaces, in the above example I have spaces on both sides of Bob. Get back to me if you have any questions, and thanks.

  3. #3
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Center data in a fixed length text field

    =if(len(a1)>=10, a1, rept(" ", (10 - len(a1))/2) & a1 & rept(" ", (11 - len(a1))/2) )
    Entia non sunt multiplicanda sine necessitate

  4. #4
    p45cal
    Guest

    Re: Center data in a fixed length text field

    Since this Q is in the programming section I'd assumed vba..

    What to do when the padding can't be equal either side of the word? Case in point is your example, 'Bob' has three letters leaving seven to be distributed evenly round it; 3+4 or 4+3?

    Please Login or Register  to view this content.
    Callable from sheet or code:
    from code:
    Please Login or Register  to view this content.
    from worksheet:
    =PadOut("Bob",10)
    =PadOut(F1,15)

    No checks for the string being too long. What should happen then?
    Last edited by p45cal; 11-23-2009 at 01:43 PM. Reason: use of Space instead of String (shg induced) and removal of dross

  5. #5
    Forum Guru shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Center data in a fixed length text field

    Or ...
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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