mardi 7 août 2018

Check if file is in Readonly state (If Statement) VBA

All, I have the below code which I require to check for the first available file which is not in a read only state and use the file name as part of a larger module.

For example if Transactions1.csv is in use by another user, then check if Transactions2 is in use etc.

The issue I am having is it always seems to be using Transactions3.csv and ignoring files 1,2 & 4. (Even if they are not in a read only state). Any help would be much appreciated.

Sub CheckIFFileisopen()

'checking multiple files

PMFTransFile = "\\Csdatg04\psproject\Robot\Project Preload\Transactions\Transactions1.csv"
Set TransworkBook = Workbooks.Open(PMFTransFile)
'Check to see if file is already open

If TransworkBook.ReadOnly Then
    ActiveWorkbook.Close
'check if 2nd file is available
PMFTransFile = "\\Csdatg04\psproject\Robot\Project Preload\Transactions\Transactions2.csv"
Set TransworkBook = Workbooks.Open(PMFTransFile)
        If TransworkBook.ReadOnly Then
        ActiveWorkbook.Close

'check if 3rd file is available
 PMFTransFile = "\\Csdatg04\psproject\Robot\Project Preload\Transactions\Transactions3.csv"
 Set TransworkBook = Workbooks.Open(PMFTransFile)

        If TransworkBook.ReadOnly Then
        ActiveWorkbook.Close

 'check if 4th file is available
 PMFTransFile = "\\Csdatg04\psproject\Robot\Project Preload\Transactions\Transactions4.csv"
 Set TransworkBook = Workbooks.Open(PMFTransFile)

     MsgBox "Cannot update Transactions, someone currently using file.  Please try again in a few minutes."
     Application.ScreenUpdating = True
     Application.Calculation = xlCalculationAutomatic
     End
     Exit Sub

   End If
   End If
   End If
 End Sub

Aucun commentaire:

Enregistrer un commentaire