[Excel] Opening other workbooks
Hi,
I'm fairly new to VBA, but have used other languages before, and I'm trying to open a second workbook and copy data across from it.
To try and teach myself this, I tried to just open a second workbook and write to it but it's not working. I also tried to add some message prompts to see how far it's getting, but they are not appearing as well. (Though the second work book does open on the screen)
My code is:
Code:
Sub Copy()
FileToOpen = Application.GetOpenFilename _
(Title:="Please choose a file to import", _
FileFilter:="Excel Files *.xls (*.xls),")
Set wbk = Workbooks.Open(FileToOpen)
MsgBox "Test2"
wbk.Sheets("Sheet1").Range("E16").Value = "Bob"
MsgBox "Test"
End Sub
Thanks for any help,
Paul
Re: [Excel] Opening other workbooks
Try this to get the file name:
Code:
filetoopen = Application.GetOpenFilename _
("Excel Files (*.xls), *.xls", _
, "Please choose a file to import!")
Re: [Excel] Opening other workbooks
Welcome to VBForums :wave:
What's not working with your code?
You are using, "wbk.Sheets("Sheet1")", that assumes that there is a sheet with this exact name.
If not it will raise an error. You could use "wbk.Sheets(1)" , which reflect the first sheet without knowing its name (could be different in other languages!).
At the end of the code you should close the workbook (wbk.close).
Re: [Excel] Opening other workbooks
Thanks for your help.
Changing the open command to Workbooks.Open(filetoopen, True, True) seems to have sorted it.
Thanks,
Paul
Re: [Excel] Opening other workbooks
the second true is to open readonly