Macro to open and close an Excel file

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I would like to create a macro that would open an Excel file based on file
path that is indicated in a cell, and then close it. For instance, open the
file for which the file path is indicated in cell A1, and then close it.

Any advice you could provide on this would be greatly appreciated.

Magnivy
 
workbooks.open Range("A1").Value
...
activeworkbook.close

--
HTH

Bob Phillips

(remove nothere from email address if mailing direct)
 
Hi, try this:

Sub File_Open()
file=Range("A1")
Workbooks.Open Filename:= file
ActiveWindow.Close
End Sub

„Magnivy" napísal (napísala):
 
Try this...

Sub OpenClose()
Dim wbk As Workbook

On Error Resume Next
Set wbk = Workbooks.Open(Sheets("Sheet1").Range("A1"))
On Error GoTo 0

If wbk Is Nothing Then Exit Sub

MsgBox wbk.Name & " is open."
wbk.Close

MsgBox "And now it is closed."
Set wbk = Nothing
End Sub
 
dim openwb as workbook
workbooks.open filename:=Range("A1")
set openwb=activeworkbook
openwb.close
 
Many thanks to everyone for your help!! kwiklearner's code seems to work best
for me.

Magnivy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top