I'm copy certain cell from one sheet to another sheet. My code searches for a value on sheet set as Condition and then searches the Source sheet for the value. Once found it copies specific cells of the row from the Source sheet to a new row on the Target sheet. I'm getting run time error 13, type mismatch. I'm not sure what I've done wrong.
VBA Code:
Sub specific_columns
Dim g As Range
Dim h As Range
Dim z As Integer
Dim Source1 As Worksheet
Dim Target1 As Worksheet
Dim Condition1 As Worksheet
Set Source1 = ActiveWorkbook.Worksheets("Input")
Set Condition1 = ActiveWorkbook.Worksheets("Top10NoisiestClient")
'This will start copying data to Target sheet at row 3
For Each g In Condition1.Range("A2:A11") 'specifiy condition
'create worksheet for each value in condition
Set Target1 = Sheets.Add(after:=ActiveSheet)
Target1.Name = "Client-" & g.Value
'add code to show client name on line A1
Target1.Range("A1").Value = g.Offset(, 1).Value
Target1.Range("A1:K1").Merge
Target1.Range("A1").Interior.ColorIndex = 45
[A2:K2] = Split("Start_Time Host_Name Client_Name Message Count Probe Source Origin Status End_Time Ack_By")
For Each h In Source1.Range("M" & Rows.Count).End(xlUp)
If h.Value = g.Value Then
Source1.Cells(h, 5).Copy Target1.Cells(lastrow + 1, 1) '<-- causes error
Source1.Cells(h, 11).Copy Target1.Cells(lastrow + 1, 2)
Source1.Cells(h, 22).Copy Target1.Cells(lastrow + 1, 3)
Source1.Cells(h, 6).Copy Target1.Cells(lastrow + 1, 4)
Source1.Cells(h, 8).Copy Target1.Cells(lastrow + 1, 5)
Source1.Cells(h, 17).Copy Target1.Cells(lastrow + 1, 6)
Source1.Cells(h, 12).Copy Target1.Cells(lastrow + 1, 7)
Source1.Cells(h, 13).Copy Target1.Cells(lastrow + 1, 8)
Source1.Cells(h, 3).Copy Target1.Cells(lastrow + 1, 9)
Source1.Cells(h, 4).Copy Target1.Cells(lastrow + 1, 10)
Source1.Cells(h, 21).Copy Target1.Cells(lastrow + 1, 11)
End If
Next h
Range("A1").Select
'Zoom to first cell
ActiveWindow.ScrollRow = 1
ActiveWindow.ScrollColumn = 1
Next g
End Sub