Add check if files are open and alert user

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
793
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hello - I have VBA code what will currently check if a file exists and if it does my code will proceed if not it will give a dialog box.

Looking to add 2 more similar checks and looking to see if possible if I already have the above, the three extra are:

1) check if file A is open (one above as well) - dialog box to warn file is open
2) check if file B is open - dialog box to warn file is open
VBA Code:
Sub Run()
    Dim sPath As String, sPartial As String, sFName As String
    Dim rws As Long
   
Application.ScreenUpdating = False
 
'open file using Partial file naming
    sPath = "....\"      ' <<<<< change accordingly
 
    sPartial = "_US_D_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.csv"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks.OpenText sPath & sFName
   
MY CODE


Workbooks(sFName).Close SaveChanges:=False


    Else
        MsgBox "File not found.", vbExclamation
    End If

End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
VBA Code:
For Each wb In Application.Workbooks
    MsgBox "Workbook " & wb.Name & " Is open."
Next
 
Upvote 0
Rich (BB code):
Sub Run()
    Dim sPath As String, sPartial As String, sFName As String
    Dim rws As Long, wb As Workbook  
        For Each wb In Application.Workbooks
            If wb.name <> ThisWorkbook.Name Then
                MsgBox "Workbook " & wb.Name & " Is open."
            End If
       Next
Application.ScreenUpdating = False
'open file using Partial file naming
    sPath = "....\"      ' <<<<< change accordingly
    sPartial = "_US_D_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.csv"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks.OpenText sPath & sFName  
MY CODE
Workbooks(sFName).Close SaveChanges:=False
    Else
        MsgBox "File not found.", vbExclamation
    End If
End Sub
 
Upvote 0
Rich (BB code):
Sub Run()
    Dim sPath As String, sPartial As String, sFName As String
    Dim rws As Long, wb As Workbook 
        For Each wb In Application.Workbooks
            If wb.name <> ThisWorkbook.Name Then
                MsgBox "Workbook " & wb.Name & " Is open."
            End If
       Next
Application.ScreenUpdating = False
'open file using Partial file naming
    sPath = "....\"      ' <<<<< change accordingly
    sPartial = "_US_D_" & Year(Now) & IIf(Len(Month(Now)) = 1, "0" & Month(Now), Month(Now)) & IIf(Len(Day(Now)) = 1, "0" & Day(Now), Day(Now)) & "*.csv"
    sFName = Dir(sPath & sPartial)
    If Len(sFName) > 0 Then
        Workbooks.OpenText sPath & sFName 
MY CODE
Workbooks(sFName).Close SaveChanges:=False
    Else
        MsgBox "File not found.", vbExclamation
    End If
End Sub
But this will tell me all open workbooks i have open. not if someone else is in a file that the vba code needs in the process. for example i have a file open not related to my code and it is saying i have it open
 
Upvote 0
I misunderstood what you were asking. You were apparently referring to shared files being open. See if this link helps.
so not a way to do it multiple times but nevertheless a way. I will just create two subs per the attached and call on them in my code
 
Upvote 0

Forum statistics

Threads
1,215,706
Messages
6,126,334
Members
449,309
Latest member
kevinsucher

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