Help VBA with Selection Command

purceld2

Well-known Member
Joined
Aug 18, 2005
Messages
586
Office Version
  1. 2013
Platform
  1. Windows
I wonder if somebody could help me with the VBA code below. Currently the code will delete all the files that have been selected in the spreadsheet. I notice that if I filter the rows and make a selection it will delete all the hidden rows also. How can I amend the code to delete just the visible cells in the selection.


Code:
Sub DelFiles()
Dim blnDelAll As Boolean
Dim rngVal As Range
Dim rngCl  As Range
Set rngVal = Selection     'Columns("I").SpecialCells(xlCellTypeVisible)
'Set rngVal = Columns("I").SpecialCells(xlCellTypeVisible)
On Error Resume Next
    For Each rngCl In rngVal.Cells
        Recycle rngCl.Value
        
         With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .Color = 65535
        .TintAndShade = 0
        .PatternTintAndShade = 0
         End With
        
        If Err.Number <> 0 Then
            Err.Clear
            rngCl.Offset(, 1) = "File not found"
            blnDelAll = True
        End If
    Next
On Error GoTo 0
If blnDelAll Then
    MsgBox "Some File were not deleted, invalid path or you do not have permission to delete this file."
Else
    MsgBox "All Files successfully deleted."
End If
End Sub

Thanks in advance
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try:

Code:
Set rngval = Selection.SpecialCells(xlCellTypeVisible)
 
Upvote 0

Forum statistics

Threads
1,214,830
Messages
6,121,839
Members
449,051
Latest member
excelquestion515

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