Reopen Error in Excel Macro

petes

Board Regular
Joined
Sep 12, 2009
Messages
168
Hi Friends!!

I have this code which copy's the data from "Name.xlsx" to "Master.xslx"
I run this macro from Master.xslx file...

Sub mast()

If Dir("C:\test\Name.xlsx") <> "" Then
Workbooks.Open "C:\test\Name.xlsx"
Worksheets("Emp").Select
ActiveSheet.Cells.Select
Selection.Copy
Windows("Master").Activate
Worksheets("Sheet1").Select
Range("A1").Select
Selection.PasteSpecial
Else
MsgBox "File doesn't exist."
End If
End Sub

I need to alter this code so that, in case if the file "Name.xlsx" is already open, it should automatically save and close...

But right now, the error is dsipalying as

----Name.xlsx is already open. Reopening will cause any changes you made to be discarded . Do you want to reopen Name.xlsx------

Basically i am trying to get rid off this error by automatically saving the opened file before it gets closed.

Thanks Pals!!!
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Try

Code:
Sub mast()

If Dir("C:\test\Name.xlsx") <> "" Then
If Not BookOpen("Name.xlsx") Then Workbooks.Open "C:\test\Name.xlsx"
Worksheets("Emp").Select
ActiveSheet.Cells.Select
Selection.Copy
Windows("Master").Activate
Worksheets("Sheet1").Select
Range("A1").Select
Selection.PasteSpecial
Else
MsgBox "File doesn't exist."
End If
End Sub
Function BookOpen(wbName As String) As Boolean
On Error Resume Next
BookOpen = Len(Workbooks(wbName).Name)
End Function
 
Upvote 0
Thanks for the reply... This revised code is working fine except with one error.. If i am executing this Macro from "Master.xlsx" file, i receive the following error

---Runtime error 9, subscript out of range---

I believe this is something to do with the "Windows("Master").Activate" line....

Appreciate your help regarding this....!!
 
Upvote 0
humm...Still the error appears. Now the error is "Subscript out of range"..

not sure what to do...??
 
Upvote 0
Are you saying that your original code didn't error on that line? When you get the error click Debug and check which line of code is highlighted.
 
Upvote 0
If i run in debug mode...(pressing F8) the error is coming after this line
Worksheets("Emp").Select
That means, this line gets highlited and after that if i press F8 the error pops up "Subscrpit out of range"
 
Upvote 0
That means that you don't have a worksheet called "emp" - maybe it is " emp" or "emp ".
 
Upvote 0

Forum statistics

Threads
1,224,516
Messages
6,179,231
Members
452,898
Latest member
Capolavoro009

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top