VBA Runtime Error when Declaring/Setting Variables

Seba Robles

Board Regular
Joined
May 16, 2018
Messages
71
Office Version
  1. 2019
  2. 2016
Platform
  1. Windows
I’m getting the following error when declaring and setting some variables in my code:

  • Runtime error 438 – Object doesn’t support this property or method
  • Runtime error 1004 – Application-defined or object-defined error


    What does the code do?
    1. Clears contents, enters new data to a column, drags down formulas on other columns
    2. Filter column E by “DISCONTINUED” and delete visible rows
    3. Show all data then filter again column E by “CHECK”

Here’s part of my code

Code:
Private Sub UpdateData_Click()
'Declare variable - filter range
Dim LngLastRow As Long
Dim FilterRange As Range

'Set reference up front
With Sheets("PAT")
    LngLastRow = .Range("A7:M" & .RowsCount).End(xlUp).Row
    Set FilterRange = .Range("E7:E" & LngLastRow)
End With

With Sheets("PAT")

[PART 1 OF CODE RUNS HERE]
    
'Part 2 of code begins

    'Filter & Delete Discontinued/Exception codes
    On Error Resume Next
    With FilterRange
        
        'Filter by DISCONTINUED
        .AutoFilter Field:=5, _
                    Criteria1:="DISCONTINUED"

                'Delete visible filtered rows
                    .Offset(1, 0).Resize(.Rows.Count - 1).SpecialCells(xlCellTypeVisible).Rows.Delete
    End With
                
    .ShowAllData
    
    'Filter by CHECK
    With FilterRange
        .AutoFilter Field:=5, _
                    Criteria1:="CHECK"
    End With

End With
End Sub

Also, is the range for Set FilterRange variable correct? Iwant to filter column E but the data goes from column A through M with headersat row 7.


I’m having trouble with executing step 2 of the code. I will appreciate any help you can give and would be grateful if you explain why the errors are occurring for me to learn and better understand.


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.
Hi. Try after changing some lines of the code as shown below:

LngLastRow = .Cells(Rows.Count, "A").End(xlUp).Row

.AutoFilter Field:=1, Criteria1:="DISCONTINUED"

.Offset(1, 0).Resize(LngLastRow - 6).SpecialCells(xlCellTypeVisible).EntireRow.Delete

.AutoFilter Field:=1, Criteria1:="CHECK"
 
Upvote 0
How about
Code:
Private Sub UpdateData_Click()
   'Declare variable - filter range
   Dim LngLastRow As Long
   Dim FilterRange As Range
   
   'Set reference up front
   With Sheets("PAT")
       LngLastRow = .Range("A" & .Rows.Count).End(xlUp).Row
       Set FilterRange = .Range("E7:E" & LngLastRow)
   End With
   
      
   [PART 1 OF CODE RUNS HERE]
       
   'Part 2 of code begins
   
   'Filter & Delete Discontinued/Exception codes
   With FilterRange
      'Filter by DISCONTINUED
      .AutoFilter 1, "DISCONTINUED"
      'Delete visible filtered rows
      On Error Resume Next
      .Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete
      On Error GoTo 0
      .AutoFilter 1
   End With
   
   'Filter by CHECK
   FilterRange.AutoFilter 1, "CHECK"
      
End Sub
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,480
Messages
6,125,050
Members
449,206
Latest member
Healthydogs

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
Back
Top