Start a Macro in an active workbook that will open a different specific workbook and copy its specific sheet to the originally active workbook?

excelos

Well-known Member
Joined
Sep 25, 2011
Messages
591
Office Version
  1. 365
Platform
  1. Windows
hello!

Start a Macro in an active workbook that will open a different specific workbook and copy its specific sheet to the originally active workbook?
and then close the different specific workbook?
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Code:
Sub r()
Dim wb As Workbook
Set wb = Workbooks.Open(Workbooks("SpecificWorkbook.xlsx")
wb.Sheets("Specific").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
wb.Close
End Sub

This code will need modification to substitute workbook and sheet names. It also might require adding in a path to open the second workbook if that workbook is in a different directory than the host workbook.
 
Last edited:
Upvote 0
Thanks!


Can you explain a bit please:
I assume wb is a variable.
Which of the rest words like Workbook, Workbooks, ThisWorkbook are builtin variables and not user-defined variables?
Can you go through each word please to explain?


For example:
Dim wb as Workbook : I understand that you set a variable named wb to be Workbook, so is Workbook a built in name for the type/class of variable for workbooks?
Then you set wb to open SpecificWorkbook but what does the Workbooks word mentioned twice mean?
Then you copy the Specific sheet from the workbook wb, into ThisWorkbook. What is the ThisWorkbook? is it builtin variable for the active workbook?

This will help me very much,
Thanks!
 
Upvote 0
How many workbooks will be open when this code is executed?

When you say 'active' workbook do you mean the workbook the code is in or another workbook?

Where would the filename and path for the 'other specific' workbook come from?

Which worksheet would you want to copy from that workbook?
 
Last edited:
Upvote 0
There will be only one workbook opened, but apparently the macro will have to open the TEMPLATEWORKBOOK to copy the TEMPLATEWORKSHEET to the ACTIVEWORKBOOK

The ACTIVEWORKBOOK is any workbook that I may open and when I click the macro, the TEMPLATEWORKSHEET should be copied to that.

I suppose by 'other specific' you mean the TEMPLATEWORKBOOK? It will be saved in the standard templates path and the filename will be TEMPLATEWORKBOOK.

I want to copy a standard named worksheet from the TEMPLATEWORKBOOK, named TEMPLATEWORKSHEET.

But please answer my questions, I don't just need the code, I need to understand what I am asking in my previous post.
 
Upvote 0
Little confused now.:eek:

That sounds like you want to open two other workbooks, the TEMPLATEWORKBOOK and another workbook.

You then want to copy a sheet, TEMPLATEWORKSHEET to the other workbook that was opened.

Is that correct or am I missing something?
 
Upvote 0
Little confused now.:eek:

That sounds like you want to open two other workbooks, the TEMPLATEWORKBOOK and another workbook.

You then want to copy a sheet, TEMPLATEWORKSHEET to the other workbook that was opened.

Is that correct or am I missing something?

Exactly
 
Upvote 0
Thanks!


Can you explain a bit please:
I assume wb is a variable.
Which of the rest words like Workbook, Workbooks, ThisWorkbook are builtin variables and not user-defined variables?
Can you go through each word please to explain?


For example:
Dim wb as Workbook : I understand that you set a variable named wb to be Workbook, so is Workbook a built in name for the type/class of variable for workbooks?
Then you set wb to open SpecificWorkbook but what does the Workbooks word mentioned twice mean?
Then you copy the Specific sheet from the workbook wb, into ThisWorkbook. What is the ThisWorkbook? is it builtin variable for the active workbook?

This will help me very much,
Thanks!

Code:
Sub r()
Dim wb As Workbook [COLOR=#008000]'Declare object [/COLOR][COLOR=#008000]variable[/COLOR]
Set wb = Workbooks.Open(Workbooks("SpecificWorkbook.xlsx") [COLOR=#008000]'Initialize object variable[/COLOR]
wb.Sheets("Specific").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) [COLOR=#008000]'Copy sheet from opened wb to active workbook.[/COLOR]
wb.Close [COLOR=#008000]'Close opened workbook.  Add 'False' to statement to prevent alert message.[/COLOR]
End Sub
The 'wb' user defined variable equates to 'Workbooks("SpecificWorkbook.xlsx)' wherever it is used in the code.
'ThisWorkbook' refers to the workbook hosting the code and is an Excel application constant.
'Workbook' is an Excel keyword for an Excel objeect and is part of the 'Workbooks' collection.
There are numerous tutorials on the web which explain the basics of VBA. You can locate them by typing 'Free VBA Tutorial' in the web search box and press enter. Several options should show as a result.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,834
Messages
6,121,876
Members
449,056
Latest member
ruhulaminappu

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