modified code and restrict splitting files each 10 files in each column

Alaa mg

Active Member
Joined
May 29, 2021
Messages
378
Office Version
  1. 2019
hi
I have this code it brings all information from the subfolders & files , then create hyperlink and open it in two columns A,B , but what I look for it . it should split each 10 file in specific column individually . so when bring the subfolders under each of them should split each 10 files in separately column with the same borders and formatting as in my picture .
I hope my idea is clear
VBA Code:
Public oldNR As Long
Sub HyperlinkDirectory()

Dim fPath As String
Dim fType As String
Dim fname As String
Dim NR As Long
Dim AddLinks As Boolean

'Select folder
    With Application.FileDialog(msoFileDialogFolderPicker)
        .AllowMultiSelect = False
         .InitialFileName = "C:\Users\OSE\Desktop\11\"
        .Show
        If .SelectedItems.Count > 0 Then
            fPath = .SelectedItems(1) & "\"
        Else
            Exit Sub
        End If
    End With

'Types of files
    fType = Application.InputBox("What kind of files? Type the file extension to collect" _
            & vbLf & vbLf & "(Example:  xls, doc, txt, pdf, *)", "File Type", "PDF", Type:=2)
    If fType = "False" Then Exit Sub

'Option to create hyperlinks
    AddLinks = MsgBox("Add hyperlinks to the file listing?", vbYesNo) = vbYes

'Create report
    Application.ScreenUpdating = False
    NR = 4
    With ActiveSheet
        .Range("A:C").Clear
        .[A2] = "LIST OF FILES"
        .[B2] = "Modified Date"

        Call FindFilesAndAddLinks(fPath, fType, NR, AddLinks)

        End With
        With ActiveSheet
          .Range("A:B").Columns.AutoFit
          .Range("B:B").HorizontalAlignment = xlCenter

        Range("B:B").Select
        Selection.NumberFormat = "d-mmm-yy  h:mm AM/pm"
        End With

        With ActiveSheet
        Range("A2").Select
        Selection.Font.Bold = True
        Range("B2").Select
        Selection.Font.Bold = True
        Columns("A:A").Select
        Selection.Font.Underline = xlUnderlineStyleNone
    End With

    Application.ScreenUpdating = True

End Sub

Private Sub FindFilesAndAddLinks(fPath As String, fType As String, ByRef NR As Long, AddLinks As Boolean)
Dim fname As String
Dim oFS As New FileSystemObject
Dim oDir


'Files under current dir
fname = Dir(fPath & "*." & fType)
With ActiveSheet

    'Write folder name
    .Range("A" & NR) = fPath
    .Range("A" & NR).Select
    If AddLinks Then .Hyperlinks.Add Anchor:=Selection, _
           Address:=fPath, _
            TextToDisplay:="FOLDER NAME:  " & "  " & UCase(Split(fPath, "\")(UBound(Split(fPath, "\")) - 1))
             Selection.Font.Bold = True
             Selection.Font.Size = 10
             Selection.Font.Name = "Arial"
             Selection.Font.Underline = xlUnderlineStyleNone
             With Selection.Interior
        .Pattern = xlSolid
        .PatternColorIndex = xlAutomatic
        .ThemeColor = xlThemeColorAccent3
        .TintAndShade = 0.399975585192419
        .PatternTintAndShade = 0
    End With

    With Selection.Font
        .ThemeColor = xlThemeColorLight1
        .TintAndShade = 0
    End With

    NR = NR + 2

    Do While Len(fname) > 0

      'filename
        If .Range("A" & NR) <> "" Then Debug.Print "Overwriting " & NR
        .Range("A" & NR) = fname
              'modified
        .Range("B" & NR) = FileDateTime(fPath & fname)

      'hyperlink
        .Range("A" & NR).Select
        If AddLinks Then .Hyperlinks.Add Anchor:=Selection, _
            Address:=fPath & fname, _
            TextToDisplay:=fname

      'set for next entry
        NR = NR + 1
        fname = Dir
    Loop

    'Files under sub dir
    Set oDir = oFS.GetFolder(fPath)
    For Each oSub In oDir.SubFolders
        NR = NR + 1
        Call FindFilesAndAddLinks(oSub.Path & "\", fType, NR, AddLinks)
    Next oSub
End With

        ActiveWindow.DisplayGridlines = False

End Sub

ff.xlsm
EFGHIJKLMNOP
1
2LIST OF FILESModified DateLIST OF FILESModified DateLIST OF FILESModified Date
3
4ITEMFOLDER NAME: 11ITEMFOLDER NAME: 11ITEMFOLDER NAME: 11
5111.pdf3-Feb-21 9:42 AM121.pdf3-Feb-21 9:42 AM131.pdf3-Feb-21 9:42 AM
6212.pdf3-Feb-21 9:45 AM222.pdf3-Feb-21 9:45 AM222.pdf3-Feb-21 9:45 AM
7313.pdf3-Feb-21 9:47 AM323.pdf3-Feb-21 9:47 AM
8414.pdf3-Feb-21 9:42 AM424.pdf3-Feb-21 9:42 AM
9515.pdf3-Feb-21 9:42 AM525.pdf3-Feb-21 9:42 AM
10616.pdf3-Feb-21 9:42 AM626.pdf3-Feb-21 9:42 AM
11717.pdf3-Feb-21 9:42 AM727.pdf3-Feb-21 9:42 AM
12818.pdf3-Feb-21 9:42 AM828.pdf3-Feb-21 9:42 AM
13919.pdf4-Feb-21 9:42 AM929.pdf4-Feb-21 9:42 AM
141020.pdf5-Feb-21 9:42 AM1030.pdf5-Feb-21 9:42 AM
15
16ITEMFOLDER NAME: AAITEMFOLDER NAME: AAITEMFOLDER NAME: AA
171A11.pdf3-Feb-21 9:42 AM1A21.pdf3-Feb-21 9:42 AM1A31.pdf3-Feb-21 9:42 AM
182A12.pdf3-Feb-21 9:45 AM2A22.pdf3-Feb-21 9:45 AM
193A13.pdf3-Feb-21 9:48 AM3A23.pdf3-Feb-21 9:48 AM
204A14.pdf3-Feb-21 9:50 AM4A24.pdf3-Feb-21 9:50 AM
215A15.pdf3-Feb-21 9:53 AM5A25.pdf3-Feb-21 9:53 AM
226A16.pdf3-Feb-21 9:56 AM6A26.pdf3-Feb-21 9:56 AM
237A17.pdf3-Feb-21 9:58 AM7A27.pdf3-Feb-21 9:58 AM
248A18.pdf3-Feb-21 10:01 AM8A28.pdf3-Feb-21 10:01 AM
259A19.pdf3-Feb-21 10:04 AM9A29.pdf3-Feb-21 10:04 AM
2610A20.pdf3-Feb-21 10:06 AM10A30.pdf3-Feb-21 10:06 AM
27
SHEET
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,224,824
Messages
6,181,186
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