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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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,223,888
Messages
6,175,217
Members
452,619
Latest member
Shiv1198

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