Search code examples
vbaexcelcell-formatting

set a cell values with 0 at left vba


Im trying to introduce a value like "0001" to a cell with vba but all I set is 1 with no 000

this is my code

    Dim iRow As Long
    Dim ws As Worksheet: Set ws = Worksheets(BD_PRODXSIST)

    With ws    
        .Range("A" & iRow).Value = "0001"            
   End With

    Set ws = Nothing

Solution

  • To insert as text makes use of the apostrophe.

    .Range("A" & iRow).Value = "'0001"
    

    Or you can also change the format after entering the number:

    .Range("A" & iRow).Value = "1"
    .Range("B" & iRow).NumberFormat = "0000"