GarnesGambit
New Member
- Joined
- Feb 23, 2024
- Messages
- 15
- Office Version
- 365
- Platform
- Windows
Hi all,
I'm new to Macros and learning as I go! I have a great Macro (I found online) that allows me to select a folder and it will import all excel files in this folder and consolidate into one worksheet.
I'm wondering, would it be possible to edit this macro to instead of take files from a folder on my desktop, I can select multiple hyperlinks in my workbook and open/ consolidate those? Example I could select multiple cells in column J of my workbook (this column contains the individual files) and run this code to consolidate?
Essentially I have a master file, where each of my files are hyperlinked and linked to specific stores (some plans are unique, others can belong to multiple stores). The struggle I have is all plans are in 1 folder just now as it's not sustainable to have to save them multiple times in multiple folders by store.
Code below:
Appreciate all help in advance!
I'm new to Macros and learning as I go! I have a great Macro (I found online) that allows me to select a folder and it will import all excel files in this folder and consolidate into one worksheet.
I'm wondering, would it be possible to edit this macro to instead of take files from a folder on my desktop, I can select multiple hyperlinks in my workbook and open/ consolidate those? Example I could select multiple cells in column J of my workbook (this column contains the individual files) and run this code to consolidate?
Essentially I have a master file, where each of my files are hyperlinked and linked to specific stores (some plans are unique, others can belong to multiple stores). The struggle I have is all plans are in 1 folder just now as it's not sustainable to have to save them multiple times in multiple folders by store.
Code below:
VBA Code:
Sub ImportFiles()
Dim strPath As String, xStrPath As String, xStrName As String, xStrFName As String
Dim xWS As Worksheet, xTWB As Workbook, DestSheet As Worksheet, FileName As String
Dim xStrAWBName As String, Sh1 As Worksheet, FolderName As String, sItem As String
Dim FolderPath As String, fldr As FileDialog, Lr As Long, Lc As Long, Lr2 As Long
On Error Resume Next
Set xTWB = ThisWorkbook
Set DestSheet = xTWB.ActiveSheet
Debug.Print DestSheet.Name
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
FolderName = sItem
Set fldr = Nothing
FolderPath = FolderName & "\"
FileName = Dir(FolderPath & "*.xls*")
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.DisplayAlerts = False
Do While FileName <> ""
Workbooks.Open FileName:=FolderPath & FileName, ReadOnly:=True
xStrAWBName = ActiveWorkbook.Name
Set Sh1 = ActiveWorkbook.Sheets("Sheet1")
xStrName = Sh1.Name
For Each xWS In ActiveWorkbook.Sheets
If xWS.Name = xStrName Then
Lr = DestSheet.Range("C" & Rows.Count).End(xlUp).Row
Lr2 = xWS.Range("C" & Rows.Count).End(xlUp).Row
Lc = Cells(1, Columns.Count).End(xlToLeft).Column
If Lr = 1 Then
Range(xWS.Cells(1, 1), xWS.Cells(Lr2, Lc)).Copy DestSheet.Range("C1")
Else
Range(xWS.Cells(2, 1), xWS.Cells(Lr2, Lc)).Copy DestSheet.Range("C" & Lr + 1)
End If
End If
Next xWS
Workbooks(xStrAWBName).Close
FileName = Dir()
Loop
xTWB.Save
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.Calculation = xlCalculationAutomatic
End Sub
Appreciate all help in advance!