msquared99
Member
OK, I have this code that loops through several workbooks in a folder. The search range is dynamic. The issue is that the data returned from the SourceRange is coming from outside the SourceRange.
The way each workbook is laid out is there is a date in column A that begins in row 24. This range is dynamic and can span to row 30 something or more. Also, below this range there are 4 blank rows then more dates that would overlap the SourceRange.
When the matching date is found I am returning cell A2, which has the name of the workbook in it, and cells from columns A (matching date), G ($ value), and I ($ value).
An example of what is happening with workbooks that have a matching date:
Match workbook 1:
SourceRange: A24:I33
Return cells are in row 52, the date does not match.
Match workbook 2:
SourceRange: A24:I34
Return cells are in row 56, the date does not match.
Match workbook 3:
SourceRange: A24:I35
Return cells are in row 50, the date does not match.
The way each workbook is laid out is there is a date in column A that begins in row 24. This range is dynamic and can span to row 30 something or more. Also, below this range there are 4 blank rows then more dates that would overlap the SourceRange.
When the matching date is found I am returning cell A2, which has the name of the workbook in it, and cells from columns A (matching date), G ($ value), and I ($ value).
An example of what is happening with workbooks that have a matching date:
Match workbook 1:
SourceRange: A24:I33
Return cells are in row 52, the date does not match.
Match workbook 2:
SourceRange: A24:I34
Return cells are in row 56, the date does not match.
Match workbook 3:
SourceRange: A24:I35
Return cells are in row 50, the date does not match.
Code:
Option Explicit
Sub ExtractionMacro()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim NRow As Long, LastRow As Long
Dim FileName As String
Dim WorkBk As Workbook
Dim DestRange As Range, FindRange As Range, i As Range, SourceRange As Range
Dim FindVal As Date
'Enter a date
FindVal = Application.InputBox("Please enter a date as MM/DD/YYYY.", "Macro Canceled")
If FindVal = False Then
MsgBox "Macro was cancelled.", 64, "Cancel was clicked."
Exit Sub
End If
' Create a new workbook and set a variable to the first sheet.
Set SummarySheet = Workbooks.Add(xlWBATWorksheet).Worksheets(1)
' This is the folder path to point to the files you want to use.
FolderPath = "C:\Test Files\"
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = 1
' Call Dir the first time, pointing it to all Excel files in the folder path.
FileName = Dir(FolderPath & "*.xl*")
' Loop until Dir returns an empty string.
Do While FileName <> ""
' Open a workbook in the folder
Set WorkBk = Workbooks.Open(FolderPath & FileName)
LastRow = WorkBk.Worksheets(1).Range("A24").End(xlDown).Row
' Set the source range to be A24 through I and LastRow.
Set SourceRange = WorkBk.Worksheets(1).Range("A24:I" & LastRow)
' Set the destination range to start at column B and
' be the same size as the source range.
Set DestRange = SummarySheet.Range("B" & NRow)
' Copy the values from the source to the destination.
For Each i In SourceRange
If i.Value = FindVal Then
'Set the cell in column A to be the file name.
DestRange.Cells(NRow, 1).Value = WorkBk.Worksheets(1).Cells(2, 1).Value
DestRange.Cells(NRow, 2).Value = SourceRange.Cells(i.Row, 1).Value
DestRange.Cells(NRow, 3).Value = SourceRange.Cells(i.Row, 7).Value
DestRange.Cells(NRow, 4).Value = SourceRange.Cells(i.Row, 9).Value
NRow = NRow + 1
End If
Next
' Close the source workbook without saving changes.
WorkBk.Close savechanges:=False
' Use Dir to get the next file name.
FileName = Dir()
LastRow = Empty
Loop
' Call AutoFit on the destination sheet so that all
' data is readable.
SummarySheet.Columns.AutoFit
End Sub