Determine Range to Select

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.
    Selection.EntireColumn.Hidden = False
    For Each Cell In Selection
        Count = Count + 1
    Next Cell
    For Ch = 1 To Count
        If ActiveCell.Value <> "" And ActiveCell.Value = 0 Then
        Selection.EntireColumn.Hidden = True
        End If
        ActiveCell.Offset(0, 1).Range("$A$1").Select
    Next Ch

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.
Rob H
LVL 37
Rob HensonFinance AnalystAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Patrick MatthewsCommented:
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

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.
Rob HensonFinance AnalystAuthor Commented:
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.
Patrick MatthewsCommented:
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)

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Patrick MatthewsCommented:
I see our comments crossed paths :)
Rob HensonFinance AnalystAuthor Commented:
Excellent amendment, thank you!!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.