Excel VBA will not keep leading zeros

I cannot find a way to keep leading zeros in my VBA code. Zeros are needed because they correspond to unique identifiers.

I tried changing the number format to text and 0000 .... in excel and the same approach in my actual code: ActiveSheet.Cells (i, j) .NumberFormat = "00000"

Any other suggestions? (If I manually enter these numbers in VBE, it also separates the leading zeros). EDIT:

Sheets.Add.Name = "Temp"
Sheets("Sheet1").Select
ActiveSheet.Cells(2, 2).NumberFormat = "000"
cid = Cells(2, 2)
MsgBox cid
Sheets("Sheet2").Select
ActiveSheet.Cells(6, 1).NumberFormat = "00000"
sid = Cells(6, 1)
Sheets("Temp").Select
Url = _
"URL;" & _
"http......asp?" & _
"X1=" & cid & "&" & _
"X2=" & sid & "&"

This is ultimately inside the loop, but I am debugging it as separate iterations.

+4
source share
2 answers

, . Numberformat . , , :

dim v as integer
v = val(Sheets("Sheet1").Cells(2, 2))
dim cid as string
cid = format(v, "000")

Select ActiveSheet , ; Cells , .
(Val , , ).


...:

Worksheets(1).Columns("A").NumberFormat = "@"
Worksheets(1).Cells(1, "A").Value = "00023"

Worksheets(1).Columns("A").NumberFormat = "000000"
Worksheets(1).Cells(1, "A").Value = "0023"

( 6 )

EDIT:
- , , ' :
( .)

Dim s As String
s = "0002"
Worksheets(1).Cells(1, "A").Value = "'" & s
+5

"00000" . . 0001 1, , 0001.

, .

Sub set_value()

Range("A1").NumberFormat = "@"
Range("A1").Value2 = "0001"

Range("B1").NumberFormat = "0000"
Range("B1").Value2 = "1"

End Sub
0

Source: https://habr.com/ru/post/1547984/


All Articles