Use of textboxes.value to do calculations

corentint

New Member
Joined
Jan 31, 2022
Messages
32
Office Version
  1. 365
Platform
  1. Windows
Hello to the community, hope all are fine.
Here is a peculiar problem in coding a userform.
  • I have textboxes in a userform that display values (by that I mean only numerical values, no alpha tolerated ;))
    • from a database Excel sheet into the userform.
  • The database does not include calculations, so in populating the textboxes in the userform, I sometime have to do the calculation in the useform code.
  • The calculations are done in the module that populates the useform
    • Using the textboxes themselve in formulae.
    • And then, formating the textbox according to what it contains ($, %, 0.0, etc.)
  • Example:
    • A calculated textbox (the content does not exist in the database):
      • TextBox221.Value = BD(LineNb, 10) * BD(LineNb, 47) where BD is the cell from the database in Excel - this works (displays the expected result)
    • The calculated textbox is then formated in a subroutine called after all calculated textboxes are completed (populated), as such:
      • TextBox221.Value = Format(TextBox221.Text, "0.00" & FXL) where FXL is a string containing a currency symbol - one character
      • This appears to work, but not always: sometimes it formats the value which is correct after calculation, BUT, sometimes it completely throws out the calculated result and I get weird number, always negative, and always values that are way above the expected results, i.e. :
      • Good result: 189.00$
      • Strange: -603245.000578 and not even formated.
    • When I follow step by step (F8) the VBA code, nothing seems amiss, not erreur messages. There are no "On error resume next" code anywhere in the module and procedures.
  • As I am writing this, I become more and more convinced I should not use textboxes to do direct computations between them, or that the formating should not be in a subroutine. Note that the subroutine is in the userform module, not outside of it. Not that this should matter, really...
Anyone has a though, idea or clue to how come this happens?
Or should I envision a reprogramming of all calculations outside the textboxes, just coding the format for the end result?
Thank you for your help, in advance.
Corentint
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Rather than TextBox221.Value = Format(TextBox221.Text, "0.00" & FXL) I should recommend:
VBA Code:
TextBox221.Value = Format(CDbl(TextBox221.Text), "0.00") & FXL
 
Upvote 0
Solution
Hello Anthony,

Thanks, your suggestion actually fixed my bug, and I do not have to recode another way.
Your answer implicitly confirms that it is possible to manipulate textboxes with value in formulae directly in a Userform; good.

By the way, your answer needed to respect the original in this respect:

TextBox221.Value = Format(CDbl(TextBox221.Text), "0.00" & FXL) the FLX is part of the string defining the format to display.
The CDbl did the trick (I knew of it, but did not think it would be needed here. hey I learn every day.

Thanks again.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top