I have a lot of labels in VB that I use in With
statement to set their properties.
Problem Is there any way I can do something like the following:
With lblA, lblB, lblC
.fontColor = color.Red
End With
Is this possible, or do I have to manually do a With
statement for each of them?
VBA With is a statement to specify an object for once and then run multiple statements for it. In simple words, by using the “WITH” statement to specify an object, and after that, you can access all the properties and methods in one go.
By using With... End With , you can perform a series of statements on a specified object without specifying the name of the object multiple times. Within a With statement block, you can specify a member of the object starting with a period, as if the With statement object preceded it.
You can nest With statements by placing one With block within another.
The With ... End With statement allows you to write shorter code by referring to an object only once instead of using it with each property. The picture above shows a macro that changes a few properties of the Range Object.
There is a shorter and more readable version of your solution:
For Each lbl As Label In {lblA, lblB, lblC}
With lbl
'...
End With
Next
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With