ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   minimizing redundant code (https://www.excelbanter.com/excel-programming/355701-minimizing-redundant-code.html)

dreamz[_31_]

minimizing redundant code
 

let's say i have some code as follows:


Code:
--------------------
If ComboReg.Value = "National" Then
ListNat.Visible = True
ListE.Visible = False
ListW.Visible = False
ListC.Visible = False
ListS.Visible = False
--------------------


is there any way to group the different lists so that i only need to
write .visible = false once? maybe something like
(liste,listw,listc,lists).visible = false.

what about for with-end with constructions? is there a way i can
write:


Code:
--------------------
with (liste,listw,listc,lists)
insert code
end with
--------------------


or something to that effect?

thanks.


--
dreamz
------------------------------------------------------------------------
dreamz's Profile: http://www.excelforum.com/member.php...o&userid=26462
View this thread: http://www.excelforum.com/showthread...hreadid=521145


Mark Lincoln

minimizing redundant code
 
If you're toggling the visibility of a set of controls, you can put the
code in a Sub and call the Sub. The test that determines visibility
could be part of the Sub code, or you could set a flag before calling
the Sub to determine whether .visible is True or False.


dreamz[_32_]

minimizing redundant code
 

Mark Lincoln Wrote:
If you're toggling the visibility of a set of controls, you can put the
code in a Sub and call the Sub. The test that determines visibility
could be part of the Sub code, or you could set a flag before calling
the Sub to determine whether .visible is True or False.

thanks for the reply. that would work, except that in my case, each
instance of the group of controls would have different visibility
properties, so i'd need to write them out anyway.

it's simple code. i was just wondering if there was a simpler way.


how about the second question? is there a simple way to format a group
of objects so that they all have the same format?


--
dreamz
------------------------------------------------------------------------
dreamz's Profile: http://www.excelforum.com/member.php...o&userid=26462
View this thread: http://www.excelforum.com/showthread...hreadid=521145


Bernie Deitrick

minimizing redundant code
 
Generally, you need to do a loop:

Dim myThing As Whatever

For Each myThing In WhateverCollection
myThing.Visible = False
Next myThing

That said, there isn't always a collection available, so you often need to improvise

Dim i As Integer

For i = 1 To 5
UserForm1.Controls("CheckBox" & i).Visible = False
Next i

And then you would need to pay attention to your naming conventions.


HTH,
Bernie
MS Excel MVP


"dreamz" wrote in message
...

let's say i have some code as follows:


Code:
--------------------
If ComboReg.Value = "National" Then
ListNat.Visible = True
ListE.Visible = False
ListW.Visible = False
ListC.Visible = False
ListS.Visible = False
--------------------


is there any way to group the different lists so that i only need to
write .visible = false once? maybe something like
(liste,listw,listc,lists).visible = false.

what about for with-end with constructions? is there a way i can
write:


Code:
--------------------
with (liste,listw,listc,lists)
insert code
end with
--------------------


or something to that effect?

thanks.


--
dreamz
------------------------------------------------------------------------
dreamz's Profile: http://www.excelforum.com/member.php...o&userid=26462
View this thread: http://www.excelforum.com/showthread...hreadid=521145




Mark Lincoln

minimizing redundant code
 
is there a simple way to format a group
of objects so that they all have the same format?


At the risk of striking out completely... :)

I've occasionally created an array of control objects and used a
For-Next loop to set properties. I would flag a control to set its
property opposite to the others by setting an integer variable to the
array index of the control in question.

If you need to set one control of the group to visible and the rest to
not visible, you might keep track of which control is visible by
storing it in an object variable. When your situation changes, set the
visible control to not visible, then set the pertinent control to
visible and flag it as such.

I hope this helps a little bit.



All times are GMT +1. The time now is 09:54 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com