|
-
Aug 2nd, 2004, 11:32 AM
#1
Thread Starter
Addicted Member
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
-
Aug 2nd, 2004, 11:45 AM
#2
You have to format cell to accept leading zeros:
EDIT: here is a quick sample:
VB Code:
Private Sub Command1_Click()
Dim exl As Object
Dim wkb As Object
'initialize and create excel object
Set exl = CreateObject("Excel.Application")
'create workbook object
Set wkb = exl.Workbooks.Add
'format cell to accept leading zeros
wkb.ActiveSheet.Range("A1").NumberFormat = String(10, "0")
'assign new value
wkb.ActiveSheet.Range("A1").Value = "000123"
'autosize
exl.Range(exl.Cells(1, 1), exl.Cells(100, 100)).Select
exl.Selection.Columns.AutoFit
exl.Cells(1, 1).Select
'show it
exl.Visible = True
'clean up and exit
Set exl = Nothing
Set wkb = Nothing
End Sub
Last edited by RhinoBull; Aug 2nd, 2004 at 11:55 AM.
-
Aug 2nd, 2004, 12:48 PM
#3
Thread Starter
Addicted Member
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
-
Aug 2nd, 2004, 12:58 PM
#4
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.
-
Aug 2nd, 2004, 01:16 PM
#5
Something like this. Just modify to be dynamic.
VB Code:
wkb.ActiveSheet.Range("A1").Value = Right("0000000123", 6)
'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 Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API 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 
-
Aug 2nd, 2004, 01:19 PM
#6
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|