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:
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.
Thanks.
Your knowledge and insight lead me to solve my dilemma. I can now load into an Image Control the newest jpg file in a directory with one button (the "search" button) and with another two buttons ("-" and "+") load live from the oldest to newest jpg file into an Image Control and vice versa.

This is all thanks too three One Dimensional Dynamic Arrays:
One Array (seen above (with a few tweaks and global variables removed)) that loads the file names into an Array, places the file names on a sheet and sorts the file names in descending order with the spreadsheet sort (will in the future try to sort the Array items without the sheet).
Second and third Array, populates the sorted file names from the sheet in an Array and check, with the help of the Me.ImageControl.Tag method, whether the path and file name is equal to the file already loaded in the Image Control, before subtracting or adding to an Array item, If needed before loading it with
Code:
Me.ImageControl.Picture = LoadPicture (Environ("UserFile") & "\Pictures\Camera Roll\" & Array(item - 1))

Thank you again.:biggrin:
 
Last edited:
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Thanks.
Your knowledge and insight lead me to solve my dilemma. I can now load into an Image Control the newest jpg file in a directory with one button (the "search" button) and with another two buttons ("-" and "+") load live from the oldest to newest jpg file into an Image Control and vice versa.

This is all thanks too three One Dimensional Dynamic Arrays:
One Array (seen above (with a few tweaks and global variables removed)) that loads the file names into an Array, places the file names on a sheet and sorts the file names in descending order with the spreadsheet sort (will in the future try to sort the Array items without the sheet).
Second and third Array, populates the sorted file names from the sheet in an Array and check, with the help of the Me.ImageControl.Tag method, whether the path and file name is equal to the file already loaded in the Image Control, before subtracting or adding to an Array item, If needed before loading it with
Code:
Me.ImageControl.Picture = LoadPicture (Environ("UserFile") & "\Pictures\Camera Roll\" & Array(item - 1))

Thank you again.:biggrin:
Glad to help,
Regards, JLG
 
Upvote 0

Forum statistics

Threads
1,223,912
Messages
6,175,340
Members
452,637
Latest member
Ezio2866

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