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:
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.
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: