Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pad single-digit numbers with a zero

I have a loop like this to save sheets as CSV files but my first 9 sheets are name liked sinani-01 ... sinani-09 (not like sinani-1 ... sinani-9). How I can concatenate a 0 only before numbers less than 10?

Sub Adder()
    Dim animal As String
    Dim i As Integer
    For i = 1 To 120
        animal = "sinani-" & i
        Sheets(animal).SaveAs "E:\Data\CSV\" & animal & ".csv", xlCSV
    Next i
End Sub
like image 445
Suffii Avatar asked Jul 25 '15 16:07

Suffii


1 Answers

VBA has a Format() function that you can use to pad numbers.

animal = "sinani-" & Format$(i, "00")

This will pad single-digit numbers with a 0. Your two- and three-digit numbers will continue to work as expected.

like image 88
Bond Avatar answered Sep 24 '22 01:09

Bond