VBA: Set active workbook when macro is run as 'thisworkbook'

Lefemmenikita

Board Regular
Joined
Jan 28, 2014
Messages
59
Office Version
  1. 2013
Platform
  1. Windows
Hi There

I have a workbook which is a newly opened excel workbook which is unsaved

A user will paste data into this blank workbook and run a macro called 'termination' (which is residing in their personal.xlsb project)

One of the actions of this macro is to open another workbook called 'source.'

I want data from 'source' to be pasted as a separate sheet in the blank workbook, which is the workbook that was active when the termination macro was run.

How do I do this?

So far, my code opens the file 'source' though cannot paste into the original unsaved workbook (the paste method fails)

My code so far
VBA Code:
Sub Termination
--wb is referring to the blank workbook opened. This is not saved
Dim wb as workbook
-----wb_1 has the info that will be copied
Dim wb_1 as workbook
dim wb_sheet as worksheet
dim ws as worksheet

set workbook= thisworkbook
set wb_1= workbooks.open("M:\network_files\source.xlsx")

with wb
set wb_sheet=worksheets(1)
end with

with wb_1.sheets(1).select
activesheet.copy
wb_1.close
end with


with wb
set ws=sheets.add
ws.select
activesheet.paste



Sorry if the code is nonsensical. I don't know how to write VBA properly


Thanks
 

Some videos you may like

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
How about
VBA Code:
Sub Termination()
'wb is referring to the blank workbook opened. This is not saved
Dim wb As Workbook
'wb_1 has the info that will be copied
Dim wb_1 As Workbook
Dim wb_sheet As Worksheet
Dim ws As Worksheet

Set wb = ActiveWorkbook
Set wb_1 = Workbooks.Open("M:\network_files\source.xlsx")


Set wb_sheet = wb.Worksheets(1)

wb_1.Sheets(1).Copy , wb.Sheets(1)
wb_1.Close
End Sub
 

Lefemmenikita

Board Regular
Joined
Jan 28, 2014
Messages
59
Office Version
  1. 2013
Platform
  1. Windows
Thank you Fluff. I haven't had a chance to test this yet...I will try it in the next few days when I have access to the workbook in question and let you know how I go
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
55,301
Office Version
  1. 365
Platform
  1. Windows
Ok, that's fine. :)
 

Watch MrExcel Video

Forum statistics

Threads
1,127,117
Messages
5,622,821
Members
415,934
Latest member
adstocking

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
Top