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:
Run-time error '9': Subscript out of Range
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" & Rows.Count).End(xlUp).Row
RBLR = SPS.Range("A" & Rows.Count).End(xlUp).Row
For I = 2 To HBLR
answer1 = HSheet.Range("I" & 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" & 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" & 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
When I hit debug it highlights the Worksheets("Building Parts").Activate
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..
Information that might be important:
1)My Workbook contains two sheets. Sheet 1 has buttons to initiate Macros and Sheet 2 contains the data.
2)Adding Worksheets("Building Parts").Activate
made the Sort work on its own, previously it didn't
work by itself or in the Macro..