troubleshooting Question

Determine Range to Select

Avatar of Rob Henson
Rob HensonFlag for United Kingdom of Great Britain and Northern Ireland asked on
Microsoft ExcelVisual Basic Classic
5 Comments1 Solution158 ViewsLast Modified:
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
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
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros