ignore if workbookis already open

vac

Board Regular
Joined
May 21, 2002
Messages
211
Thanks to Nimrod, this is the code Im using.
Public Sub MainCopyProcedure()
Workbooks.Open Filename:="C:\Documents and Settings\David Culshaw\My Documents\master.xls"
On Error Resume Next

' the variables are : (SourceBk, SourceSht, TargBk, TargSht)
Call CopyRow3("Paul.xls", "dailyfigures", "Master.xls", "Paul")
On Error Resume Next
Call CopyRow3("Mark.xls", "dailyfigures", "Master.xls", "Mark")
On Error Resume Next
Call CopyRow3("John.xls", "dailyfigures", "Master.xls", "John")
On Error Resume Next


End Sub

Private Sub CopyRow3(SourceBk, SourceSht, TargBk, TargSht)
With Workbooks(TargBk).Sheets(TargSht)
NxRw = .Cells(65536, 1).End(xlUp).Row + 1
Workbooks(SourceBk).Sheets(SourceSht).Range("3:3").Copy _
Destination:=.Range(NxRw & ":" & NxRw)
End With
End Sub

the extra line I have added is to open the master sheet. Its the 3rd line down. What is the best way to go trying to open this workbook, without getting errors if it is already open. ie, if it is already open when the macro is run then ignore that request and carry one with the rest of the code.
Thanks.
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
Vac :
What's up with all the error traps ? I had provided you with a solution which only required one trap in the "CopyRow3" procedure , in my last posting to you ....

Public Sub MainCopyProcedure()
' the variables are : (SourceBk, SourceSht, TargBk, TargSht)
Call CopyRow3("Paul.xls", "dailyfigures ", "Master.xls", "Paul")
Call CopyRow3("Mark.xls", "dailyfigures ", "Master.xls", "Mark")
Call CopyRow3("John.xls", "dailyfigures ", "Master.xls", "John")

End Sub

Private Sub CopyRow3(SourceBk, SourceSht, TargBk, TargSht)
On Error GoTo NotFound
With Workbooks(TargBk).Sheets(TargSht)
NxRw = .Cells(65536, 1).End(xlUp).Row + 1
Workbooks(SourceBk).Sheets(SourceSht).Range("3:3").Copy _
Destination:=.Range(NxRw & ":" & NxRw)
End With
NotFound:
End Sub
 
Upvote 0
PLEASE NOTE: That all your Error Traps have been removed and replaced with the one I had suggested in my previous post.

Public Sub MainCopyProcedure()
If Not fileIsOpen("Master.xls") Then
Workbooks.Open fileName:="C:\Documents and Settings\David Culshaw\My Documents\master.xls"
End If
' the variables are : (SourceBk, SourceSht, TargBk, TargSht)
Call CopyRow3("Paul.xls", "dailyfigures ", "Master.xls", "Paul")
Call CopyRow3("Mark.xls", "dailyfigures ", "Master.xls", "Mark")
Call CopyRow3("John.xls", "dailyfigures ", "Master.xls", "John")

End Sub

Private Sub CopyRow3(SourceBk, SourceSht, TargBk, TargSht)
On Error GoTo NotFound
With Workbooks(TargBk).Sheets(TargSht)
NxRw = .Cells(65536, 1).End(xlUp).Row + 1
Workbooks(SourceBk).Sheets(SourceSht).Range("3:3").Copy _
Destination:=.Range(NxRw & ":" & NxRw)
End With
NotFound:
End Sub



Function fileIsOpen(fileName As String) As Boolean
Dim wrkBook As Workbook
For Each wrkBook In Workbooks
If UCase(wrkBook.Name) = UCase(fileName) Then
fileIsOpen = True
Exit Function
End If
Next wrkBook
fileIsOpen = False
End Function
 
Upvote 0
This version not only checks to see if file exist it also gives alert if can't be found in the expected path.

Public Sub MainCopyProcedure()
On Error GoTo NoMaster
If Not fileIsOpen("Master.xls") Then
Workbooks.Open fileName:="C:\Documents and Settings\David Culshaw\My Documents\master.xls"
End If
' the variables are : (SourceBk, SourceSht, TargBk, TargSht)
Call CopyRow3("Paul.xls", "dailyfigures ", "Master.xls", "Paul")
Call CopyRow3("Mark.xls", "dailyfigures ", "Master.xls", "Mark")
Call CopyRow3("John.xls", "dailyfigures ", "Master.xls", "John")


Exit Sub
NoMaster:
pt = MsgBox("C:\Documents and Settings\David Culshaw\My Documents\master.xls", vbCritical, "No Master File Found:")

End Sub

Private Sub CopyRow3(SourceBk, SourceSht, TargBk, TargSht)
On Error GoTo NotFound
With Workbooks(TargBk).Sheets(TargSht)
NxRw = .Cells(65536, 1).End(xlUp).Row + 1
Workbooks(SourceBk).Sheets(SourceSht).Range("3:3").Copy _
Destination:=.Range(NxRw & ":" & NxRw)
End With
NotFound:
End Sub



Function fileIsOpen(fileName As String) As Boolean
Dim wrkBook As Workbook
For Each wrkBook In Workbooks
If UCase(wrkBook.Name) = UCase(fileName) Then
fileIsOpen = True
Exit Function
End If
Next wrkBook
End Function
 
Upvote 0
That is great Nimrod, Thank You very much. I hadnt put in you error code yet, it still had a line in that I was trying to use. Thanks very much for your quick responce and all your help, vac
 
Upvote 0
That works great, Thanks. What I need to add is that when this procedure is run the source sheet, whichever one it is, is closed. It wont matter if the sheet is saved.
I was trying to use a code similar to this:
ActiveWorkbook.Close False
after each of the source sheets, but it dosnt work if not all the source sheets are open.
Thanks
 
Upvote 0
just insert to your sub copyrow3 below notfound: :

workbooks(SourceBk).close false


In addition, don't put exteionsion .xls when you call copyrow3("yourfile.xls"), it will produce an error message, just yourfile
name will do.
 
Upvote 0
Thanks very much for the reply, unfortunately i cant get the code to work with that ne line in. I have removed the xls extensions, but with the new line in it comes up error message box, cant find master file, even though its there and open. If I remove the new line evreything works ok.
Thanks
 
Upvote 0
Ok thanks to all who helped. By adding this line
On error resume next
Ive got it to work ok.
Thank You
 
Upvote 0

Forum statistics

Threads
1,215,634
Messages
6,125,934
Members
449,275
Latest member
jacob_mcbride

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