Consulting

Results 1 to 4 of 4

Thread: Converting numbers to text by macro

  1. #1

    Converting numbers to text by macro

    I recently saw this macro on another forum, where it was offered as a method to change the values of numbers that had been stored as text, to numbers that can be summed. Doesn't excel have an inbuilt function that can already do this?
    Sub replnos()
    'replnos Macro
    ' Replace 0-9 with themselves to convert numbers stored as text to actual calculable numbers - Ann's cheat
    '
    ' Cells.Replace What:="0", Replacement:="0", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Cells.Replace What:="1", Replacement:="1", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Cells.Replace What:="2", Replacement:="2", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Cells.Replace What:="3", Replacement:="3", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Cells.Replace What:="4", Replacement:="4", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Cells.Replace What:="5", Replacement:="5", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Cells.Replace What:="6", Replacement:="6", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Cells.Replace What:="7", Replacement:="7", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Cells.Replace What:="8", Replacement:="8", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    Cells.Replace What:="9", Replacement:="9", LookAt:=xlPart, SearchOrder _
    :=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
    End Sub

  2. #2
    VBAX Regular
    Joined
    Sep 2023
    Posts
    97
    Location
    You can copy the range of numbers that are text back into itself as a value and this automatically converts them to numbers. So, say a Range("A2:A25") is numbers entered as text, using this line
    Sheet1.Range("A2:A25") = Sheet1.Range("A2:A25").Value
    will convert them to numbers.

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Welcome back to VBAX Ian. As jdelano has suggested there is a much simpler method than the code as provided.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    If you don't want to use VBA, an array formula will work

    The numbers are entered as '1, '2, etc to ensure that they are really Text

    The Sum formula is entered as Control-Shift-Enter and that will add the braces

    Capture.JPG

    Capture2.JPG
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •