+ Reply to Thread
Results 1 to 20 of 20

Subtracting text boxes from one another doesn't work if answer is negative.

  1. #1
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Subtracting text boxes from one another doesn't work if answer is negative.

    I have two text boxes that are set to deduct from one another on the user form when the user presses the submit button.
    If the value in the second text box is less than the value in the first giving a positive answer then it works fine but if the answer is negative then no answer is produced!

    The code for my deduction is:
    Please Login or Register  to view this content.

    Once the submit button is pressed the answer from this subtraction should be put into a particular cell on the spreadsheet and my code for this is:
    Please Login or Register  to view this content.
    Any help would be greatly appreciated.

    Many Thanks

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    Val() is a string function. That it works at all is probably lucky.

    I would use cDbl() to insure the math works, and also I would specifically test each value before attempting the math that it is a number.

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    Thanks for the response, I did not know Val() was a string function and could be problematic, i've changed a lot of code which I had used this.

    Unfortunately although better code it has not solved my issue of receiving blank results if the answer to the sum is a negative one!

    Is there anything else I can check or do I need to use a different formatting code for the box for when the result is filled into the spreadsheet?

    I have googled a lot to try and solve this from other peoples learnings but I cannot find anyone else with the same problem!

    Many thanks for any other help you can offer.

  4. #4
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    since the contents of textboxes are also strings, I do not see a problem with Val here? btw your code works for me with negative results. what are you seeing in the output cell?
    Last edited by JosephP; 07-16-2012 at 11:37 AM.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  5. #5
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    Thankyou for your responce,
    The output cell is within a table and simply displays nothing! But if the result is positive it shows a value as i would expect.

    Possibly there are issues where the numbers come from that go into the code if it works fine for you.

    The TargetRateTextBox is different dependant on the selection made by the user in the userform from a combobox (GenreSubSectionCBO), the Jobsummary worksheet is where the sheet administrator types the target rates that populate the userform.
    The TargetRateTextBox is populated from the worksheet dependant on a selection using the code;

    Please Login or Register  to view this content.
    Pic of worksheet where target rate is initially inputted by administrator;
    Screen Shot 2012-07-17 at 08.57.18.png

    The RateTextBox code is;

    Please Login or Register  to view this content.
    The Cost Totals Text Box code works out all machine and labour costs minus any deductions.
    The Activity production is a user typed textbox which i have tried to make stronger code as suggested in the first reply which told me to make sure the user inputs a numeric value with the if IsNumeric code, when i have applied this to my original code I get a Run-time error 13 'Type Mismatch'.
    This is the code as advised;

    Please Login or Register  to view this content.
    Sorry I know this is lots of info and probably makes more sense to me than to anyone else not working on my project, if you need any more info to help me then please ask.

    Many Thanks,

  6. #6
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    what formatting do you have applied to the cell? If you alter your code to:
    Please Login or Register  to view this content.
    what message do you see?

  7. #7
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    Please Login or Register  to view this content.
    It says
    Unformatted Value:
    Formatted Value:

    Screen Shot 2012-07-17 at 12.09.36.png

  8. #8
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    which tells us that the textbox is completely empty at this point, which it should not be. if you alter the initial code from
    Please Login or Register  to view this content.
    to
    Please Login or Register  to view this content.
    what messages do you see when the rate textbox value is larger than the target rate, and what do you actually see in the lossgaintextbox when you click the submit button?

  9. #9
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    When I apply this code both message boxes show the result that it should be e.g.; 1 - 100 = -99 but it still shows up blank when being transferred to my spreadsheet.

    Screen Shot 2012-07-18 at 14.49.52.png

    I have checked the formatting of the cells on the worksheet and set them all to currency, is there anything else i should do to the sheet itself?

    This problem makes no sense to me because the message boxes show that the calculation is fine and makes no sense that it only transfers data to the sheet if the result is over 0!

    Thank-you for sticking with me on this and helping out.

  10. #10
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    unless you can post a workbook, I suggest you step through the code and watch the LossGainTextBox to see when it gets cleared.

  11. #11
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    I have tried working through this and really cannot see the problem so please find attached my spreadsheet, I have deleted most un-relevant content so that it is a size that is sendable, if you want to run the user form you do it from the 'instructions, input & print' page; choose 'Clearing Job Sheet' then from the 'Sub Section' combo box drop down choose the first option 'test subsection 1', add a small number into the 'Activity Production' text box then go onto the labour tab and put any number in the 'total man hours worked' text box.
    It will also require a date entry.
    The Combobox will make the data go onto the 'CLS1' sheet.
    The values will make the rate higher than the target rate causing a negative number.

    Thanks again,
    Ben
    Attached Files Attached Files

  12. #12
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    it works for me. does the example workbook fail for you?

  13. #13
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    I've cleared the tables on sheet 'CLS1' then inputted two sets of data into the user form, 1 that results in a positive result ($0.50) and one that should have resulted in a negative result (-$11.50), as you can see from my print screen picture the pink loss/gain column only shows the positive result.

    Screen Shot 2012-07-18 at 22.00.25.png

    Was this not the case on your computer, should i re-install Office? Have you ever seen problems like this running Office on a Mac?

  14. #14
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    I thought you were using 2010. do you actually have 2011? or 2004?

  15. #15
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    I wasn't sure and assumed it was 2010 when filling in my profile. I am running 2011 for the Mac.

  16. #16
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    okay-I have some things to get done this afternoon but will try and test it later in 2011.

  17. #17
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    thank-you again for your time and help.

    Does this mean that code within VBA acts differently on different versions of excel that will eventually open the workbook?

  18. #18
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    it can do especially if both mac and win versions are involved (they are separate business units)

    ---------- Post added at 11:34 PM ---------- Previous post was at 11:08 PM ----------

    ok this seems to work
    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    05-18-2012
    Location
    Cleethorpes, England
    MS-Off Ver
    Excel 2011
    Posts
    20

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    That works well now, I'll have to incorporate some code to find out the platform and version then activate different code from this.

    Thanks again for all your help.

  20. #20
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Subtracting text boxes from one another doesn't work if answer is negative.

    that oughta work in all versions. if you need to test for mac you can use conditional compilation
    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)

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