Question

Is there an easy way to check if a file, opened using Application.GetOpenFilename, has the expected structure? For example, the value of A1 in the first sheet have to be "Archive".

If the file is not as expected, then the VBA script should not continue doing its work.

Was it helpful?

Solution

The easiest method would be to validate the file immediately after opening.

Once done, you can check first and if it is the wrong file, you can close it and stop executing:

MyFilePath = Application.GetOpenFilename()

Set MyWorkbook = Workbooks.Open(MyFilePath)

If MyWorkbook.Worksheets(1).Range("A1") <> "Archive" Then
    MyWorkbook.Close
    Exit Sub
End If
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top