Announcement

Collapse
No announcement yet.

Cstr function

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Cstr function

    hi everybody
    i am trying to convert a range of numbers to text
    using the Cstr function ,it doesnt work.
    excel VBA help states that CStr function converts a numeric value to a String.
    i am aware that i can convert a number by preceding it with an apostrophe
    i just need to know why Cstr doesnt do the job

    Code:
     Sub covrt()
        For Each rng In Selection.Cells
            rng = CStr(rng.Value)
        Next
      End Sub
    thanx in advance

  • #2
    Re: Cstr function

    Hi,

    The CSTR() function is working but the range object is helpfully converting your numeric value back into a real value instead of text because thats what Excel likes
    So you need to tell it that it should be text and not a number.
    Sub covrt()
    Dim strValue As String
    Dim rng As Range

    For Each rng In Selection.Cells
    strValue = CStr(rng.Value)
    rng.NumberFormat = "@"
    rng.value = strValue
    Next
    End Sub

    Cheers
    Andy

    Comment


    • #3
      Re: Cstr function

      thanx very much andy for this
      now i should keep in mind something new
      not only the logic behind excel but also
      what excel likes

      Comment


      • #4
        Re: Cstr function

        Wouldn't it be easier just to change the format

        Code:
            
                Selection.NumberFormat = "@"
        HTH

        Bob

        Comment

        Working...
        X