title: Search and copy from another worksheet
body: <p>I'm trying to make a function that based on data from one worksheet will search another for comparison and copy from that to first one.</p>
  
  <p>This is my function:</p>
  
  <pre><code>Sub kopiuj()
      Dim row As Long
      Dim copyRng As Range, pasteRng As Range
  
      row = 7
      If IsEmpty(Cells(row, 3)) Then
          MsgBox "Error1"
          Exit Sub
      End If
      Do Until IsEmpty(Cells(row, 3))
       If IsEmpty(Cells(row, 6)) And Cells(row, 5) = "cond" Then
            MsgBox "Error2"
             Exit Sub
       End If
       copyRng = szukaj2(Cells(row, 5).Value, Cells(row, 6).Value)
       Set pasteRng = Range(Cells(row, 8), Cells(row, 25))
       copyRng.Copy pasteRng
       row = row + 1
      Loop
  
  End Sub
  
  Function szukaj2(ByVal pp As String, ByVal p As String) As Range
    Dim PN As Worksheet
    Dim row As Integer
    Set PN = ActiveWorkbook.Sheets("PN")
  
    If pp &lt;&gt; "cond1" And pp &lt;&gt; "cond2" Then pp = "sth"
    For row = 7 To PN.Cells(Rows.Count, 4).End(xlUp).row Step 1
     If StrComp(pp, PN.Cells(row, 3).Value, vbTextCompare) = 0 And StrComp(p, PN.Cells(row, 1).Value, vbTextCompare) = 0 Then
      Set szukaj2 = PN.Range(PN.Cells(row, 8), PN.Cells(row, 25))
      Exit For
     End If
    Next
  End Function
  </code></pre>
  
  <p>I get Error 91 </p>
  
  <blockquote>
    <p>runtime error "Object variable or With block variable not set"</p>
  </blockquote>