Subscript out of range VBA

excelerino

New Member
Joined
Nov 2, 2016
Messages
47
Hi all,

I'm trying to get rows with errors automatically deleted whenever a particular sheet is activated. The following code, most of which I got online, give me a subscript out of range error on the bolded line. I saw there was a similar thread earlier today, but I just don't understand the problem

Private Sub Worksheet_Activate()
Dim rng As Range
Dim rngError As Range
Set rng = Worksheets("Sheet7")
On Error Resume Next
Set rngError = rng.Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
On Error GoTo 0
If Not rngError Is Nothing Then
rngError.EntireRow.Delete
End If
End Sub

Thanks in advance!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,849
Office Version
  1. 365
Platform
  1. Windows
You are trying to set a sheet to a variable you have said in your Dim is a range.
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
You've declared rng as a Range but then here you try and assign it to a worksheet, that's not going to work.

Which sheet do you want to delete rows from?

Which range on that sheet do you want to check for errors?
 
Upvote 0

excelerino

New Member
Joined
Nov 2, 2016
Messages
47
Ok. I first had it as the following, in which I just changed the range values

Set rng = Sheets("Sheet7").Range("A1:DC461")

But basically I want it to check the entirety of Sheet7 as more data will be added
 
Upvote 0

Big Wade

Board Regular
Joined
Jul 15, 2014
Messages
50
Try something like this.
Code:
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Dim lastRow As Long
lastRow = Range("A" & Rows.Count).End(xlUp).Row
If Sh.Name = "Sheet7" Then
    For j = lastRow To 1 Step -1
        If IsError(Range("A" & j)) Then Rows(j).Delete
    Next j
End If
End Sub

Change the Sh.Name and Range column to match your preferences
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Which sheets module is the code in?
 
Upvote 0

excelerino

New Member
Joined
Nov 2, 2016
Messages
47
@Big Wade Thanks. For some reason nothing happens though when that code is in place?

@Norie I put the code in Sheet7
 
Upvote 0

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,358
Office Version
  1. 365
Platform
  1. Windows
Try this.
Code:
Private Sub Worksheet_Activate()
Dim rng As Range
Dim rngError As Range

    Set rng = Me.UsedRange ' Me refers to the sheet the module belongs to

    On Error Resume Next
    Set rngError = rng.Cells.SpecialCells(xlCellTypeFormulas, xlErrors)
    On Error GoTo 0

    If Not rngError Is Nothing Then
        rngError.EntireRow.Delete
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,190,810
Messages
5,983,044
Members
439,816
Latest member
aggelos

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top