?
Solved

Determine Range to Select

Posted on 2016-10-06
5
Medium Priority
?
76 Views
Last Modified: 2016-10-06
Hi All,

I have the code below which currently selects a fixed range and hides columns where the value of the cell in row 1 is not blank but is zero.
Range("L1:YF1").Select
    Selection.EntireColumn.Hidden = False
    
    For Each Cell In Selection
        Count = Count + 1
    Next Cell
        
    Range("L1").Select
    For Ch = 1 To Count
        If ActiveCell.Value <> "" And ActiveCell.Value = 0 Then
        Selection.EntireColumn.Hidden = True
        Else
        End If
        ActiveCell.Offset(0, 1).Range("$A$1").Select
    Next Ch
Range("$K$2").Select

Open in new window

Question is, line 1 selects a fixed Range but I would like to change this to the equivalent of "Shift + Ctrl + End" ie from current cell to the last cell in the row. End Right won't work because there are gaps.

I could go to XFD1 and then do an End Left to determine the last column but how do I then convert that into the range to be selected.

For the second section of the code where I am using ActiveCell.Offset to move across one column at a time. I assume this is the "right" way to do it because I assume that if I did it with "For Each Cell in Selection.... Next Cell" I would end up hiding the whole selection each time. Happy to be corrected on that point.

As always, much appreciated.
Thanks
Rob H
0
Comment
Question by:Rob Henson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 41832237
Why bother selecting anything at all?

Dim rng As Range
Dim cel As Range

Set rng = Range(Range("L1"), Cells(1, Columns.Count).End(xlToLeft))
For Each cel In rng.Cells
    If cel <> "" and cel = 0 Then
        cel.EntireColumn.Hidden = True
    End If
Next

Open in new window


Note that by using Columns.Count, this is also compatible with pre-2007 versions of Excel, which were limited to 256 columns.
0
 
LVL 33

Author Comment

by:Rob Henson
ID: 41832244
That looks great Patrick!!

Do I have to Unhide columns to start with? If a column was previously zero value it would get hidden but if there is now a value in that column I need it unhidden; hence line 2 unhiding the whole range.

There are no other hidden columns so I could select the whole sheet to Unhide if so required.
0
 
LVL 93

Accepted Solution

by:
Patrick Matthews earned 2000 total points
ID: 41832249
This may be even better, as it will unhide columns that were already hidden, but did not meet the criteria for being hidden.  The version you choose will depend on your exact need.

Dim rng As Range
Dim cel As Range

Set rng = Range(Range("L1"), Cells(1, Columns.Count).End(xlToLeft))
For Each cel In rng.Cells
    cel.EntireColumn.Hidden = (cel <> "" And cel = 0)
Next

Open in new window

0
 
LVL 93

Expert Comment

by:Patrick Matthews
ID: 41832252
I see our comments crossed paths :)
0
 
LVL 33

Author Closing Comment

by:Rob Henson
ID: 41832260
Excellent amendment, thank you!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question