Please Need Help with Selection.PasteSpecial not working.

Ben AFF

Board Regular
Joined
Sep 21, 2023
Messages
54
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a macro that allows user to select a file and copy a range from a selected worksheet from this file into a worksheet where the macro is actioned.
When actioned the range from the selected file is copied but its not pasted into the worksheet where the macro is actioned.

The macro needs to copy the range (A2:B100) in Sheet 1 of a selected workbook into Sheet 2 starting from A2 of the workbook where the Macro is actioned.

I cannot find what is wrong. Im pretty useless with VBA coding.

Please can you help me make it work with the correct code? Much appreciated

Sub GG()

Set masterBook = ActiveWorkbook

sFileName = Application.GetOpenFilename(Title:="Choose file", MultiSelect:=False)

If sFileName = False Then
MsgBox "Please select a file"
Exit Sub
End If


Set otherbook2 = Workbooks.Open(sFileName)
Sheets("sheet1").Select
Range("A2:B100").Copy


masterBook.Activate
Sheets("sheet2").Select
Range("A2").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False


otherbook2.Close SaveChanges:=False
End Sub

Sub CloseWorkbook()

otherbook2.Close SaveChanges:=False ' <<< this will not work; Object variable "otherBook2" is out of scope

End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.

Forum statistics

Threads
1,215,069
Messages
6,122,956
Members
449,096
Latest member
Anshu121

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