Does the Upper Bound of an Array of Files Names always represent the Newest File

Hmerman

Board Regular
Joined
Oct 2, 2016
Messages
102
Hello,
I recently started using arrays in vba. My second array is a Single Dimension Dinamic Array that populates the array with a String of jpg file names in a directory. I just have two questions:
1] I noticed in the Local Window that the Upper Bound of the array is the Newest Files (according to date). What I want to know, does the Ubound always represent the Newest File?

2] I am trying to call from the array the secondNewest file on the click of a button and when I click again the thirdNewest file and so on. But I am stuck and get an error when I call the Array minus 2 or 3.
Can someone please assist or give advice?


*The code calling the MostRecentFile I got from the net.
Code:
Code:
Dim secondNewest as String
Dim thirdNewest as String
Option Explicit

Function AllImageNames()
 Dim MostRecentFile As String
    Dim MostRecentDate As Date
    Dim FileName As String
    Dim FileSpec As String
    Dim Directory As String
    Dim CompleteImages() As Variant
    Dim imgNumbers As Long
    
    
    'Specify the file type for images if any
    FileSpec = "*.jpg*"
    
    'specify the directory
    Directory = Environ("UserProfile") & "\Pictures\Camera Roll\"
    FileName = Dir(Directory & FileSpec)
    
If FileName <> "" Then
    Do While FileName <> ""
        imgNumbers = imgNumbers + 1
        FileName = Dir()
    Loop
End If
    imgNumbers = imgNumbers - 1
        
    'Make array dynamic
    ReDim CompleteImages(0 To imgNumbers)
    're-populate empty string
    FileName = Dir(Directory & FileSpec)
    
    'populate array CompleteImages with jpg file strings in Directory
    For imgNumbers = LBound(CompleteImages) To UBound(CompleteImages)
                MostRecentFile = FileName
                MostRecentDate = FileDateTime(Directory & FileName)
                FileName = Dir()
                CompleteImages(imgNumbers) = FileName
    Next imgNumbers
        
          AllImageNames = MostRecentFile
               [COLOR=#ff0000]secondNewest = CompleteImages(imgNumbers - 2).Value[/COLOR]
              thirdNewest = CompleteImages(imgNumbers - 3).Value
               
    
    Erase CompleteImages
End Function

I am using the global variables secondNewest and thirdNewest in another button click sub trying to call the one If the other is not yet in a Image Control.
MostRecentFile is only used to call the NewestFile.
 
Last edited:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
A one dimension array does not have any negative attributes, so all references must be positive. In your case 0 To the number of files less one. The method used to fill the array will put the files in the order that they appear in the directory if you view them in File Explorer, not necessarily in date created order. The files are normally stored in the directory using the windows default of numeric/alpha digital sort. If your file name contains a date and the naming convention is used cosistently by all who save the files, then you might get the array formed in a date sequence, but I would not rely on it.
 
Upvote 0
try secondNewest = CompleteImages(2)

Thanks. Sorry I did not mention that there are 23 files (for now) in the folder, therefore as the base element is 0 I want to call CompleteImages(21). Your suggested syntax did populate the secondNewest string element path and file name 21.
 
Upvote 0
Thanks for letting me know, I too am learning this and in fact watched a video on it today so I'll go back and see where I am going wrong
 
Upvote 0
The method used to fill the array will put the files in the order that they appear in the directory if you view them in File Explorer, not necessarily in date created order. The files are normally stored in the directory using the windows default of numeric/alpha digital sort. If your file name contains a date and the naming convention is used consistently by all who save the files, then you might get the array formed in a date sequence, but I would not rely on it.
This is some very good advice. Thank you.
I played around with the Sort option under View in File Explorer and noticed in the Locals Window that the Array got populated accordingly.
 
Upvote 0
Dryver14: I too am learning this and in fact watched a video on it today so I'll go back and see where I am going wrong
We are both learning. I also watched a vid about a week ago. Great and powerfull stuff these Arrays.
 
Last edited:
Upvote 0
Is there a way mathematically to ask within the rules of the FileDateTime function if a file within my Array is equal to the MostRecentFile - 1?
Therefore giving me the path and file name string to populate the secondNewest file.
 
Last edited:
Upvote 0
...I'll go back and see where I am going wrong
Dryver you are not wrong, your syntax is correct. I did not give the info that there are 23 elements in my Array. Therefore you could not possibly know that the element I was looking for is 22 minus 1.
 
Upvote 0
Dryver you are not wrong, your syntax is correct. I did not give the info that there are 23 elements in my Array. Therefore you could not possibly know that the element I was looking for is 22 minus 1.

If you are going to use a minus or plus value to define the array item, then I suggest you use the LBound or UBound as a base reference point, ie. If your array is named 'ary' then LBound(ary) + a number or UBound(ary) - a number. That way, whether the array is base 0 or base 1 it will still be the same number of items in reference to either end of the array.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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