Excel VBA - Rows Starting With 0

Dim testtext As String
testtext = Worksheets("Sheet2").Range("B6").Value
Worksheets("Sheet2").Range("B7").Value = testtext

Easily copy one cell content to another. But I ran into problems when the contents of B6 '02345- after running the macro, B7 contains 2345, and I do not want to lose the initial zero. I tried replacing .Valuewith .Textin the second line of code, and that didn't help.

+4
source share
4 answers

If you change the .NumberFormat property to "@", it will write 02345 in the cell.

Dim testtext As String
testtext = Worksheets("Sheet2").Range("B6").Value
Worksheets("Sheet2").Range("B7").NumberFormat = "@"

Worksheets("Sheet2").Range("B7").Value = testtext

, 0 , 0s . Excel particualar, , .

+9

, Range,

With Worksheets("Sheet2")
  .Range("B7") = .Range("B6")
End With

, .
testext, Variant.

+4

copy.

   Worksheets("Sheet2").Range("B6").copy Worksheets("Sheet2").Range("B7")
+2

You can use the FormulaR1C1cell property to insert a single quote, which causes Excel to interpret it as text. This mimics what actually happens when you type '02345in B6initially, as you can see by doing this with the macro recorder turned on:

Worksheets("Sheet2").Range("B7").FormulaR1C1 = "'" & testtext
+1
source

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


All Articles