If Then to check if a workbook is open

S

Shawn

I have a workbook names "Space.xls" and when a certain macro is run it goes
and pulls date from a workbook named "Moon.txt". This all works fine.

However, I need a code that will look to see if "Moon.txt" is open and if it
is not to skip that part of the code.
 
D

Don Guillett

This code will open if closed or activate if open. Works from a workbook
name typed in a cell.

Sub GetWorkbook()
If ActiveCell.Value = "" Then Exit Sub
workbookname = ActiveCell.Value
On Error GoTo OpenWorkbook
Windows(workbookname & ".xls").Activate
Exit Sub
OpenWorkbook:
Workbooks.Open(workbookname & ".xls").RunAutoMacros xlAutoOpen
End Sub
 
S

Shawn

That isn't exactly what I need. When the macro is running it gets to a line
that says:

Windows("Moon.txt").Activate

I need a If Then statement something like:

If Window("Moon.txt") Open Then
Window("Moon.txt").Activate
etc. etc. etc.
End If

--
Thanks
Shawn


Don Guillett said:
This code will open if closed or activate if open. Works from a workbook
name typed in a cell.

Sub GetWorkbook()
If ActiveCell.Value = "" Then Exit Sub
workbookname = ActiveCell.Value
On Error GoTo OpenWorkbook
Windows(workbookname & ".xls").Activate
Exit Sub
OpenWorkbook:
Workbooks.Open(workbookname & ".xls").RunAutoMacros xlAutoOpen
End Sub
 
D

Don Guillett

Did you try deleting the first line and changing the 2nd line

workbookname = ActiveCell.Value
workbookname = "moon"

and changing .xls to .txt

--
Don Guillett
SalesAid Software
[email protected]
Shawn said:
That isn't exactly what I need. When the macro is running it gets to a
line
that says:

Windows("Moon.txt").Activate

I need a If Then statement something like:

If Window("Moon.txt") Open Then
Window("Moon.txt").Activate
etc. etc. etc.
End If
 
B

Bob Phillips

On Error Resume Next
Set oWB = Workbooks("Moon.txt")
On Error Goto 0
If Not oWB Is Nothing Then
'do the stuff
End If

--

HTH

Bob Phillips

(replace xxxx in the email address with gmail if mailing direct)
 
Top