excel vba delete all files except name files in folder site

KlausW

Active Member
Joined
Sep 9, 2020
Messages
403
Office Version
  1. 2016
Platform
  1. Windows
Hi

I am using this VBA-code to Kill all files in a folder.

Kill "C:\Users\k-wit\OneDrive\F-div\Rejseafregning\Bilag\*.*"

It works. But I shout like VBA to leave this fil B1.PNG

back every time.

Someone who can help.

Regards Klaus W
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
if you are looping through the files, checking the file name before deleting the file would by an idea.

can you show the code you are using to loop through the files?
 
Upvote 0
if you are looping through the files, checking the file name before deleting the file would by an idea.

can you show the code you are using to loop through the files?
I use this code.
VBA Code:
Sub Del()

   Kill "C:\Users\k-wit\OneDrive\F-div\Rejseafregning\Bilag\*.*"
  
    End Sub
 
Upvote 0
I found this code here on MrExcel an it work.
VBA Code:
Sub Del()


    Dim path As String
    path = Range("I3")
    If Right(path, 1) <> "\" Then
        path = path & "\"
    End If
    
    Dim exceptedFile As String
    exceptedFile = "B1.png"
    
    Dim currentFile As String
    currentFile = Dir(path & "*.*", vbNormal)
    
    While (Len(currentFile) > 0)
        If LCase(currentFile) <> LCase(exceptedFile) Then
            Kill path & currentFile
        End If
        currentFile = Dir
    Wend
    
    MsgBox "Bilag slettet!", vbExclamation

 
   'Sheets("Stamdata").Select
  ' Mail_workbook_Outlook
   
   End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,217,388
Messages
6,136,307
Members
450,003
Latest member
AnnetteP

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