Show Last file in a folder in Alphabetical order.

dimer

New Member
Joined
Dec 7, 2011
Messages
12
Hi All,

I have script below that create msgbox after msgbox with a file name of the files in the folder, one by one, in alphabetical order.
:ROFLMAO:

Sub Macro123()
Set obFSO = CreateObject("Scripting.FileSystemObject")
Set mFolder = obFSO.GetFolder("C:\Temp\")

'temp = mainFolder.Files.Count
For Each MFile In mFolder.Files
MsgBox MFile.Name
Next

Set obFSO = Nothing
Set mFolder = Nothing

End Sub


if I use temp = mainFolder.Files.Count
And Count returns number 5 - meaning there are 5 files in the folder.

My question is: How I can using Msgbox to show only the last file?
I tryed something like MsgBox MFile(temp).Name but it does not work :(

Please help,
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
I dare say there's probably a way to reference the last file by it's count in a directory, though this will do the same (albeit less efficiently):

Code:
Option Explicit
Sub Macro2()

    Dim objFSO As Object, _
        objFSOFolder As Object
    Dim varFile As Variant, _
        varFileLast As Variant

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFSOFolder = objFSO.GetFolder("P:\")
    
    For Each varFile In objFSOFolder.Files
        varFileLast = varFile
    Next varFile
    
    MsgBox varFileLast 'Filename and path
    MsgBox objFSO.GetFileName(varFileLast) 'Filename only
    
    Set objFSO = Nothing
    Set objFSOFolder = Nothing

End Sub

HTH

Robert
 
Upvote 0
Thank you Robert,
<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
Your solution works perfectly no questions, and I will use it for time being. BIG THANK YOU.
<o:p></o:p>
But, I still think, running For Next operator is not that efficient as just referring to the last file.<o:p></o:p>
 
Upvote 0
But, I still think, running For Next operator is not that efficient as just referring to the last file

Agreed - but, like you, I couldn't find a way to open the file in that manner.
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

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