ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Select range from a varaible (https://www.excelbanter.com/excel-programming/421967-select-range-varaible.html)

Excel User[_2_]

Select range from a varaible
 
Hi,

I'm trying to set a range that is hidden from an array, but cannot seem to
get it working, also is there a better way to set the range as you can see
from below after rng1 is set, rng2 is one column right from each column in
rng1, rng3 is one column right from each column in rng2 etc

private function hidecols(who)

Dim RngToHide As Range

RngToHide = who

rng1= "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
rng2 =
"BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
rng3 =
"BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
rng4 =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
rng5 =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"


Range(RngToHide).Select
Selection.EntireColumn.Hidden = True
End Sub


joel

Select range from a varaible
 
Try this

Sub test()
Call hidecols("rng1")
End Sub
Private Function hidecols(who)

Dim RngToHide As Range

Select Case who
Case "rng1":
rng = "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
Case "rng2":
rng = "BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
Case "rng3":
rng = "BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
Case "rng4":
rng =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
Case "rng5":
rng =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"
End Select

Range(rng).Select
Selection.EntireColumn.Hidden = True
End Function


"Excel User" wrote:

Hi,

I'm trying to set a range that is hidden from an array, but cannot seem to
get it working, also is there a better way to set the range as you can see
from below after rng1 is set, rng2 is one column right from each column in
rng1, rng3 is one column right from each column in rng2 etc

private function hidecols(who)

Dim RngToHide As Range

RngToHide = who

rng1= "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
rng2 =
"BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
rng3 =
"BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
rng4 =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
rng5 =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"


Range(RngToHide).Select
Selection.EntireColumn.Hidden = True
End Sub



Excel User[_2_]

Select range from a varaible
 
Joel,

Thanks for your code, I think I have found a bug, if I select the same range
in Excel and hide it works fine, however if I use the code to hide the
columns any merged cells which span the columns are also hidden - am I doing
something wrong?

i.e. cell bw5 to eo5 are a merged cell, rng1 includes bw if I try hiding the
column via the code all columns are hidden, but if you try doing this in
Excel it works - strange?

Thanks




"Joel" wrote in message
...
Try this

Sub test()
Call hidecols("rng1")
End Sub
Private Function hidecols(who)

Dim RngToHide As Range

Select Case who
Case "rng1":
rng = "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
Case "rng2":
rng =
"BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
Case "rng3":
rng =
"BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
Case "rng4":
rng =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
Case "rng5":
rng =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"
End Select

Range(rng).Select
Selection.EntireColumn.Hidden = True
End Function


"Excel User" wrote:

Hi,

I'm trying to set a range that is hidden from an array, but cannot seem
to
get it working, also is there a better way to set the range as you can
see
from below after rng1 is set, rng2 is one column right from each column
in
rng1, rng3 is one column right from each column in rng2 etc

private function hidecols(who)

Dim RngToHide As Range

RngToHide = who

rng1= "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
rng2 =
"BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
rng3 =
"BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
rng4 =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
rng5 =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"


Range(RngToHide).Select
Selection.EntireColumn.Hidden = True
End Sub



Rick Rothstein

Select range from a varaible
 
This is a slightly different approach, but it seems to work with merged
cells. In the subroutine below, you would pass in a "group" number instead
of a range name. For example, if you wanted to hide the columns associated
with "rng3", you would just pass in the 3. For example...

Sub Test()
HideColumnGroup 3
End Sub

Here is the code for the HideColumnGroup subroutine...

Sub HideColumnGroup(Who As Long)
Range("BW:ED").EntireColumn.Hidden = False
Range("BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,D M:DM,DS:DS,DY:DY"). _
EntireColumn.Offset(, Who - 1).Hidden = True
End Sub

Note that the first statement of the subroutine unhides **all** hidden rows
within the grouping area (cell range BW:ED) before it hides the requested
group of columns. If you don't want previously hidden columns to become
unhidden, the just remove that first statement.

--
Rick (MVP - Excel)


"Excel User" wrote in message
...
Joel,

Thanks for your code, I think I have found a bug, if I select the same
range in Excel and hide it works fine, however if I use the code to hide
the columns any merged cells which span the columns are also hidden - am I
doing something wrong?

i.e. cell bw5 to eo5 are a merged cell, rng1 includes bw if I try hiding
the column via the code all columns are hidden, but if you try doing this
in Excel it works - strange?

Thanks




"Joel" wrote in message
...
Try this

Sub test()
Call hidecols("rng1")
End Sub
Private Function hidecols(who)

Dim RngToHide As Range

Select Case who
Case "rng1":
rng = "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
Case "rng2":
rng =
"BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
Case "rng3":
rng =
"BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
Case "rng4":
rng =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
Case "rng5":
rng =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"
End Select

Range(rng).Select
Selection.EntireColumn.Hidden = True
End Function


"Excel User" wrote:

Hi,

I'm trying to set a range that is hidden from an array, but cannot seem
to
get it working, also is there a better way to set the range as you can
see
from below after rng1 is set, rng2 is one column right from each column
in
rng1, rng3 is one column right from each column in rng2 etc

private function hidecols(who)

Dim RngToHide As Range

RngToHide = who

rng1= "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
rng2 =
"BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
rng3 =
"BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
rng4 =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
rng5 =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"


Range(RngToHide).Select
Selection.EntireColumn.Hidden = True
End Sub




joel

Select range from a varaible
 
Your original posted code are assigning the ranges to variables rng1 - 5 but
these ranges weren't being used. You also passed a vairable who that wasn't
being used. My code just fixed these problems in your original code.

"Excel User" wrote:

Joel,

Thanks for your code, I think I have found a bug, if I select the same range
in Excel and hide it works fine, however if I use the code to hide the
columns any merged cells which span the columns are also hidden - am I doing
something wrong?

i.e. cell bw5 to eo5 are a merged cell, rng1 includes bw if I try hiding the
column via the code all columns are hidden, but if you try doing this in
Excel it works - strange?

Thanks




"Joel" wrote in message
...
Try this

Sub test()
Call hidecols("rng1")
End Sub
Private Function hidecols(who)

Dim RngToHide As Range

Select Case who
Case "rng1":
rng = "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
Case "rng2":
rng =
"BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
Case "rng3":
rng =
"BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
Case "rng4":
rng =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
Case "rng5":
rng =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"
End Select

Range(rng).Select
Selection.EntireColumn.Hidden = True
End Function


"Excel User" wrote:

Hi,

I'm trying to set a range that is hidden from an array, but cannot seem
to
get it working, also is there a better way to set the range as you can
see
from below after rng1 is set, rng2 is one column right from each column
in
rng1, rng3 is one column right from each column in rng2 etc

private function hidecols(who)

Dim RngToHide As Range

RngToHide = who

rng1= "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
rng2 =
"BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
rng3 =
"BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
rng4 =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
rng5 =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"


Range(RngToHide).Select
Selection.EntireColumn.Hidden = True
End Sub




Excel User[_2_]

Select range from a varaible
 
Rick that's cool - thanks again !

"Rick Rothstein" wrote in message
...
This is a slightly different approach, but it seems to work with merged
cells. In the subroutine below, you would pass in a "group" number instead
of a range name. For example, if you wanted to hide the columns associated
with "rng3", you would just pass in the 3. For example...

Sub Test()
HideColumnGroup 3
End Sub

Here is the code for the HideColumnGroup subroutine...

Sub HideColumnGroup(Who As Long)
Range("BW:ED").EntireColumn.Hidden = False
Range("BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,D M:DM,DS:DS,DY:DY"). _
EntireColumn.Offset(, Who - 1).Hidden = True
End Sub

Note that the first statement of the subroutine unhides **all** hidden
rows within the grouping area (cell range BW:ED) before it hides the
requested group of columns. If you don't want previously hidden columns to
become unhidden, the just remove that first statement.

--
Rick (MVP - Excel)


"Excel User" wrote in message
...
Joel,

Thanks for your code, I think I have found a bug, if I select the same
range in Excel and hide it works fine, however if I use the code to hide
the columns any merged cells which span the columns are also hidden - am
I doing something wrong?

i.e. cell bw5 to eo5 are a merged cell, rng1 includes bw if I try hiding
the column via the code all columns are hidden, but if you try doing this
in Excel it works - strange?

Thanks




"Joel" wrote in message
...
Try this

Sub test()
Call hidecols("rng1")
End Sub
Private Function hidecols(who)

Dim RngToHide As Range

Select Case who
Case "rng1":
rng = "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
Case "rng2":
rng =
"BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
Case "rng3":
rng =
"BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
Case "rng4":
rng =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
Case "rng5":
rng =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"
End Select

Range(rng).Select
Selection.EntireColumn.Hidden = True
End Function


"Excel User" wrote:

Hi,

I'm trying to set a range that is hidden from an array, but cannot seem
to
get it working, also is there a better way to set the range as you can
see
from below after rng1 is set, rng2 is one column right from each column
in
rng1, rng3 is one column right from each column in rng2 etc

private function hidecols(who)

Dim RngToHide As Range

RngToHide = who

rng1= "BW:BW,CC:CC,CI:CI,CO:CO,CU:CU,DA:DA,DG:DG,DM:DM,D S:DS,DY:DY"
rng2 =
"BX:BX,CD:CD,CJ:CJ,CP:CP,CV:CV,DB:DB,DH:DH,DN:DN,D T:DT,DZ:DZ,EF:EF"
rng3 =
"BY:BY,CE:CE,CK:CK,CQ:CQ,CW:CW,DC:DC,DI:DI,DO:DO,D U:DU,DA:DA,EG:EG"
rng4 =
"BZ:BZ,CF:CF,CL:CL,CR:CR,CX:CX,DD:DD,DJ:DJ,DP:DP,D V:DV,DB:DB,EH:EH,EN:EN"
rng5 =
"BA:BA,CG:CG,CM:CM,CS:CS,CY:CV,DE:DE,DK:DK,DQ:DQ,D W:DW,DC:DC,EI:EI,EO:EO"


Range(RngToHide).Select
Selection.EntireColumn.Hidden = True
End Sub





All times are GMT +1. The time now is 07:56 PM.

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