Select failed?

D

davegb

This simple macro hides certain columns, then it's supposed to select
cell B3, but it tells me the select methods failed.

Sub HideColTop2AC()
Dim rTopCell As Range
Dim lColTop As Long
Dim WkSht As Worksheet
For Each WkSht In ActiveWorkbook.Worksheets
If Not Right(WkSht.Name, 7) = "Records" Then

Set rTopCell = WkSht.Range("3:3").find("top",
LookIn:=xlValues, LookAt:=xlPart)
If Not rTopCell Is Nothing Then
lColTop = rTopCell.Column

WkSht.Range(WkSht.Columns(lColTop),
WkSht.Columns("AC")).Hidden = True
WkSht.Range("B3").Select<---ERROR

End If
End If
Next
End Sub

Why is the select failing?
Thanks
 
B

Bob Phillips

The worksheet needs to be active

Sub HideColTop2AC()
Dim rTopCell As Range
Dim lColTop As Long
Dim WkSht As Worksheet
For Each WkSht In ActiveWorkbook.Worksheets
If Not Right(WkSht.Name, 7) = "Records" Then

Set rTopCell = WkSht.Range("3:3").Find("top", LookIn:=xlValues,
LookAt:=xlPart)
If Not rTopCell Is Nothing Then
lColTop = rTopCell.Column

WkSht.Range(WkSht.Columns(lColTop),
WkSht.Columns("AC")).Hidden = True
WkSht.Activate
WkSht.Range("B3").Select

End If
End If
Next
End Sub

--

HTH

RP
(remove nothere from the email address if mailing direct)
 
R

Ron de Bruin

Hi

The sheet is not active on that moment (can't select a cell in a sheet that is not active)
WkSht = is the sheet in the loop and that is not the activesheet

You can add a
WkSht.Select
line in the loop
 
T

Tom Ogilvy

The real answer is to remove that line unless you absolutely need B3 to be
selected. The code as written does not do any selecting except for that.
 
D

davegb

Thanks for pointing that out, Tom! Turns out, I didn't need it. But I
learned something from it, so it was worth finding out.
 

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

Similar Threads


Top