Results 1 to 6 of 6

Thread: Excel - Number with Prefix 0

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    MD
    Posts
    182

    Excel - Number with Prefix 0

    I would like to enter number as text in Excel without using single quotes. Any help on this pls?

    To be very clear, I just need something equivalent to Excel --> Format --> Cell --> Category --> Text (Select Text from list)

    Example

    I currently have
    xlSheet.Cells(1, 1).Value = ("01012004")

    I don't want like this
    xlSheet.Cells(1, 1).Value = ("'01012004") 'Single quote before 0

    Thanks

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    You have to format cell to accept leading zeros:

    EDIT: here is a quick sample:
    VB Code:
    1. Private Sub Command1_Click()
    2. Dim exl As Object
    3. Dim wkb As Object
    4.  
    5.     'initialize and create excel object
    6.     Set exl = CreateObject("Excel.Application")
    7.     'create workbook object
    8.     Set wkb = exl.Workbooks.Add
    9.     'format cell to accept leading zeros
    10.     wkb.ActiveSheet.Range("A1").NumberFormat = String(10, "0")
    11.     'assign new value
    12.     wkb.ActiveSheet.Range("A1").Value = "000123"
    13.     'autosize
    14.     exl.Range(exl.Cells(1, 1), exl.Cells(100, 100)).Select
    15.     exl.Selection.Columns.AutoFit
    16.     exl.Cells(1, 1).Select
    17.     'show it
    18.     exl.Visible = True
    19.    
    20.     'clean up and exit
    21.     Set exl = Nothing
    22.     Set wkb = Nothing
    23.  
    24. End Sub
    Last edited by RhinoBull; Aug 2nd, 2004 at 11:55 AM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2001
    Location
    MD
    Posts
    182
    Thanks.

    But it doesn't work like Text Format in Excel.

    It gave a value "0000000123" in Excel. I need only "000123" to be displayed. Can you provide a generic code so that it will accept any string (with different lengths in a column?

    Thanks

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    I've already given you a "generic" sample code - the rest is upto you. Just work with NumberFormat property - if you cannot do it from within VB then open Excel directly and try experiment with one cell on Worsheet_Activate event (or something of that nature). That is generic property that allows you to create your own user format.

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    Something like this. Just modify to be dynamic.
    VB Code:
    1. wkb.ActiveSheet.Range("A1").Value = Right("0000000123", 6)
    2. 'Results = "000123"
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132
    That will only work if NumberFormat is set to six zeros ("000000") - value in the cell will be presented as "123" plus as many leading zeros as specified in that property OR only "123" if NumberFormat is "General".

Posting Permissions

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



Click Here to Expand Forum to Full Width