-3

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.

Luuklag
  • 3,897
  • 11
  • 38
  • 57
agustin
  • 1,311
  • 20
  • 42
  • So you want to open the file to validate it before you open the file? – psubsee2003 Feb 16 '14 at 18:03
  • `in the first sheet` on the first sheet or this sheet has some specific name? – Dmitry Pavliv Feb 16 '14 at 18:05
  • Is there any reason you don't want to open the file first, validate it, and then decide whether to continue executing? – psubsee2003 Feb 16 '14 at 18:05
  • @psubsee2003 not necessarily. Right now I open the file. I just want to check if a condition is meeted with the just opened file before continuing with the macro (export the sheets into the file where the macro is executed). If the user have chosen a wrong file, then it does not make sense continuing with the macro itself and therefore I should stop the macro and advise the user. – agustin Feb 16 '14 at 18:09

1 Answers1

2

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
psubsee2003
  • 8,563
  • 8
  • 61
  • 79