AutoFilter method of range class failed

Akbarov

Active Member
Joined
Jun 30, 2018
Messages
347
Office Version
  1. 365
Platform
  1. Windows
Hello, I am trying to delete whole row.
If any cell in column A on sheet1 contains a value from sheet2 column P
But for some reason I get error 1004

Can anyone help me please? Where I did a mistake?

Error line
Code:
.AutoFilter 1, Array(Arr), 7

VBA Code:
    Dim wb As Workbook
    Dim ws As Worksheet, Arr, i As Long
    Dim LR As Long
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Sheet1")
    Set sh = wb.Sheets("Sheet2")
    Application.ScreenUpdating = False
   
    ws.Activate
    LR = Cells(Rows.Count, 16).End(xlUp).Row
   
    Arr = ws.Range("P2:P" & LR).Value
    Arr = Application.Transpose(Application.Index(Arr, 0, 1))
   
    For i = LBound(Arr) To UBound(Arr)
        Arr(i) = CStr(Arr(i))
    Next i
   
    sh.Activate
    With sh.Range("A3:A2000")
        .AutoFilter 1, Array(Arr), 7
        .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        .AutoFilter
    End With

And just debugged:
Arr(i) = <Subscript out of range>
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Can you show me your Sheet1, Column P, make sure you show both Row and Column references.
 
Upvote 0
Actually based on your description you have your sheet references back to front.
I am trying to delete whole row.
If any cell in column A on sheet1 contains a value from sheet2 column P
If the above means you are trying to delete rows from Sheet1 just reverse your sheet references to the below.
Rich (BB code):
    Set ws = wb.Sheets("Sheet1")
    Set sh = wb.Sheets("Sheet2")
    Set ws = wb.Sheets("Sheet2")
    Set sh = wb.Sheets("Sheet1")
 
Upvote 0
Thank you for reply.
I replaced sheet names may be this way will be better.
VBA Code:
    Dim wb As Workbook
    Dim ws As Worksheet, Arr, i As Long
    Dim LR As Long
    Set wb = ThisWorkbook
    Set ws = wb.Sheets("Backend")
    Set sh = wb.Sheets("MAIN")
    Application.ScreenUpdating = False
    
    ws.Activate
    LR = Cells(Rows.Count, 16).End(xlUp).Row
    
    Arr = ws.Range("P2:P" & LR).Value
    Arr = Application.Transpose(Application.Index(Arr, 0, 1))
    
    For i = LBound(Arr) To UBound(Arr)
        Arr(i) = CStr(Arr(i))
    Next i
    
    sh.Activate
    With sh.Range("A3:A2000")
        .AutoFilter 1, Array(Arr), 7
        .Offset(1).Resize(.Rows.Count - 1).EntireRow.Delete
        .AutoFilter
    End With
 

Attachments

  • ColumnP.png
    ColumnP.png
    167.9 KB · Views: 5
  • ColumnMain.png
    ColumnMain.png
    90.9 KB · Views: 7
Upvote 0
Based on those sheet names and those Set commands, the code will look in Backend at Column P, get the 2 values that are there being 2017 and 2018.
Filter the sheet main applying those values to filter column A of the Sheet Main, and delete those rows.

What happens when you run it ?
Does it still give you an error message ? If so what is the message and which line in the code is hightlighted ?
If there is no error does it delete what you are expecting ?
 
Upvote 0
I get error in following line.
 

Attachments

  • error111111.png
    error111111.png
    23.4 KB · Views: 11
Upvote 0
Do you know how to use the Watch window ? If so put Arr in the watch window and click on the plus sign to see what is in Arr.
If not, then when it errors out, do the following:
• Double click on the word Arr to select it (doesn't matter which one)
• right click and Add Watch
• Hit ok

Send me a picture of the Watch window if there is a plus sign click on it first to expand it like the below image.
I am login off but can have a look tomorrow.
Share a sample of your work book via dropbox, google drive etc with your code and sample data if you can.

1666962664022.png
 
Upvote 0
Here it is. Everything looks fine.
If I can't solve it today, I will share sample file.
Thanks for your time.
 

Attachments

  • error1111112.png
    error1111112.png
    8.7 KB · Views: 7
Upvote 0
I get error in following line.
So you are getting an error on the line Arr(i) = CStr(Arr(i)) but when it errors out the watch window is showing what you have in Post #8, that doesn't seem to make sense.
Are you going to be able to share a sample file ?
 
Upvote 0
Hi Alex, the data in Main was imported from different sheet. I was using xlPasteAllUsingSourceTheme then I replaced it with xlPasteValues it works fine now. Thanks for your time.
 
Upvote 0

Forum statistics

Threads
1,215,615
Messages
6,125,857
Members
449,266
Latest member
davinroach

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