clean way to do this ? 3 text box's influencing 4th

unityjon

New Member
Joined
Jan 12, 2005
Messages
47
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi,
i'm slowly getting there after copying loads of examples and working through modify and create my own code to make a userform front end for a database, i'm almost done but looking back through the code some of the parts i have done myself look a little 'messy' but as they are specific to my needs trying to search for 'how to tidy this mess up' is fruitless. the following code takes input from comboboxes adn multiplies the three values together to create a 'score' which we will use elsewhere, i was wondering, seeing as the code is virtually idential could it be done with less code some how ?
Here is the current code:
Code:
Private Sub Reg11_Change()

If Reg11.Value >= 0 Then
    Reg14.Value = Reg11.Value * Reg12.Value * Reg13.Value
    End If
End Sub


Private Sub reg12_Change()
If Reg12.Value >= 0 Then
    Reg14.Value = Reg11.Value * Reg12.Value * Reg13.Value
    End If
End Sub

Private Sub Reg13_Change()
If Reg13.Value >= 0 Then
    Reg14.Value = Reg11.Value * Reg12.Value * Reg13.Value
    End If
End Sub

Private Sub Reg14_Change()
    Me.Reg14 = Reg14.Value
    
End Sub

Reg 11, 12, 13 are combobox's
Reg14 is just a textbox to display the result

it would look neater IMO without all the duplication, or am i just being pedantic and if its not broke dont try to fix it ?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
I dont see a need for
Private Sub Reg14_Change()
Me.Reg14 = Reg14.Value
End Sub

If you changed Reg14, then it already has a value. No need to set it again.
 
Upvote 0
Good point, i think i put that in for testing so that Reg14 was updated everytime.
I'm looking to see if i can create one if / then statement that captures all of the entries as they happen seeing as i set the values to "0" before hand
 
Upvote 0
You could try something like this:

Code:
Private Sub Reg11_Change()
If Reg11.Value >= 0 Then Call Calc
End Sub


Private Sub reg12_Change()
If Reg12.Value >= 0 Then Call Calc
End Sub


Private Sub Reg13_Change()
If Reg13.Value >= 0 Then Call Calc
End Sub


Private Sub Calc()
Reg14.Value = Reg11.Value * Reg12.Value * Reg13.Value
End Sub
 
Upvote 0
thats much neater, thank you. Got a few other areas i can apply the same technique too also. Cheers
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,848
Members
452,361
Latest member
d3ad3y3

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