opening external file to copy/move sheet to/from - vba

lezawang

Well-known Member
Joined
Mar 27, 2016
Messages
1,805
Office Version
  1. 2016
Platform
  1. Windows
Hi
I want to open an closed excel file (abc.xlsx) and then move/copy sheet from the current workbook to that file (abc.xlsx). With your help, I learnt how to do that. But I just found that, when I open the file using open() function, excel will consider that file as workbooks(2). If that is the case, then why I need this line of code below? why I need to set it to X? I can work on it using workbooks(2) object

Code:
[INDENT]' why I need to assign it to x
set x = application.workbooks.open("c:\abc.xlsx")[/INDENT]


Code:
Sub abc()
    Dim x As Workbook
    Dim y As String
    y = InputBox("enter file name")
    Set x = Application.Workbooks.Open(y)
    workbooks(1).sheets(1).copy before:= x.sheets(1)
    'i can use workbooks(2) instead of x
    Workbooks(2).Close
End Sub
 
Last edited:

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Try opening two workbooks & then run your code.
 
Upvote 0
Thank you. I opened 2 files beside the one I am working on. So I opened abc.xlsx first and then xyz.xlsx. When I said workbooks(2).close, excel closed abc.xlsx. So everything was normal? Still do not see why I need to do x.close instead of workbooks(2).close. Thanks

Code:
Sub abc()
    Dim x As Workbook
    Dim n As Workbook
    Dim y As String
    y = InputBox("enter file name")
    Set x = Application.Workbooks.Open(y)
    y = InputBox("enter file name")
    Set n = Application.Workbooks.Open(y)
    Workbooks(2).Close
End Sub
 
Upvote 0
Have 2 workbooks open & then run your original code, that way you will then have three workbooks open.
 
Upvote 0
Same result! my main file A.xlsx is open already, then opened B.xlsx (by clicking on it) then opened C.xlsx (by clicking). Now I have 3 files opened. I ran the code then B.xlsx got closed. Thank you.
 
Upvote 0
Which file was opened by the code?
 
Upvote 0
none. i opened them manually first the run the code as per your instructions.
 
Upvote 0
If you open 2 workbooks manually then run the code from post#1, which workbook closes?
 
Upvote 0
I am working on A, then opened B and C and then ran the code from post#1. The code asked me to open a file, I said open B again and after that excel closed this time C and kept A and B. This time different answer!

So the whole idea of assigning name to an open file (open workbook) is to help knowing the file (in this case called X) without worring about how many workbooks are opened and which workbooks(#) will be assigned to the opened one.

So When I say

set x = app.wkbs.open("file name")

I know the opened file is call X and i can keep working on that. Is that the main idea? thank you so much.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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