I am trying to get VBA to create a list of all the different tabs in different portfolios. The output should be a table with columns as names of tabs and the file dir at the top. I try selecting a folder with all the different files (where also the macro file lies), however, I only get the macro to loop through excels in the folder and does nothing.
Dim MyFolder As String 'Path collected from the folder picker dialog
Dim MyFile As String 'Filename obtained by DIR function
Dim wbkMacro As Workbook 'The current file that the macro is in
Dim wbk As Workbook 'Used to loop through each workbook
Set wbkMacro = ActiveWorkbook
On Error Resume Next
Application.ScreenUpdating = False
'Opens the folder picker dialog to allow user selection
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
MyFile = Dir(MyFolder) 'DIR gets the first file of the folder
'Loop through all files in a folder until DIR cannot find anymore
Do While MyFile <> ""
'Opens the file and assigns to the wbk variable for future use
Set wbk = Workbooks.Open(Filename:=MyFolder & MyFile)
'Replace the line below with the statements you would want your macro to perform
' Dim mainworkBook As Workbook
'Set mainworkBook = ActiveWorkbook
For i = 1 To wbk.Sheets.Count
'Either we can put all names in an array , here we are printing all the names in Sheet 2
wbkMacro.Sheets("Sheet1").Range(“A” & i) = wbk.Sheets(i).Name
Next i
wbk.Close savechanges:=True
MyFile = Dir 'DIR gets the next file in the folder
Loop
Application.ScreenUpdating = True
End Sub```