Looping With a Set of Variables

Senior

New Member
Joined
Apr 5, 2011
Messages
6
I want to do a loop, processing against a set of variables, all of the same type. I think I could put the set of variables into a range and read them from there, but didn't want to add a bunch of ranges. I'd like to do it something like described below, but haven't found a close enough reference or sample for the syntax.

What I need to accomplish:

Process Var1
Process Var2
Process Var3
Process Var4

How I'd like to do it:

Dim myVariable as Integer
For Each myVariable In (Var1,Var2,Var3,Var4)
Process myVariable
Next myVariable

------------

I thought perhaps I could do it like this:

Sub Process (myVariable as Integer)
Process myVariable
End Sub

Call Process (Var1)
Call Process (Var2)
Call Process (Var3)
Call Process (Var4)
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
can you expand on what the variables will be used for, populating cells, performing mathematical functions,

a number of loop types are available
 
Upvote 0
In this case the variable is an integer corresponding to a column number previously calculated using the match function. Each column gets a variable. I use this for filtering, creating views (not to be confused with Custom Views), and other functions.

I do this because Custom Views sorely broke for me... consistently, but that is another story.

Here is the processing against the variable. The values passed are a selection of not necessarily adjacent column numbers.


Range("A1").Offset(0, myVariable - 1).Select
On Error Resume Next
Selection.EntireColumn.Hidden = False

OR to go the other way

Range("A1").Offset(0, myVariable - 1).Select
On Error Resume Next
Selection.EntireColumn.Hidden = True
 
Upvote 0
I solved it with a subroutine. Perhaps it could be more efficient, but works for me.

Public Sub Hide_Column(myColumn As Integer, myHidden As String)
'MyHidden is True or False
Range("A1").Offset(0, myColumn - 1).Select
On Error Resume Next
Selection.EntireColumn.Hidden = myHidden
End Sub


Call it as follows:

Sub Hide_Left ()
Sub Declare_Column_Offsets 'Declares and sets Columns 1 to 40
Sheets("Data").Select
Call Hide_Column(Column1, "True")
Call Hide_Column(Column13, "True")
Call Hide_Column(Column27, "True")

Call Hide_Column(Column2, "False")
Call Hide_Column(Column14, "False")
Call Hide_Column(Column28, "False")
End Sub

Sub Hide_Right ()
Sub Declare_Column_Offsets
Sheets("Data").Select
Call Hide_Column(Column1, "False")
Call Hide_Column(Column13, "False")
Call Hide_Column(Column27, "False")

Call Hide_Column(Column2, "True")
Call Hide_Column(Column14, "True")
Call Hide_Column(Column28, "True")
End Sub

:)
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,706
Members
452,939
Latest member
WCrawford

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