Apply vba code to multiple userform objects

S

sjoopie

Hello,

Allthough I've only recently started to program vba in excel, I suspec
there must be an easy way to apply vba code to multiple objects in
userform. Could anyone help me with this? I would like to 'group' th
lines in the example below:

ProjectenForm.EgKbA.BackStyle = fmBackStyleOpaque
ProjectenForm.EgKmA.BackStyle = fmBackStyleOpaque
ProjectenForm.EgKdA.BackStyle = fmBackStyleOpaque
ProjectenForm.EgHbA.BackStyle = fmBackStyleOpaque
ProjectenForm.EgHmA.BackStyle = fmBackStyleOpaque
ProjectenForm.EgHdA.BackStyle = fmBackStyleOpaque
ProjectenForm.MgKbA.BackStyle = fmBackStyleOpaque
ProjectenForm.MgKmA.BackStyle = fmBackStyleOpaque
ProjectenForm.MgKdA.BackStyle = fmBackStyleOpaque
ProjectenForm.MgHbA.BackStyle = fmBackStyleOpaque
ProjectenForm.MgHmA.BackStyle = fmBackStyleOpaque
ProjectenForm.MgHdA.BackStyle = fmBackStyleOpaque

Greetings,
Jori
 
R

Robin Hammond

Use the controls collection. Something like this:

For every control on a form...
For Each C in Me.Controls

Next C

For specific controls with similar names

For nCounter = 1 to 10

Controls("txt" & nCounter).text = "test"

Next nCounter

for specific controls
vList = Array("txtA", "cbA","lstA")
For Each vCtl in Vlist
controls(vctl).enabled = true
Next vCtl
 
N

Nikos Yannacopoulos

Try this:

For Each ctl In ProjectenForm.Controls
ctl.BackStyle = fmBackStyleOpaque
Next

HTH,
Nikos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top