简体   繁体   中英

Copy rows from multiple workbooks based on cell value

How do I copy cells from a workbook based on a cell-value.

In the excel file that needs to be filled, column B contains a part of a filename in which the data can be found.

B2 contains 312123-145

B3 contains 312123-195

etc, 

normally around 18rows, but the loop/step can go until a empty cell if found

The workbook the data contains is the cell-value.xlsm There is a sheet with name Yield in the 312123-145.xlsm From that sheet I would like to copy A2:N2, and paste that data at the main excel sheet, in columns E:R in the corrosponding row.

I don't know how to start on to look-up a cell value and find the file with the correct data and then how do I step to the next row.

This the code I started with, I hardcoded first all cellvalues to keep it simple.

Sub ImportWorksheet() 
    ' This macro will import a file into this workbook 
    Sheets("Sheet1").Select 
    PathName = "C:\Documents\test\"
    Filename = "312123-195" 
    TabName = "Yield"
    ControlFile = ActiveWorkbook.Name 
    Workbooks.Open Filename:=PathName & Filename 
    ActiveSheet.Name = TabName 
    Sheets(TabName).Copy After:=Workbooks(ControlFile).Sheets(1) 
    Windows(Filename).Activate 
    ActiveWorkbook.Close SaveChanges:=False 
    Windows(ControlFile).Activate 
End Sub

Import Data From Closed Workbooks

Option Explicit

Sub ImportData()
    
    Dim sFolderPath As String: sFolderPath = "C:\Documents\test\"
    Dim sFileExtension As String: sFileExtension = ".xlsm" ' ".xls*"
    Const sName As String = "Yield"
    Const srgAddress As String = "A2:N2"
    Const sFileDelimiter As String = "-"
    
    Const dName As String = "Sheet1"
    Const dlCol As String = "B" ' Lookup
    Const dvCol As String = "E" ' Value
    Const dfRow As Long = 2
    
    If Right(sFolderPath, 1) <> "\" Then sFolderPath = sFolderPath & "\"
    If Left(sFileExtension, 1) <> "." Then sFileExtension = "." & sFileExtension
    
    Dim dwb As Workbook: Set dwb = ThisWorkbook ' workbook containing this code
    
    Dim dws As Worksheet: Set dws = dwb.Worksheets(dName)
    Dim dlRow As Long: dlRow = dws.Cells(dws.Rows.Count, dlCol).End(xlUp).Row
    If dlRow < dfRow Then Exit Sub ' no data
    Dim dlrg As Range
    Set dlrg = dws.Range(dws.Cells(dfRow, dlCol), dws.Cells(dlRow, dlCol))
    Dim dcCount As Long: dcCount = dws.Range(srgAddress).Columns.Count
    Dim dvrg As Range: Set dvrg = dws.Cells(dfRow, dvCol).Resize(, dcCount)
    
    Dim swb As Workbook
    Dim sws As Worksheet
    Dim sFilePattern As String
    Dim sFileName As String
    Dim dlCell As Range
    Dim swsCount As Long
    
    Application.ScreenUpdating = False
    
    For Each dlCell In dlrg.Cells
        sFilePattern = sFolderPath & Left(CStr(dlCell.Value), _
            InStr(1, CStr(dlCell.Value), sFileDelimiter, vbTextCompare) - 1) _
            & sFileExtension
        sFileName = Dir(sFilePattern)
        If Len(sFileName) > 0 Then ' file (workbook) exists
            Set swb = Workbooks.Open(sFolderPath & sFileName)
            On Error Resume Next
                Set sws = swb.Worksheets(sName)
            On Error GoTo 0
            If Not sws Is Nothing Then ' worksheet exists
                dvrg.Value = sws.Range(srgAddress).Value
                swsCount = swsCount + 1
            End If
            swb.Close SaveChanges:=False
        End If
        Set dvrg = dvrg.Offset(1)
    Next dlCell
    
    Application.ScreenUpdating = True
    
    Select Case swsCount
    Case 0
        MsgBox "No data imported", vbCritical
    Case 1
        MsgBox "Data imported from one worksheet.", vbInformation
    Case Else
        MsgBox "Data imported from " & swsCount & " worksheets.", vbInformation
    End Select
    
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM