Question

I write the following line into Excel file from my VBscript code

excelFile.write("001"  & vbTab)

But when I check the excel it has value 1 instead of 001

How can I convince excel to "not helping me" from VBscript?

I cannot change all of the user's excel. I need do it from VBScript.

When I write into txt file the number is correct.

Was it helpful?

Solution 4

This is solved my problem

excelFile.write("="""001""""  & vbTab)

From here:

OTHER TIPS

As you mention a text file, I believe execFile is a text stream that writes a tab delimited file that is imported into Excel later. Then you should quote (all) the data in (all) the text columns:

excelFile.write """001"""  & vbTab

You may have to state this in the import specification and/or in a schema.ini file.

'Firstly in Excel select entire column right click and select Format Cells, 'Custom and enter 000

'vbscript to make any cell in column A format 000 would be as follows

enter code here
Set xl = CreateObject("Excel.Application") 
xl.Visible =False
Set wb1 = xl.WorkBooks.Open("c:\Test.xls")
xl.DisplayAlerts = False
xl.columns("A:A").NUMBERFORMAT="000"
wb1.Save
xl.displayalerts=false
xl.Quit 
Set wb1 = nothing
Set xl = Nothing

Change the format of the cell that you're writing into from 'General' to 'Custom' '000' - This will give you 3 digits to any number you enter

If you need to do it from VB itself you should be able to do something like:

 Cells.Select 

    With Selection
        .NumberFormat = "000"
    End With

Try this:

excelFile.write("'001")

I just put ' before the first 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top