Hi, Need some help with my VBA as I'm new to all this.
I have so far made some code which will look at a folder, that is selected in cell B1, and then create a list of all the folder names inside that folder and then turn all of the names into hyperlinks to the folder in question. The problem comes when I change folders which will happen every month.
Is it possible to output this list of folders to a pre-defined cell that could be set in excel, say cell D1, so that when the folder is changed every month they will not overwrite the previous months folder names. I have tried this twice but failed each time.
Thanks for any help in advance.
Below is the code I have written so far.
Sub Example1()
Dim wb As Workbook
Dim WS As Worksheet
Dim objFSO As Object
Dim objFolder As Object
Dim objSubFolder As Object
Dim i As Integer
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.StatusBar = ""
Set wb = ThisWorkbook
Set wsControl = wb.Sheets("Control")
Folder_Name = wsControl.Cells(1, 2)
If Folder_Name = "" Then
MsgBox "Path location is not entered. Please enter path"
wsControl.Cells(1, 2).Select
End
End If
'Create an instance of the FileSystemObject
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Get the folder object
Set objFolder = objFSO.GetFolder(Folder_Name)
i = 1
'loops through each file in the directory
For Each objSubFolder In objFolder.subfolders
'select cell
Range(Cells(i + 1, 1), Cells(i + 1, 1)).Select
'create hyperlink in selected cell
ActiveSheet.Hyperlinks.Add Anchor:=Selection, Address:= _
objSubFolder.Path, _
TextToDisplay:=objSubFolder.Name
i = i + 1
Next objSubFolder
Columns("A:A").Select
ActiveWorkbook.Worksheets("Control").sort.SortFields.Clear
ActiveWorkbook.Worksheets("Control").sort.SortFields.Add Key:=Range("A1"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveWorkbook.Worksheets("Control").sort
.SetRange Range("A2:A1000")
.Header = xlNo
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
End Sub
Bookmarks