+ Reply to Thread
Results 1 to 6 of 6

Values in textbox are read as strings so I can't add them, how do i solve this?

  1. #1
    Forum Contributor
    Join Date
    12-03-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    107

    Values in textbox are read as strings so I can't add them, how do i solve this?

    I have no idea why excel vba is doing this.

    Please Login or Register  to view this content.

    So the textbox sum is supposed to add the amount in textbox_body and the amount in textbox_components. But that doesn't happen, instead, excel puts them together and treats it as a string. For example textbox_body has a value of 2.5 and textbox_components is 2.7, instead of getting 5.2 it returns 2.52.7 !!

    I also tried
    Please Login or Register  to view this content.
    and

    Please Login or Register  to view this content.
    but none of those worked.

    it may be important to note that the values textbox_body and textbox_components are extracted from an excel sheet like so:

    Please Login or Register  to view this content.


    What am i doing wrong here?
    Last edited by kjy1989; 03-06-2014 at 04:10 AM.

  2. #2
    Forum Guru :) Sixthsense :)'s Avatar
    Join Date
    01-01-2012
    Location
    India>Tamilnadu>Chennai
    MS-Off Ver
    2003 To 2010
    Posts
    12,684

    Re: Values in textbox are read as strings so I can't add them, how do i solve this?

    May be try this....

    Please Login or Register  to view this content.


    If your problem is solved, then please mark the thread as SOLVED>>Above your first post>>Thread Tools>>
    Mark your thread as Solved


    If the suggestion helps you, then Click *below to Add Reputation

  3. #3
    Forum Expert
    Join Date
    02-19-2013
    Location
    India
    MS-Off Ver
    07/16
    Posts
    2,386

    Re: Values in textbox are read as strings so I can't add them, how do i solve this?

    Don't Know much but Try keeping
    Please Login or Register  to view this content.
    -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    WANT TO SAY THANKS, HIT ADD REPUTATION (*) AT THE BOTTOM LEFT CORNER OF THE POST

    More we learn about excel, more it shows us, how less we know about it.

    for chemistry
    https://www.youtube.com/c/chemistrybyshivaansh

  4. #4
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Values in textbox are read as strings so I can't add them, how do i solve this?

    It is better to coerce to numeric:
    Please Login or Register  to view this content.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  5. #5
    Forum Contributor
    Join Date
    12-03-2012
    Location
    London, England
    MS-Off Ver
    Excel 2010
    Posts
    107

    Re: Values in textbox are read as strings so I can't add them, how do i solve this?

    Quote Originally Posted by Izandol View Post
    It is better to coerce to numeric:
    Please Login or Register  to view this content.
    thank you to all that replied, I tried everything but the only one that worked was yours Izandol, thank you. However i still have a problem with it.

    If either textbox_body or textbox_components has no value in it, it returns an error called Run time error 13: Type Mismatch.

    I know that's causing the problem because when i put 0 in the empty textbox, it works, but if it's empty. it doesnt, how do i solve this?

  6. #6
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Values in textbox are read as strings so I can't add them, how do i solve this?

    You may use Val instead of CDbl or you may test:
    Please Login or Register  to view this content.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Read & Write textbox
    By crossy5575 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-29-2012, 11:32 AM
  2. Read a Range of 5 cells of strings and assign value of 1-5
    By BeefNoodleSoup in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-16-2012, 12:15 PM
  3. macro to Read Data Strings
    By mithil in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-04-2010, 06:25 PM
  4. How to read values from a textbox on a userform
    By AnthonyWB in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-25-2010, 04:14 AM
  5. TextBox - Read / Write
    By Herve_Rob in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-31-2007, 02:50 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1