0

I'm trying to use the code block:

Sub OpenPulledReports(ByRef filepath)

MsgBox (Dir(filepath))

If Dir(filepath) <> "" Then
    Application.Workbooks.Open (filepath)
End If

End Sub

to check if a file is open already, and open it if not. When this Sub runs, I can't debug through it, but rather, it just takes off to End Sub.

That's not my problem, though. When it runs, a file explorer window opens with the title "Update Values: filename" (as in, the filename which the filepath reaches). This is really slowing things down. Also, I'm pretty sure my Dir(filepath) isn't doing what I'm expecting.

Thanks for the help.

Tawm
  • 535
  • 3
  • 12
  • 25
  • What do you mean by "can't debug it"? Keep in mind that as the sub requires a parameter, you can't simply click inside the code and hit f8 - you need to debug through the sub which passes the parameter. In the VBA window, click the edge to the left of the line 'MsgBox...'. This will create a red circle, which means when your code reaches that point, it will stop running, and open the debug window. Then you can step through it 1 line at a time by pressing F8. Try that - is your code even reaching that stage, or does it actually break before that point? – Grade 'Eh' Bacon Jul 09 '15 at 17:22
  • @Grade'Eh'Bacon thanks for the reply. I didn't know about the parameter requirement bit, but that makes sense. What I mean is that I will Step Into the `Application.Workbooks.Open (filepath)` and it won't let me see what happens, it'll sorta freeze up as it processes opening the file and then ends the sub. – Tawm Jul 09 '15 at 17:33
  • Well that's the first problem for you to address. VBA code runs line by line, and if it stops before it gets to the line you're looking at, you won't get a correct answer on that line. Could you provide the sub that opens under the Workbooks.Open event? – Grade 'Eh' Bacon Jul 09 '15 at 17:35
  • Can you add an example of the value of the `filepath` parameter? – ChipsLetten Jul 09 '15 at 17:45
  • You may want to use [THIS](http://stackoverflow.com/questions/9373082/detect-whether-excel-workbook-is-already-open-using-vba/9373914#9373914) to check if the file is open or not and then try to open it? – Siddharth Rout Jul 09 '15 at 17:49
  • Checking to see if the file is open, and checking to see if the file exists are two different things. – Rdster Jul 09 '15 at 19:59
  • @Rdster yeah sorry, I'm trying to see if it's open. I know it exists and I know the path and file name. – Tawm Jul 10 '15 at 14:53

0 Answers0