Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default 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


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default 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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,934
Default 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



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default 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





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default 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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Hyperlink varaible substitution - How To Sledge Bacon Excel Worksheet Functions 10 May 29th 08 06:03 PM
How do I macro sort with a varaible range (differing last cell)? sutibusan Excel Programming 2 November 7th 07 08:24 PM
Object varaible or With block variable not set? Rick S. Excel Programming 4 September 26th 07 07:20 PM
assining a varaible to the value in another sheet. shishi Excel Programming 1 August 8th 05 09:40 PM
Use varaible for worsheet name in a formule slm Excel Programming 3 July 19th 05 12:34 PM


All times are GMT +1. The time now is 12:51 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"