Run-time error '9' subscript out of range

Diane S 21 Reputation points
2021-01-08T14:49:31.28+00:00

First, I am not a programmer! I copied a VBA code found online and modified it to fit my data. The scripts worked fine when I applied to a file using dummy data; however, when I copied to my real data file, I get the "run-time error '9' subscript out of range" error message. Here is the VBA code I'm using...


Sub SplitandFilterSheet()

'Step 1 - Copy Sheet

'Step 2 - Filter by Department and delete rows not applicable

'Step 3 - Loop until the end of the list

Dim Splitcode As Range

Sheets("Master").Select

Set Splitcode = Range("Dept_Split_Code")

For Each cell In Splitcode

Sheets("Master").Copy After:=Worksheets(Sheets.Count)

ActiveSheet.Name = cell.Value

With ActiveWorkbook.Sheets(cell.Value).Range("Master_Data")

.AutoFilter Field:=22, Criteria1:="<>" & cell.Value, Operator:=xlFilterValues

.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Delete

End With

ActiveSheet.AutoFilter.ShowAllData

Next cell

End Sub


The error occurs "With ActiveWorkbook.Sheets(cellValue).Range("Master_Data")

I've verified with range names are correct. Any assistance is greatly appreciated.

As typical, time is of the essence!

Thanks - Diane

Developer technologies | Visual Basic for Applications
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-01-08T18:52:32.5+00:00

    Check if this works: *With ActiveWorkbook.Sheets(****CStr(cell.Value)****).Range("Master_Data")*.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.