VBA Delete Files that are not ending in <insert text here>

Ottsel

Board Regular
Joined
Jun 4, 2022
Messages
167
Office Version
  1. 365
Platform
  1. Windows
Fairly new still to VBA. I've tried to put this together, but keep running into issues. Anyone see what I'm doing wrong?

In short, whatever is in column E or column 5, if it doesn't end in ".xls" or ".xlsm" then i want to delete the row.

VBA Code:
Sub test123()

Dim y As Long
Dim cellValue As String
 
For y = 2 To 257
    cellValue = Cells(y, 5).Value
    If cellValue = Right(cellValue, 4).Value = ".xls" Or cellValue = Right(cellValue, 5).Value = ".xlsm" Then
        Rows(y).EntireRow.Delete
    End If
Next y
    
End Sub

Tips, tricks or pointers would be appreciated! Thanks in advance!
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
cellValue is not an object it's a string, so you can't use it as an object and use the .Value property

But you can use them directly.

Try:

VBA Code:
Sub test123()
  Dim y As Long
  Dim cellValue As String
 
  Application.ScreenUpdating = False
  For y = 257 To 2 Step -1
    cellValue = Cells(y, 5).Value
    If Right(cellValue, 4) = ".xls" Or Right(cellValue, 5) = ".xlsm" Then
      Rows(y).EntireRow.Delete
    End If
  Next y
  Application.ScreenUpdating = True
End Sub


-----

Another way to delete:
VBA Code:
Sub test1234()
  ActiveSheet.Range("E1:E257").AutoFilter 1, "*.xls", xlOr, "*.xlsm"
  ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.Delete
  ActiveSheet.ShowAllData
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
 
Last edited:
Upvote 1
Solution
cellValue is not an object it's a string, so you can't use it as an object and use the .Value property

But you can use them directly.

Try:

VBA Code:
Sub test123()
  Dim y As Long
  Dim cellValue As String
 
  Application.ScreenUpdating = False
  For y = 257 To 2 Step -1
    cellValue = Cells(y, 5).Value
    If Right(cellValue, 4) = ".xls" Or Right(cellValue, 5) = ".xlsm" Then
      Rows(y).EntireRow.Delete
    End If
  Next y
  Application.ScreenUpdating = True
End Sub


-----

Another way to delete:
VBA Code:
Sub test1234()
  ActiveSheet.Range("E1:E257").AutoFilter 1, "*.xls", xlOr, "*.xlsm"
  ActiveSheet.AutoFilter.Range.Offset(1).EntireRow.Delete
  ActiveSheet.ShowAllData
End Sub


--------------
Let me know the result and I'll get back to you as soon as I can.
Cordially
Dante Amor
--------------
I went with the first option, but I threw in a Not, so it wouldn't delete the xls or xlsm files, which was an error on my end.

As always, thank you Dante!

VBA Code:
If Not Right(cellValue, 4) = ".xls" Or Right(cellValue, 5) = ".xlsm" Then
 
Upvote 0
so it wouldn't delete the xls or xlsm file
In that case it should be like this.
That is, one condition is not met and the other condition is not met.
Rich (BB code):
If Not Right(cellValue, 4) = ".xls" And Not Right(cellValue, 5) = ".xlsm" Then

Or this way. The conditions must go between parentheses after the NOT, that is, that one or the other condition is not met.
Rich (BB code):
If Not (Right(cellValue, 4) = ".xls" Or Right(cellValue, 5) = ".xlsm") Then

:cool:
 
Upvote 0
In that case it should be like this.
That is, one condition is not met and the other condition is not met.
Rich (BB code):
If Not Right(cellValue, 4) = ".xls" And Not Right(cellValue, 5) = ".xlsm" Then

Or this way. The conditions must go between parentheses after the NOT, that is, that one or the other condition is not met.
Rich (BB code):
If Not (Right(cellValue, 4) = ".xls" Or Right(cellValue, 5) = ".xlsm") Then

:cool:
oh, good to know. Thanks Dante!
 
Upvote 0

Forum statistics

Threads
1,215,103
Messages
6,123,103
Members
449,096
Latest member
provoking

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