Hi All,
I have some code i'm using for another macro but trying to modify it for a similar different macro.
The below macro asked the user to select a folder which contained multiple project files and then consolidate data within all those files.
I now need the code to allow the user to select the specific file rather than a group of files in a folder.
Sub CONSOLIDATE()
Dim Path As String ' Set parameters
Dim FileName As String
Dim MyArray(3) As Variant
Dim Sh As Worksheet
Dim i As Integer
Const MainSh As String = "Projects"
Dim LR As Long
With Application.FileDialog(msoFileDialogFolderPicker) ' Select folder where project files are saved
.ButtonName = "OK"
.Title = "Select folder containing files to consolidate"
.AllowMultiSelect = False
.Show
On Error Resume Next
Path = .SelectedItems(1)
Err.Clear
On Error GoTo 0
End With
If Path = Empty Then MsgBox "Macro cancelled.": Exit Sub ' If message box is cancelled exit macro
If Right(Path, 1) <> "\" Then Path = Path & "\"
FileName = Dir(Path & "*.xlsx")
Do While FileName <> ""
Workbooks.Open FileName:=Path & FileName, ReadOnly:=True
For Each Sh In ActiveWorkbook.Worksheets
If Sh.Range("A3").Value = "Project Title" Then 'If cell A3 contains the text "Project Title" then copy data from that sheet
MyArray(0) = Sh.Range("F4").Value
MyArray(1) = Sh.Range("C3").Value
MyArray(2) = Sh.Range("F3").Value
LR = ThisWorkbook.Sheets(MainSh).Range("A" & Rows.Count).End(xlUp).Row + 1 ' calculate the last row of the data range
For i = 0 To 3 ' Number of columns in the range
ThisWorkbook.Sheets(MainSh).Cells(LR, i + 1).Value = MyArray(i)
Next i
ThisWorkbook.Sheets(MainSh).Hyperlinks.Add _
Anchor:=ThisWorkbook.Sheets(MainSh).Range("A" & LR), Address:=Path & FileName ' Add hyperlinks to column A to link to project files
End If
Next Sh
ActiveWorkbook.Close False
FileName = Dir()
Loop
End Sub
Bookmarks