title: Excel VBA | Union method on another Worksheet
body: <p>I am trying to get a specific Worksheet exported to PDF. The script is called from another Worksheet. As there can be a case where I don't want to print/convert all sites into the PDF, I have some IF-methodes included. "If the site x is unwanted, do not include it on the UNION-method and therefore do not convert it to PDF". Works fine, as long as I call the script within the destination Worksheet. </p>
  
  <p>How can I call the script from another Worksheet? I always get the error, that the Range-Object can not be selected (in line: Union(intSeite1, intSeite2, intSeite3, intSeite4, intSeite5, intSeite6).Select).</p>
  
  <p>cheers and thanks,
  Martin</p>
  
  <pre><code>Sub export()
  Application.DisplayAlerts = False
  
  Dim rngSeite1 As Range
  Dim rngSeite2 As Range
  Dim rngSeite3 As Range
  Dim rngSeite4 As Range
  Dim rngSeite5 As Range
  Dim rngSeite6 As Range
  Dim rngDruckbereich As Range
  
  With Worksheets("Ausgabe")
      .Unprotect
  
      Set intSeite1 = .Range("A1:A92").EntireRow
  
      If .Range("a94") &lt;&gt; 1 Then
          Set intSeite2 = .Range("A94:A205").EntireRow
      Else
          Set intSeite2 = Nothing
      End If
  
      If .Range("a207") &lt;&gt; 1 Then
          Set intSeite3 = .Range("A207:A403").EntireRow
      Else
          Set intSeite3 = Nothing
      End If
  
      If .Range("a405") &lt;&gt; 1 Then
          Set intSeite4 = .Range("A405:A516").EntireRow
      Else
          Set intSeite4 = Nothing
      End If
  
      If .Range("a518") &lt;&gt; 1 Then
          Set intSeite5 = .Range("A518:A629").EntireRow
      Else
          Set intSeite5 = Nothing
      End If
  
      If .Range("a631") &lt;&gt; 1 Then
          Set intSeite6 = .Range("A631:A716").EntireRow
      Else
          Set intSeite6 = Nothing
      End If
  End With
  
      If Not intSeite4 Is Nothing And Not intSeite5 Is Nothing Then
          Union(intSeite1, intSeite2, intSeite3, intSeite4, intSeite5, intSeite6).Select
      ElseIf Not intSeite4 Is Nothing Then
          Union(intSeite1, intSeite2, intSeite3, intSeite4, intSeite6).Select
      ElseIf Not intSeite5 Is Nothing Then
          Union(intSeite1, intSeite2, intSeite3, intSeite5, intSeite6).Select
      Else
          Union(intSeite1, intSeite2, intSeite3, intSeite5, intSeite6).Select
      End If
  
      Selection.ExportAsFixedFormat _
      Type:=xlTypePDF, _
      Filename:=CreateObject("WScript.Shell").SpecialFolders("Desktop") &amp; "\" &amp; Worksheets("Eingabe").Range("D41"), _
      Quality:=xlQualityStandard, _
      IncludeDocProperties:=True, _
      IgnorePrintAreas:=False, _
      OpenAfterPublish:=True
  
  With Worksheets("Ausgabe")
      .Protect
  End With
  End Sub
  </code></pre>