Go back to first workbook after activating a second workbook

VBAProIWish

Well-known Member
Joined
Jul 6, 2009
Messages
1,027
Office Version
  1. 365
Platform
  1. Windows
Hello All,

The code below copies data from the "2011-03 Green" workbook and pastes it into the "Tea" workbook.

How can I tell the macro to go back to the original workbook which will have a different name every month I run this macro?

For example...THIS month it's "2011-03 Green", but next month the workbook will be named "2011-04 Green"

Code:
Sub aaa_test_macro
 
 
Dim Rng As Range
Set Rng = Range("a1").CurrentRegion
Set Rng = Rng.Offset(1, 0)
Set Rng = Rng.Resize(Rng.Rows.Count - 1)
Rng.Copy
 
    Windows("Tea.xlsm").Activate
 
Range("A1").End(xlDown).Offset(1, 0).Select
    ActiveSheet.Paste
 
How can I change the line below to go to the previously activated workbook?
[B][COLOR=red]Windows("2011-03 Green.xlsm").Activate[/COLOR][/B]
 
End Sub


Thanks much!
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
VoG,

No, unfortunately it's not. The workbook will never have the same name either.
 
Upvote 0
Wouldn't there be some code like...

Set activesheet as "originalsheet"

code

code

code


Go back to "originalsheet"?

Just a thought
 
Upvote 0
Try

Code:
Sub aaa_test_macro()
 
 
Dim Rng As Range
Dim wb As Workbook
Set wb = ActiveWorkbook
Set Rng = Range("a1").CurrentRegion
Set Rng = Rng.Offset(1, 0)
Set Rng = Rng.Resize(Rng.Rows.Count - 1)
Rng.Copy
 
    Windows("Tea.xlsm").Activate
 
Range("A1").End(xlDown).Offset(1, 0).Select
    ActiveSheet.Paste
 
wb.Activate
 
End Sub
 
Upvote 0
Yes yes yes,

That worked! 2 quick things if possible...

The only thing now is that the "tea" WB starts as a hidden WB, but after the macro, it becomes unhidden.

1. Is there a way to keep the WB hidden after the macro?
2. Is there a way to ignore that ENTIRE macro if the "tea" WB is not found?

This macro would be perfect then :biggrin:


Thanks!
 
Upvote 0
Maybe:

Code:
Sub aaa_test_macro()
 
 
Dim Rng As Range
Dim wb As Workbook
Set wb = ActiveWorkbook
Set Rng = Range("a1").CurrentRegion
Set Rng = Rng.Offset(1, 0)
Set Rng = Rng.Resize(Rng.Rows.Count - 1)
Rng.Copy
On Error GoTo puke
    Windows("Tea.xlsm").Activate
 
Range("A1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
puke:
 
wb.Activate
ActiveWindow.Visible = False
End Sub
 
Upvote 0
Ahh, maybe my fault, it's hiding the original workbook, not the "Tea" workbook.
 
Upvote 0
Sorry, I misunderstood. Try

Code:
Sub aaa_test_macro()
 
 
Dim Rng As Range
Dim wb As Workbook
Set wb = ActiveWorkbook
Set Rng = Range("a1").CurrentRegion
Set Rng = Rng.Offset(1, 0)
Set Rng = Rng.Resize(Rng.Rows.Count - 1)
Rng.Copy
On Error GoTo puke
    Windows("Tea.xlsm").Activate
 
Range("A1").End(xlDown).Offset(1, 0).Select
ActiveSheet.Paste
Windows("Tea.xlsm").Visible = False
puke:
 
wb.Activate
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,543
Messages
6,179,429
Members
452,914
Latest member
echoix

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