-1

I have many buttons in userform. I don't want write for every buttons function "afterUpdate", because every will be similar. Is the way make it simpler than write single function for single button?

Sub button1_afterUpdate()

End sub


Sub button2_afterUpdate()    
    '............

    'The same code as button1 function

    '...........
End sub
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73
Mr Tea
  • 141
  • 7
  • Yes, you can probably do that, for example using a "control array". Without any examples though, it's difficult to make any actual suggestions as to how you'd do that. – Tim Williams Feb 05 '19 at 03:05
  • One approach is to put most of your code in a single sub and call that from each handler, passing any needed arguments. I meant something *specific* when I suggested examples though. I know what you mean by your question, but not exactly what you're doing. – Tim Williams Feb 05 '19 at 05:37

1 Answers1

1

An easy way would be to create a sub for the actual code that is the seame for every button:

Sub RunThisForEveryButton_afterUpdate()

    'This is the same code that should run in every button

End Sub

And then call this sub in every event …

Sub button1_afterUpdate()
    RunThisForEveryButton_afterUpdate
End sub

Sub button2_afterUpdate()    
    RunThisForEveryButton_afterUpdate
End sub

Another approach is to create one event for all buttons using WithEvents. Therefore have a look here VBA: Using WithEvents on UserForms.

T.M.
  • 9,436
  • 3
  • 33
  • 57
Pᴇʜ
  • 56,719
  • 10
  • 49
  • 73