Loading

Friday, February 13, 2009

Excel VBA Macro Example: Opening & Closing a Workbook (Excel File)

When we're working on something using macro in excel, sometimes we need the data in a different excel file (workbook). Rather than open the file (workbook) manually, we can open excel file by using Excel VBA Macro. Following example will show you how to open & close the excel file using Excel VBA Macro.

Sub OpenCopyAndClose()
Dim excelFile As String

excelFile = "myfile.xls"

'open excel file "myfile.xls" in drive D:\
Workbooks.Open "D:\" & excelFile

'copy value of range A1 from "myfile.xls"
ThisWorkbook.Sheets(1).Range("D1").Value = _
Workbooks(excelFile).Sheets(1).Range("A1").Value

'close "myfile.xls" file and don't save any changes
Workbooks(excelFile).Close SaveChanges:=False
End Sub

SHARE TWEET

Thank you for reading this article Excel VBA Macro Example: Opening & Closing a Workbook (Excel File) With URL https://x-tutorials.blogspot.com/2009/02/excel-vba-macro-example-opening-closing.html. Also a time to read the other articles.

0 comments:

Write your comment for this article Excel VBA Macro Example: Opening & Closing a Workbook (Excel File) above!