VBA workbook open debug

csilabgirl

Active Member
Joined
Aug 14, 2009
Messages
359
Excel 2002

I have some VBA code in a workbook (workbook #1) that runs when the workbook is opened. The code subsequently opens another workbook (workbook #2) and then copies data from it and pastes it into workbook #1. The problem is sometimes the user already has workbook #2 open when they open workbook #1, so they end up getting a debug error. Is there some different code to work around this? Below is my code. Thanks for the help.

Private Sub Workbook_Open()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p> </o:p>
Dim wbOpen As Workbook<o:p></o:p>
<o:p></o:p>
Set wbOpen = Workbooks.Open(Filename:="\\Server\Chromosomal Labs\ChromoLIMS\Client List.xls")<o:p></o:p>
<o:p></o:p>
wbOpen.Sheets("Clients").Columns("D:L").Copy<o:p></o:p>
ThisWorkbook.Sheets("Clients").Columns("D:L").PasteSpecial Paste:=xlPasteValues
<o:p> </o:p>
wbOpen.Close<o:p></o:p>
Sheets("Intro").Select
End Sub<o:p></o:p>
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
Try like this

Code:
Function WorksheetExists(WSName As String) As Boolean
On Error Resume Next
WorksheetExists = Worksheets(WSName).Name = WSName
On Error GoTo 0
End Function

Sub test()
If Not WorksheetExists("Sheet1") Then
        MsgBox "Unable to proceed", vbExclamation, "Error"
        Exit Sub
End If
 
Upvote 0
VoG,

Thank you for the code, but to clarify, where am I putting this new code in relation to my workbook_open code
 
Upvote 0
Hi. Scrub my earlier suggestion (that was checking for existence of a sheet not a workbook. Try like this

ThisWorkbook module

Code:
Private Sub Workbook_Open()

Dim wbOpen As Workbook

If IsOpen("Client List.xls") Then
    Set wbOpen = Workbooks("Client List.xls")
Else
    Set wbOpen = Workbooks.Open(Filename:="\\Server\Chromosomal Labs\ChromoLIMS\Client List.xls")
End If
wbOpen.Sheets("Clients").Columns("D:L").Copy
ThisWorkbook.Sheets("Clients").Columns("D:L").PasteSpecial Paste:=xlPasteValues

wbOpen.Close
Sheets("Intro").Select
End Sub

Regular module

Code:
Function IsOpen(anyName As String) As Boolean
Dim wb As Workbook
On Error GoTo noFilesOpen
For Each wb In Workbooks
    If LCase(wb.Name) = LCase(anyName) Then
        IsOpen = True
        Exit Function
    End If
Next wb
Exit Function
noFilesOpen:
IsOpen = False
End Function
 
Upvote 0
VoG,

I used the This Workbook code and when I open the workbook (that this code is in) I get a "compile error, sub or function not defined" and it highlights this portion of the code (see in red). When I opened my workbook the "client list" excel was not open already.

Private Sub Workbook_Open()
Application.ScreenUpdating = False
Dim wbOpen As Workbook
If IsOpen("Client List.xls") Then
Set wbOpen = Workbooks("Client List.xls")

Thank you
 
Upvote 0
Try (regular module)

Rich (BB code):
Public Function IsOpen(anyName As String) As Boolean
Dim wb As Workbook
On Error GoTo noFilesOpen
For Each wb In Workbooks
    If LCase(wb.Name) = LCase(anyName) Then
        IsOpen = True
        Exit Function
    End If
Next wb
Exit Function
noFilesOpen:
IsOpen = False
End Function
 
Upvote 0
Vog,

That works but there is one problem. If the user already has client list open, it closes it after the workbook_open code runs. If the user already has the client list open they would not want it to be closed on them as they are probably actively working in it. I generally only want it to close client list, if it was not already open to begin with, but I guess that is a whole new set of code.
 
Upvote 0
Try this

Code:
Private Sub Workbook_Open()

Dim wbOpen As Workbook
Dim WasOpen As Boolean

If IsOpen("Client List.xls") Then
    Set wbOpen = Workbooks("Client List.xls")
    WasOpen = True
Else
    Set wbOpen = Workbooks.Open(Filename:="\\Server\Chromosomal Labs\ChromoLIMS\Client List.xls")
End If
wbOpen.Sheets("Clients").Columns("D:L").Copy
ThisWorkbook.Sheets("Clients").Columns("D:L").PasteSpecial Paste:=xlPasteValues

If Not WasOpen Then wbOpen.Close
Sheets("Intro").Select
End Sub
 
Upvote 0
Something like this might do the trick


Code:
Private Sub Workbook_Open()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
<o:p></o:p>
Dim wbOpen As Workbook<o:p></o:p>
<o:p>On Error Resume Next</o:p>
Windows("Name of the 2nd workbook.xls").Close
Set wbOpen = Workbooks.Open(Filename:="\\Server\Chromosomal Labs\ChromoLIMS\Client List.xls")<o:p></o:p>
<o:p></o:p>
wbOpen.Sheets("Clients").Columns("D:L").Copy<o:p></o:p>
ThisWorkbook.Sheets("Clients").Columns("D:L").PasteSpecial Paste:=xlPasteValues
<o:p></o:p>
wbOpen.Close<o:p></o:p>
Sheets("Intro").Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,730
Members
452,939
Latest member
WCrawford

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top