Results 1 to 8 of 8

Thread: [RESOLVED] text value convert into number

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    241

    Resolved [RESOLVED] text value convert into number

    Hi1
    Mytotal1 shows "45"
    cell value shows 45
    Cells(x, MyCl).Value = Mytotal1
    how do i convert text to number
    itried
    Cells(x, MyCl).text = Mytotal1
    run time error 1004 unable to set the text property in range class

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: text value convert into number

    Try
    Code:
    Cells(x, MyCl).text = Val(Mytotal1)

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: text value convert into number

    in excel, you can not set the text property of a cell, only the value or formula
    the code you are using should convert the text string to a number in the cell, unless the cell is formatted as text
    to explicitly, convert text to a number, depending on the usage, you can use val, cint, cdbl, ccur, or some others

    what is the exact problem you are having?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: text value convert into number

    Set the cell value and cell formatting appropriately.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    241

    Re: text value convert into number

    Mytotal1 has screen value that is showing what everthe value in there for example "191"
    as text
    when i compare if cells(x,Mycl).value = Mytotal1
    technically it matcehs 191 = "191"
    but still it exitthe iff statment becasue the cell has val Mytotal 1has text formate
    Cells(x, MyCl).text = Mytotal1

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: text value convert into number

    i don't know what the problem is you are getting as this should work fine
    vb Code:
    1. Dim mytot As String
    2. mytot = "121"   'string
    3. Range("c3").Value = 121   'value
    4. MsgBox Range("c3") = mytot   'true
    is the value of the cell calculated by formula?
    that is more likely to be an issue
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: text value convert into number

    Perhaps the cell was formatted as Text before the value is applied.
    You need to reformat the cell to "General" or "Number" BEFORE setting its value.
    Code:
    Cells(x, MyCl).NumberFormat = "General" '-- or "#,##0" or any valid number format
    Cells(x, MyCl).Value = Mytotal1
    Cells(x, MyCl).Text is read-only property.
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2007
    Posts
    241

    Re: text value convert into number

    Thanks for everybody its working great.

Posting Permissions

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



Click Here to Expand Forum to Full Width