1
votes

I have list of Excel in same format with different date entry! About 100 excel files, which have different titles. I need to build one master table where I would need to reference some cells from the template used and do some analysis in my master table.

Anyhow, I tried to use the tutorial "How To Reference Or Link Value In Unopened/Closed Excel Workbook File?" to do that and it works fine for closed worksheet!

However, I could not figure way in how to use this tutorial and change the File path dynamically?

Note:

  1. All files in the same folder and every excel sheet have its code and title example: Meal Code-Title.All files have the same structure.

  2. Every Excel have General information in the sheet1, example (Meal Title, Time to prepare,cost, profit, Number of likes, and Number of dislike).The excel sheet have 4 sheets which am not interested on.

  3. I don’t want to merge all files into one file "workbook".I need to have one master excel which do analysis base on the data in the 100 files from Sheet1 (without opening the files).

screenshot

screenshot

Here how I did it using index : =INDEX('D:\Meals[100-Pasta.xlsx]Sheet1'!$B:B,3,1)

Here the formula

2
I think it is important for you to tell us if all the Excel files are within the same folder or within subfolders of the same folder. This information might affect any answer/solution provided. Also, do the names of the 100 Excel files change? Do you have a list of them somewhere? The basic form of a reference to a closed external workbook seems to be 'X[Y]Z'!A, where you would replace X with the just the folder/directory path of the workbook, replace Y with the name of the workbook including extension, replace Z with sheet name and A with range address.chillin
`Welcome to Stack Overflow!" How many worksheets do each workbook have? This doesn't sounds like you just need to copy a worksheet from each file to one workbook. Can you provide an example of the data, before & after? Stack Overflow is a place that developers (of any experience level!) can bring a specific question about a specific issue. It's important to give examples of your research on this issue so far, as well as what you've tried and why it didn't work. See minimal reproducible example and help center and some great tips here.ashleedawg
So you could create the reference as a string dynamically, and then assign it to a range.formula.chillin
@chillin updated !Sabra Al- Busaidi

2 Answers

0
votes

Why do the workbooks have to stay closed? You can easily import data from all workbooks in a folder, into a mater file, which contains everything.

Sub Basic_Example_1()
    Dim MyPath As String, FilesInPath As String
    Dim MyFiles() As String
    Dim SourceRcount As Long, Fnum As Long
    Dim mybook As Workbook, BaseWks As Worksheet
    Dim sourceRange As Range, destrange As Range
    Dim rnum As Long, CalcMode As Long

    'Fill in the path\folder where the files are
    MyPath = "C:\Users\Ron\test"

    'Add a slash at the end if the user forget it
    If Right(MyPath, 1) <> "\" Then
        MyPath = MyPath & "\"
    End If

    'If there are no Excel files in the folder exit the sub
    FilesInPath = Dir(MyPath & "*.xl*")
    If FilesInPath = "" Then
        MsgBox "No files found"
        Exit Sub
    End If

    'Fill the array(myFiles)with the list of Excel files in the folder
    Fnum = 0
    Do While FilesInPath <> ""
        Fnum = Fnum + 1
        ReDim Preserve MyFiles(1 To Fnum)
        MyFiles(Fnum) = FilesInPath
        FilesInPath = Dir()
    Loop

    'Change ScreenUpdating, Calculation and EnableEvents
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'Add a new workbook with one sheet
    Set BaseWks = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
    rnum = 1

    'Loop through all files in the array(myFiles)
    If Fnum > 0 Then
        For Fnum = LBound(MyFiles) To UBound(MyFiles)
            Set mybook = Nothing
            On Error Resume Next
            Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
            On Error GoTo 0

            If Not mybook Is Nothing Then

                On Error Resume Next

                With mybook.Worksheets(1)
                    Set sourceRange = .Range("A1:C1")
                End With

                If Err.Number > 0 Then
                    Err.Clear
                    Set sourceRange = Nothing
                Else
                    'if SourceRange use all columns then skip this file
                    If sourceRange.Columns.Count >= BaseWks.Columns.Count Then
                        Set sourceRange = Nothing
                    End If
                End If
                On Error GoTo 0

                If Not sourceRange Is Nothing Then

                    SourceRcount = sourceRange.Rows.Count

                    If rnum + SourceRcount >= BaseWks.Rows.Count Then
                        MsgBox "Sorry there are not enough rows in the sheet"
                        BaseWks.Columns.AutoFit
                        mybook.Close savechanges:=False
                        GoTo ExitTheSub
                    Else

                        'Copy the file name in column A
                        With sourceRange
                            BaseWks.cells(rnum, "A"). _
                                    Resize(.Rows.Count).Value = MyFiles(Fnum)
                        End With

                        'Set the destrange
                        Set destrange = BaseWks.Range("B" & rnum)

                        'we copy the values from the sourceRange to the destrange
                        With sourceRange
                            Set destrange = destrange. _
                                            Resize(.Rows.Count, .Columns.Count)
                        End With
                        destrange.Value = sourceRange.Value

                        rnum = rnum + SourceRcount
                    End If
                End If
                mybook.Close savechanges:=False
            End If

        Next Fnum
        BaseWks.Columns.AutoFit
    End If

ExitTheSub:
    'Restore ScreenUpdating, Calculation and EnableEvents
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
End Sub

You may also want to consider using the AddIn from the link below.

https://www.rondebruin.nl/win/addins/rdbmerge.htm

enter image description here

0
votes

My answer is untested, as am on mobile. It wasn't clear to me whether you had an existing list of the 100 Excel files, with codes and meals for each one, somewhere (your Master Table image shows only one row/file) -- or whether you wanted to look up all the files.

Code below attempts to look up the files.

Option Explicit

Sub InsertExternalReferences()

' Change if/as needed to the folder of the 100 Excel files. '
Const FOLDER_PATH as string = "D:\Meals"

Dim Filename as string
Filename = dir$(folder_path &"\*-*.xlsx" , vbnormal)

Dim Index as long
Dim FileIndex as long

' Change this line to the name of the sheet that contains the MasterTable -- else you'll get an error. '
With thisworkbook.worksheets("MasterTable")

Do until Len(filename) = 0

FileIndex = FileIndex + 1

' To me, does not make sense to use the INDEX function. It would make sense if you were looking up the value dynamically with a combination of INDEX and MATCH, but you do not appear to be. You may as well just give the cell reference if structure throughout 100 workbooks is not going to change. '

' Reading rows 2 to 8 on each Sheet1'
For Index = 2 to 8

'Index+2 below means we start writing from column 4 (AKA column D).'

'5+ below means we are skipping the first 5 rows on the MasterTable sheet and begin writing from the sixth row. Change it to however many rows you need to skip.'

.cells(5+FileIndex,Index+2).formula = "='" & folder_path & "[" & filename & "]Sheet1'!B" & cstr(Index)

Next index

Filename = dir$()
Loop

End with


End sub

If I have misunderstood, let me know.