How can I condense this?

viper

Active Member
Joined
Feb 15, 2002
Messages
382
These several if statements perform exactly as I need but I'm wondering if there is a condensed way to achieve the same results?

Private Sub Accountnumber_Change()

If Accountnumber = "500" Then
UserForm1.Check500.Value = True
UserForm1.TextBox500.Value = Invoiceamount
End If
If Accountnumber = "513" Then
UserForm1.Check513.Value = True
UserForm1.TextBox513.Value = Invoiceamount
End If
If Accountnumber = "510" Then
UserForm1.Check510.Value = True
UserForm1.TextBox510.Value = Invoiceamount
End If
If Accountnumber = "610" Then
UserForm1.Check610.Value = True
UserForm1.TextBox610.Value = Invoiceamount
End If
If Accountnumber = "612" Then
UserForm1.Check612.Value = True
UserForm1.TextBox612.Value = Invoiceamount
End If
If Accountnumber = "613" Then
UserForm1.Check613.Value = True
UserForm1.TextBox613.Value = Invoiceamount
End If
If Accountnumber = "614" Then
UserForm1.Check614.Value = True
UserForm1.TextBox614.Value = Invoiceamount
End If
If Accountnumber = "617" Then
UserForm1.Check617.Value = True
UserForm1.TextBox617.Value = Invoiceamount
End If
If Accountnumber = "624" Then
UserForm1.Check624.Value = True
UserForm1.TextBox624.Value = Invoiceamount
End If

End Sub

I also need to add:
If Accountnumber <> (any of the above numbers) then
Textboxother = Accountnumber
Textboxotherdollar = Invoiceamount
End If
Thanks,
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
<pre>
Private Sub Accountnumber_Change()

On Error GoTo ERR_OTHER_NUMBER
UserForm1.Controls("Check" & Accountnumber).Value = True
UserForm1.Controls("TextBox" & Accountnumber).Value = Invoiceamount

Exit Sub

ERR_OTHER_NUMBER:
'you may want to put a msgbox in here to inform the user that a new account _
number needs to be created
Textboxother = Accountnumber
Textboxotherdollar = Invoiceamount

End Sub</pre>

This should work, give it a try and let me know.

HTH :biggrin:

_________________<font color = green> Mark O'Brien
This message was edited by Mark O'Brien on 2002-07-19 22:29
 
Upvote 0
Hi Viper

How about:

Code:
Dim iNum As Integer
Dim cContCh As Control
Dim cContTb As Control
Select Case Accountnumber.Value
    Case 500, 513, 510, 610, 612, 613, 614, 617, 624
    iNum = Accountnumber.Value
    Set cContCh = UserForm1.Controls("Check" & iNum)
    Set cContTb = UserForm1.Controls("TextBox" & iNum)
cContCh = True
cContTb = iNum
Case Else
Textboxother = Accountnumber
Textboxotherdollar = Invoiceamount
End Select
 
Upvote 0
Thanks again Mark,

I used yours and added Textboxother.value = "" before Exit Sub. Although it worked it kept the first two digits of the account number in the other box until the third matched an account. But works really well.

Thanks also Dave,
I had an error with this line of code:
Set cContTb = UserForm1.Controls("TextBox" & iNum)
I forgot the exact error but something about matching I think. With my time restraint on this project I didn't have time to find out why. But I'll have a look at it later because I like both of your guy's code, and I can tailor them for other projects.

thanks again
 
Upvote 0

Forum statistics

Threads
1,214,588
Messages
6,120,412
Members
448,959
Latest member
camelliaCase

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