Atlantis
Atlantis

Reputation: 592

how to add two numeric strings?

I am trying to display in a cell a numeric string value with the char "0" at the beginning. this is my code:

    Sub test2()
    Dim text As String
    text = Range("A1").Value
    Range("a1").Value = "0" & text

    End Sub

if the value in A1 is with letters it works fine (e.g. from "a" to "0a") if the value in A1 is numeric it displays only the first numeric value (e.g. from "11" to "11" instead of "011"

Upvotes: 2

Views: 181

Answers (1)

The approach to take depends upon:

  1. Whether you want to use VBA or a worksheet formula. In your example you have VBA, but it is perhaps not mandatory, or even to be avoided if possible (this is usually my choice, unless VBA is required).

  2. Whether you want the result to be numeric or string (you asked for a string, and it is the easier option).

A non-VBA, which gives you a string (from an integer number or a string in cell A1), is

="0"&TEXT(A1,"0")

With VBA, use the solution by TimWilliams (Range("a1").Value = "'0" & text instead of the last line of your code).

Upvotes: 2

Related Questions