Move between workbooks error

albertod8

New Member
Joined
Mar 31, 2023
Messages
10
Office Version
  1. 2021
Platform
  1. Windows
Hi, I am beginner and I am trying to build a code that copies a couple of worksheets into a new workbook. It is working until the line that i mark in red. I am trying to move to WB2 to paste the data and it tries to paste it over WB1.

Sub country_overview()

' country1
'
Dim WB1 As Workbook
Dim WB2 As Workbook

Set WB1 = ThisWorkbook
Cells.Select
Selection.Copy
Workbooks.Add
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
ActiveWindow.Zoom = 70
Sheets("Sheet1").Select
Sheets("Sheet1").Name = "Country"
Sheets.Add After:=ActiveSheet
Sheets("Sheet2").Select
Sheets("Sheet2").Name = "Programs "
Set WB2 = ThisWorkbook
WB1.Activate
Sheets("Current Month").Select
Rows("2:2").Select
Application.CutCopyMode = False
Selection.AutoFilter
Range("C3").Select
ActiveSheet.Range("$A$2:$AD$10000").AutoFilter Field:=3, Criteria1:= _
"Finland"
Cells.Select
Selection.Copy
WB2.Activate
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.CutCopyMode = False
WB1.Activate
End Sub
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
If you are adding a new Workbook and want to set WB2 equal to that, I believe you want to change this:
VBA Code:
Set WB2 = ThisWorkbook
to this:
VBA Code:
Set WB2 = ActiveWorkbook
as "ThisWorkbook" is the workbook the VBA code is located in, not your new workbook.
 
Upvote 1
Solution
You are welcome.
Glad I was able to help!
 
Upvote 0

Forum statistics

Threads
1,224,798
Messages
6,181,038
Members
453,014
Latest member
Chris258

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