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
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