can't hide workbook or trap error

G

Guest

I'm trying to hide the workbook that contains my macro library. This used to
work, but has suddenly failed in 2 ways: the line "wb.Visible = False"
produces the error "Object doesn't support this property or method" and the
error is not trapped.

Sub auto_open()

Dim i&, wb

For i& = 1 To Workbooks.Count
Set wb = Workbooks(i&)
If InStr(UCase$(wb.Name), "PERSONAL.XLS") Then GoSub HideWB
If InStr(UCase$(wb.Name), "CUSTOM.XLS") Then GoSub HideWB
Next i&

Exit Sub

HideWB:

On Error GoTo SkipHide
wb.Visible = False
SkipHide:
On Error GoTo 0
Return

End Sub

What could be going wrong?
 
B

Bob Flanagan

Instead of wb.visible = false, you want to hide the window:

Windows(ThisWorkbook.Name).Visible = False

Bob Flanagan
Macro Systems
http://www.add-ins.com
Productivity add-ins and downloadable books on VB macros for Excel
 
G

Guest

Thanks, Bob.

Now I'm trying:

Windows(wb.Name).Visible = False

but I still get "Object doesn't support this property or method".

I confirmed that wb.Name = "PERSONAL.XLS".
 
D

Dave Peterson

This may get you over the hump:

Dim wkbk As Workbook
Dim myWindow As Window
Set wkbk = ActiveWorkbook
For Each myWindow In wkbk.Windows
myWindow.Visible = False
Next myWindow
 
G

Guest

Dave,

Ignore my last message.

After I first tried your code, I closed excel, reopened a workbook, and the
macro libraries (stored in XLS's in \XLSTART) were hidden as desired. Thanks!
 
D

Dave Peterson

You'd still want to cycle through all the workbooks:

Option Explicit
Sub auto_open()

Dim i As Long
Dim wb As Workbook
Dim myWindow As Window

For i = 1 To Workbooks.Count
Set wb = Workbooks(i&)
If InStr(1, wb.Name, "personal.xls", vbTextCompare) > 0 _
Or InStr(1, wb.Name, "custom.xls", vbTextCompare) > 0 Then
For Each myWindow In wb.Windows
myWindow.Visible = False
Next myWindow
End If
Next i

End Sub
 

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