+ Reply to Thread
Results 1 to 3 of 3

Excel 2007 : How to select randomly 5 files from a folder/directory

Hybrid View

  1. #1
    Registered User
    Join Date
    08-08-2008
    Location
    Johannesburg
    MS-Off Ver
    2007
    Posts
    55

    How to select randomly 5 files from a folder/directory

    Hi Guys
    I'm using this macro to create a list of all files in a specific folder
    Sub BuildListOf_Files_to_Upload()
    
    Extension = "*.jpg"
    Folder = Range("C8").Value '"C:\images folder"
    First = True
    RowCount = 1
    Do
    If First = True Then
    fName = Dir(Folder & "\" & Extension)
    First = False
    Else
    fName = Dir()
    End If
    If fName <> "" Then
    Range("N" & RowCount + 1) = fName
    RowCount = RowCount + 1
    End If
    Loop While fName <> ""
    End Sub
    How can I improve the code so the macro will list 5 files that selected randomly?

    Many thanks for your help.
    Amit.
    Last edited by furiousfox; 11-03-2011 at 02:56 AM. Reason: Solved
    Using Excel 2007.
    Amit Cohen

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: How to select randomly 5 files from a folder/directory

    Amit,

    If you want the output to be 5 random files instead of all of them, you can use the following:
    Sub BuildListOf_Files_to_Upload()
        
        Const strExt As String = "*.jpg"
        
        Dim strFldr As String: strFldr = Range("C8").Text & "\" '"C:\images folder"
        Dim fName As String: fName = Dir(strFldr & strExt)
        Dim arrFiles() As String
        Dim arrIndex As Long, arrChk As Long
        Dim arrRandFiles(1 To 5) As String
        
        While fName <> vbNullString
            arrIndex = arrIndex + 1
            ReDim Preserve arrFiles(1 To arrIndex)
            arrFiles(arrIndex) = fName
            fName = Dir
        Wend
        
        Randomize
        For arrIndex = 1 To 5
            arrRandFiles(arrIndex) = arrFiles(WorksheetFunction.RandBetween(1, UBound(arrFiles)))
            For arrChk = 1 To arrIndex - 1
                If arrRandFiles(arrChk) = arrRandFiles(arrIndex) Then arrIndex = arrIndex - 1
            Next arrChk
        Next arrIndex
        
        Range("N1").Resize(5).Value = WorksheetFunction.Transpose(arrRandFiles)
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    08-08-2008
    Location
    Johannesburg
    MS-Off Ver
    2007
    Posts
    55

    Re: How to select randomly 5 files from a folder/directory

    Hi tigeravatar

    This is awesome,
    Works like a charm.

    Many thanks for your help.

    Amit

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1