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
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,943
Messages
6,175,546
Members
452,652
Latest member
eduedu

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