Looping Problem . . .Argh!

E

ed

I am using the following code to cycle through open sheets and hide those
with nothing in Cell A2. When I run the code, it never changes sheets, it
just applies the logic to the same sheet.

Dim SH As Worksheet
Dim xlRng As Object 'Excel.Range
Dim xlApp As Excel.Application

For Each SH In xlApp.ActiveWorkbook.Worksheets
Set xlRng = xlApp.Range("A2")
If xlRng = "" Then xlApp.ActiveWindow.SelectedSheets.Visible = False
Next SH

Any Ideas
 
D

Don Guillett

try this
Sub hideshts()
For Each ws In Worksheets
If ws.Range("a2") = "" Then ws.Visible = False
Next
End Sub
 
E

e

I tried your code but it still will not move on to the next "For" command. It
still tries to apply the changes to the one sheet.
 
M

Michael Malinsky

My code, which is similar to Don's works fine unless you are trying to hide
the only visible sheet in the workbook (you cannot hide all sheets in a
workbook).

Sub HideSheets()

For Each ws In Worksheets
If ws.Range("A2") = "" Then
ws.Visible = False
End If
Next ws

End Sub
 
A

as

I found the problem. For some reason If I used the IF/Then on the same line
without an End if it screwed up. Once I went with the longer form, all worked.

Thanks for your help.
 
D

Don Guillett

shouldn't. You can use if dddd then eeeeee without endif on SAME line or
even with a _ continuation char. Else use

if ddd then
eee
end if
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top