I finally stumbled across a function online that supposedly checks to see if a file by some name is open.
Function CheckFile(strFile As String) As Boolean
Dim Wbk As Workbook
CheckFile = False
On Error GoTo NotOpen
Set Wbk = Workbooks(strFile)
CheckFile = True
Exit Function
NotOpen:
End Function
This seems to work, at least as far as I have been able to test it so far. However, I am running into problems when trying to create the file if it doesn't exist. Here's my code:
' If this worksheet doesn't exist, create it
If Not (CheckFile("List.xls")) Then
Workbooks.Add ("List.xls")
Set listbook = Workbooks("List.xls")
' Create headings
Set list = listbook.Sheets(1)
list.Cells(1, 1).Value = "Part No."
list.Cells(1, 2).Value = "Description"
list.Cells(1, 3).Value = "Source Vendor"
list.Cells(1, 4).Value = "Sales"
list.Cells(1, 5).Value = "No. Orders"
list.Cells(1, 6).Value = "B02 Status"
list.Cells(1, 7).Value = "B02 Stock"
list.Cells(1, 8).Value = "No. Competing Vendors"
' Otherwise, define the first sheet as the target
Else
Set listbook = Workbooks("List.xls")
Set list = listbook.Sheets(1)
End If
However, I get an error on the Workbooks.Add("List.xls") line saying that the file counld not be found. I am confused - I thought this command was supposed to create a new file. Why then would I be getting this error, and how can I get around it?
Bookmarks