Capping result from vba calculation

jose001

Board Regular
Joined
Mar 27, 2006
Messages
103
Afternoon Excel Gurus

I have a very perplexing problem which I can't get my head around. Would really appreciate some advice on the best solution.

On a Userform I have 1 ComboBox, 2 TextBox's and a button. The user makes a selection from the ComboBox and enters a value in the first Textbox. Excel then multiplies the first Textbox by a percentage and fills the third box. Everything is working OK (see below) but I need to ‘cap’ the result depending on the value of the combo i.e if the ComboBox is “0-1” the result from the calculation cannot exceed 25,000. Does anyone have any ideas on how I can do this?

Code:
Private Sub ComboBox1_Change()

If ComboBox1.Value = "0-1" Then
TextBox2.Value = TextBox1.Value * 0.01
ElseIf ComboBox1.Value = "1-5" Then
TextBox2.Value = TextBox17.Value * 0.005
ElseIf ComboBox1.Value = "5+" Then
TextBox27.Value = TextBox17.Value * 0.003
End If

End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Did you try to use another if?

Code:
Private Sub ComboBox1_Change()

If ComboBox1.Value = "0-1" Then
CalculatedVal = TextBox1.Value * 0.01
if CalculatedVal <=25000 then
  TextBox2.Value = CalculatedVal 
else
  TextBox2.Value = 25000
end if
ElseIf ComboBox1.Value = "1-5" Then
TextBox2.Value = TextBox17.Value * 0.005
ElseIf ComboBox1.Value = "5+" Then
TextBox27.Value = TextBox17.Value * 0.003
End If

End Sub
 
Upvote 0
Many Thanks for the quick reply Rafa, I didn't even realise you could do that. I'll try it now and let you know.

Thanks again :)
 
Upvote 0
Hi Rafa,

Is this terribly wrong? It's not working at the moment :(

Code:
Private Sub ComboBox1_Change()

If ComboBox1.Value = "0-1" Then
calculatedVal = TextBox1.Value * 0.01
If calculatedVal <= 25000 Then
  TextBox2.Value = calculatedVal
Else
  TextBox2.Value = 25000
End If

ElseIf ComboBox1.Value = "1-5" Then
calculatedVal = TextBox1.Value * 0.005
ElseIf calculatedVal <= 62500 Then
  TextBox2.Value = calculatedVal
Else
  TextBox2.Value = 62500
End If

ElseIf ComboBox1.Value = "5+" Then
calculatedVal = TextBox1.Value * 0.003
ElseIf calculatedVal <= 75000 Then
  TextBox2.Value = calculatedVal
Else
  TextBox2.Value = 75000
End If


End Sub
 
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,245
Members
452,900
Latest member
LisaGo

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