How do I amend a macro to check if read only

ghrek

Active Member
Joined
Jul 29, 2005
Messages
427
Hi

I have the following macro but im trying to get it to notify me if its read only when opening.

How do I do this and can it be tidied up?


Declare the variables

Dim MyPath As String

Dim MyFile As String

Dim LatestFile As String

Dim LMD As Date



'Specify the path to the folder

MyPath = "T:\Passenger Accounts\SHARED\passacc\Excel\passacc\Tableau Data"

'Make sure that the path ends in a backslash

If Right(MyPath, 1) <> "" Then MyPath = MyPath & ""


'Get the first Excel file from the folder

MyFile = Dir(MyPath & "*.xlsx", vbNormal)



'If no files were found, exit the sub

If Len(MyFile) = 0 Then

MsgBox "No files were found...", vbExclamation

Exit Sub

End If



LatestFile = ""



'Loop through each Excel file in the folder

Do While Len(MyFile) > 0



If MyFile > LatestFile Then

LatestFile = MyFile

End If



'Get the next Excel file from the folder

MyFile = Dir



Loop



'Open the latest file

Workbooks.Open MyPath & LatestFile



End Sub
 
Last edited:

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try:
Code:
Sub Macro1095804()

Dim MyPath$, MyString$
Dim MyFile$, LatestFile$
Dim wb As Workbook


'Specify the path to the folder
MyPath = "T:\Passenger Accounts\SHARED\passacc\Excel\passacc\Tableau Data"
'Get the first Excel file from the folder
MyFile = Dir(MyPath & "*.xlsx", vbNormal)
'If no files were found, exit the sub
If Len(MyFile) = 0 Then
    MsgBox "No files were found...", vbExclamation
    Exit Sub
End If


LatestFile = ""


'Loop through each Excel file in the folder
Do While Len(MyFile) > 0
    If MyFile > LatestFile Then
        LatestFile = MyFile
    End If
    'Get the next Excel file from the folder
    MyFile = Dir
Loop


'Open the latest file
Set wb = Workbooks.Open(MyPath & LatestFile)
If wb.ReadOnly = True Then MsgBox LatestFile & " is opened as Read Only.", vbExclamation
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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