VBA - Listing File names in a folder using a macro in Excel 2007

RoundRocket

New Member
Joined
Nov 28, 2013
Messages
24
Hi, i have the vba code below which lists file names in a chosen folder in the first column of an excel worksheet (worksheet 2 in this case), does anyone know how i can adapt this code so that it doesnt pick up and report on any files that begin with a '~$' ?

Occasionally it picks up files beginning like this, for example: ~$Doc1.docx
If anyone knows what these file types are?! please let me know. I think they are temporary files.. but not sure. thank you.

Sub Button3_Click()
Call ListDocumentFiles
End Sub
Sub ListDocumentFiles()
Dim fso As New FileSystemObject
Dim fle As file
Dim fldr As folder
Dim i As Integer

Call ThisWorkbook.Worksheets(2).Range("A:A").Clear
With Application.FileDialog(msoFileDialogFolderPicker)
Call .Show
Set fldr = fso.GetFolder(.SelectedItems(1))
End With
For Each fle In fldr.Files
If InStr(1, fle.Name, ".", vbTextCompare) <> 0 Then
Let i = i + 1
Let ThisWorkbook.Worksheets(2).Cells(i, 1) = fle.Name
End If
Next
MsgBox ("Search complete")
Set fle = Nothing
Set fldr = Nothing
Set fso = Nothing
End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
If InStr(1, fle.Name, ".", vbTextCompare) <> 0 and Left$(fle.Name, 2) <> "~$" Then
 
Upvote 0
Back-up of open excel files, in which you are working, but haven't saved yet -- Excel does the saving for you just in case, I think
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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