TOP

Spider macro for collecting data from various files Excel

YouLibreCalc for Excel logo

Description

In this article, we will consider an example of a macro that can search for data in many workbooks and record the selected information in a separate report file.


To do this, open your book, go to Visual Basic Editor (Alt+F11) , add a new module blank (Insert - Module) and copy this macro text there:

Sub Report_file()
'moonexcel.com.ua
Application.ScreenUpdating = False ' disable screen refresh 

    Set report = Workbooks("Report.xlsb").Worksheets(1)
    find_field = report.[a1]
    
    ' open the dialog for selecting files for import 
    FilesToOpen = Application.GetOpenFilename _
      (FileFilter:="All files (*.*), *.*", _
      MultiSelect:=True, Title:=" Select files! ")

    If TypeName(FilesToOpen) = "Boolean" Then
        MsgBox " No file selected! "
        Exit Sub
    End If
    
    ' we go through all the selected files one by one 
    m = 1
    While m <= UBound(FilesToOpen)
        Set importWB = Workbooks.Open(Filename:=FilesToOpen(m))
        Set importWS = importWB.Worksheets(1)
    
        ' we beat the cells"  hats  " 
        For Each cell2 In report.Range(report.Cells(1, 2), report.Cells(1, report.UsedRange.Columns.Count))
            On Error Resume Next: Err.Clear
            ' looking for meaning in an open book 
            tr = importWS.UsedRange.Find(find_field).Row
            tc = importWS.UsedRange.Find(find_field).Column
            x = importWS.Range(importWS.Cells(tr, tc), importWS.Cells(20000, tc)).Find(report.Cells(m + 1, 1).Value).Row
            y = importWS.UsedRange.Find(cell2.Value).Column
            ' transfer the found values to the report file 
            report.Cells(m + 1, cell2.Column).Value = importWS.Cells(x, y).Value
        Next
        
        importWB.Close savechanges:=False
        m = m + 1
    Wend
         
Application.ScreenUpdating = True
End Sub

After that, you can return to Excel and run our macro through the menu Tools - Macro - Macros (Alt+F8) .