I am trying to write a program which would take the information from a user selected grid and the information adjacent to it and send them to another workbook. However, whenever I compile, I would get the error 1004 (Automation). Can someone please point out where I have made a mistake in my code? It will be greatly appreciated.
Sub CopyItemsByLocation()
Dim wbThis As Workbook
Dim wsThis As Worksheet
Dim wbTarget As Workbook
Dim wsTarget As Worksheet
Dim strName As String
Dim i As Integer
Dim rng1 As Range
Set wbThis = ActiveWorkbook
Set wsThis = ActiveSheet
strName = ActiveSheet.Name
Set wbTarget = Workbooks.Open("C:\Users\Administrator\Desktop\Excel Testing\Excel Info Testing 2.xlsx")
Set wsTarget = wbTarget.Worksheets(strName)
Set rng1 = Selection
For i = 1 To 4
If i = 1 Then
wsThis.Range(rng1).Copy Destination:=wsTarget.Range("E5") **'<~Error occurs here**
Set rng1 = rng1.Offset(0, 1)
ElseIf i = 2 Then
wsThis.Range(rng1).Copy Destination:=wsTarget.Range("G5")
Set rng1 = rng1.Offset(0, 1)
ElseIf i = 3 Then
wsThis.Range(rng1).Copy Destination:=wsTarget.Range("I5")
Set rng1 = rng1.Offset(0, 1)
Else
wsThis.Range(rng1).Copy Destination:=wsTarget.Range("K5")
Set rng1 = rng1.Offset(0, 1)
End If
Next i
Application.CutCopyMode = False
wbTarget.Save
wbTarget.Close
Set wbTarget = Nothing
Set wbThis = Nothing
End Sub