Help Populating ListBox

Scott M

Board Regular
Joined
Dec 31, 2011
Messages
152
I would like to populate a list Box with the sum of the values entered in the textboxes on a user form without putting any data in a worksheet is this possible?
For example the user brings up the user form and enters 2 in textBox1 and 2 in textBox2 then the list box would show 4
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
Scott

You could do it like this.
Code:
Listbox1.AddItem Val(Textbox1.Value)  + Val(Textbox2.Value
 
Upvote 0

Misca

Well-known Member
Joined
Aug 12, 2009
Messages
1,742
Try something like
Code:
With Me
    .ListBox1.AddItem Val(.TextBox1.Value) + Val(.TextBox2.Value)
End With
The "Me" works when used in the userform module. If you're using the code from the normal module replace the "Me" with your userform name.
 
Upvote 0

Scott M

Board Regular
Joined
Dec 31, 2011
Messages
152
Thanks That works great. I do have one question The values that will be entered in the text boxes will be fractions currently if I enter for example 1/2 in textBox1 and 1/2 in textBox2 The List Box shows 2 Do you have any suggestions for formatting the textboxes and/or the list Box so that the values are fractions or maybe when the user enters a fraction in the Textboxes it will convert the value to decimals
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
ADVERTISEMENT
Instead of Val use Evaluate to get the decimal value of the fraction, then if you want to display as fraction in the listbox format the result as something like # ?/?.
 
Upvote 0

Scott M

Board Regular
Joined
Dec 31, 2011
Messages
152
Thanks Norie That was helpful I do need to format the result but it needs to be in inches and decimals 16.8593 for example I have tried several examples that I found but can't get any of them tor work could someone point me in the right direction here is the code that populates the list box.
Code:
 With Me
    .LstBxResult.AddItem Evaluate(.TxtFraimOpWdth.Value) + Val(.TxtScrbEndSum.Value) + Val(.TxtMidStyDiv2.Value) - Val(.TxtPartDiv2.Value)
 End With
   End If
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,367
Office Version
  1. 365
Platform
  1. Windows
ADVERTISEMENT
Scott

Do you just need the result formatted as a 'normal' number?

What exactly do you have in the texboxes and what problems are you having?
 
Upvote 0

Scott M

Board Regular
Joined
Dec 31, 2011
Messages
152
Hi Norie TxtFraimOpWdth.Value is a fraction i.e. 12 5/8 TxtScrbEndSum.Value Is the result of two list boxes that are fractions also. its value is decimal i.e. 1.75 TxtMidStyDiv2.Value is the result of a text Box value that is a fraction also and is divided by 2 and that result is a decimal. TxtPartDiv2.Value is also the result of a textbox value that is a fraction divided by2 and that result is a decimal The list box LstBxResult is in decimals but the decimal places are way more than I need I need the Result of the Code I posted to be 4 decimal places not 5 or 8.
I hope that I explained this clear enough.
Thanks for your help

<colgroup><col style="width: 48pt;" width="64"> <tbody>
</tbody>
 
Upvote 0

Scott M

Board Regular
Joined
Dec 31, 2011
Messages
152
Norie I just need the result formatted as a normal number Like 16.8593.It needs to be no more than 4 Decimal places
Don't Know If this maters or not but the list box is populated when the user clicks on a command button

<colgroup><col style="width: 48pt;" width="64"> <tbody>
</tbody>


<colgroup><col style="width: 48pt;" width="64"> <tbody>
</tbody>
 
Upvote 0

ZVI

MrExcel MVP
Joined
Apr 9, 2008
Messages
3,875
Office Version
  1. 2019
  2. 2016
  3. 2010
Platform
  1. Windows
Try this:
Rich (BB code):
Private Sub CommandButton1_Click()
  Dim x As Double
  x = Eval(TxtFraimOpWdth) + Eval(TxtScrbEndSum) + Eval(TxtMidStyDiv2) - Eval(TxtPartDiv2)
  LstBxResult.AddItem Format(x, "#0.0000")
End Sub
 
Function Eval(Equation As String) As Double
  Dim s As String
  s = Replace(Application.Trim(Equation), " ", "+")
  If Len(s) Then Eval = Evaluate(s)
End Function
 
Upvote 0

Forum statistics

Threads
1,195,663
Messages
6,011,009
Members
441,579
Latest member
satishrazdhan

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
Top