title: Using a Private Function inside a Sub creates Subscripts out of Range?
body: <p>So I have a macro that compares two workbooks and eliminates any similar entries on the Workbook running the macro but I have at the end a Private Function that runs to sort the second sheet of the Workbook's contents. When I run the function by itself it works fine, executing and sorting the heet with no problem, however when I run it in the Macro itself it returns this:</p>
  
  <blockquote>
    <p>Run-time error '9': Subscript out of Range</p>
  </blockquote>
  
  <pre><code>Sub CheckReadyBoard()
      Dim I, J, total, fRow As Integer
      Dim found As Range
  
  
      'SP = SharePoint List, SPS = SharePoint Sheet
      'Filter by Dept then Machine
      '
      Dim SP As Workbook
      Dim SPS As Worksheet
      Set SP = Workbooks.Open(get_user_specified_filepath())
      Set SPS = SP.Sheets(1)
      Set HSheet = ThisWorkbook.Sheets("Building Parts")
      ' Remove Anything from Readyboard and NOT Ready for T
      HBLR = HSheet.Range("A" &amp; Rows.Count).End(xlUp).Row
      RBLR = SPS.Range("A" &amp; Rows.Count).End(xlUp).Row
  
      For I = 2 To HBLR
          answer1 = HSheet.Range("I" &amp; I).Value
          Set found = SPS.Columns("G:G").Find(what:=answer1) 'finds a match
          If found Is Nothing Then
              'Leave Line w/ Nothing so it will pass this line
          Else
              HSheet.Range("I" &amp; I).EntireRow.Delete
          End If
      Next I
      'Application.ScreenUpdating = False
      'Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
      'Application.ScreenUpdating = True
      Sort2
      SP.Close
  
  End Sub
  
  
  
  Private Function Sort2()
      Set HSheet = ThisWorkbook.Sheets("Building Parts")
      HBLR = HSheet.Range("A" &amp; Rows.Count).End(xlUp).Row
      Worksheets("Building Parts").Activate
  
      With Range(Columns("A"), Columns("I"))
          .Sort key1:=Columns("A"), Order1:=xlDescending, Header:=xlNo
          .Sort key1:=Columns("B"), Order1:=xlDescending, Header:=xlNo
      End With
  End Function
  </code></pre>
  
  <p>When I hit debug it highlights the <code>Worksheets("Building Parts").Activate</code> I'm not super well-versed in VBA so i'm not sure if my Syntax is bad or if my I need to make a slight edit someplace.. </p>
  
  <p>Information that might be important:</p>
  
  <p>1)My Workbook contains two sheets. Sheet 1 has buttons to initiate Macros and Sheet 2 contains the data. </p>
  
  <p>2)Adding <code>Worksheets("Building Parts").Activate</code> made the Sort work on its own, previously it didn't 
      work by itself or in the Macro..</p>