VBA - Search files in a folder, copy IF problem

JooMa

New Member
Joined
Apr 27, 2017
Messages
1
Hi everyone!
I'm a total VBA newb and I require some professional assistance getting my code to work.
I've searched far and wide with no luck and I'm getting quite desperate.
All kind of help will be greatly appreciated!

In short, I need my code to search through Excel workbooks in a predestined folder and copy rows to my master workbook (where the macro is) if three spesific conditions are being met (start & end date and product number).
This is how far I've gotten modifying different bits and pieces to suit my needs:

Code:
Sub Etsiva_click()

'Macro optimization
    With Application
        .ScreenUpdating = False
        .EnableEvents = False
        .Calculation = xlCalculationManual
    End With

'Sourcefolder and filenames
    Dim FolderPath As String, FileName As String
    FolderPath = "C:\Users\MYUSERNAME\Documents\Projects\Excel\Tests\Data\"
    FileName = Dir(FolderPath & "*.xl*")
    
'Workbooks and sheets
    Dim WorkBk As Workbook
    Dim WorkSht As Worksheet
    Dim TemPest As Worksheet
    Dim ActIon As Worksheet

'Search definitions
    Dim nRows As Long, LastRow As Long
    Dim CurYear As Date, StartDate As Date, EndDate As Date
    Dim ProDuct As String
    
'Count last used row and define starting row
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row And Cells(Rows.Count, 4).End(xlUp).Row
    nRows = 3
    
'More workbooks and sheets
    Set TemPest = ThisWorkbook.Worksheets("Temp")
    Set ActIon = ThisWorkbook.Worksheets("Actions")
    
'Search criteria
    StartDate = ActIon.Range("A2").Value
    EndDate = ActIon.Range("A3").Value
    ProDuct = ActIon.Range("A4").Value
    
'Empty "Reporting" (this workbook) "Temp" -sheet before copying new data
    TemPest.Range("2:7").ClearContents

'Loop and copy
        Do While FileName <> ""
            If FileName <> ThisWorkbook.Name Then
                Set WorkBk = Workbooks.Open(FolderPath & FileName)
                WorkBk.Activate
                Set WorkSht = WorkBk.Worksheets("Sheet1")
                With WorkBk.Worksheets(1)
                    Do
                        If WorkSht.Cells(nRows, 4).Value <= EndDate And WorkSht.Cells(nRows, 4).Value >= StartDate And WorkSht.Cells(nRows, 1).Value = ProDuct Then '***
                            TemPest.Cells(nRows, 1).Value = WorkBk.Worksheets("Sheet1").Cells(nRows, 1).Value   'Product number
                            TemPest.Cells(nRows, 2).Value = WorkBk.Worksheets("Sheet1").Cells(nRows, 2).Value   'Product
                            TemPest.Cells(nRows, 3).Value = WorkBk.Worksheets("Sheet1").Cells(nRows, 3).Value   'Variable
                            TemPest.Cells(nRows, 4).Value = WorkBk.Worksheets("Sheet1").Cells(nRows, 4).Value   'Date
                            TemPest.Cells(nRows, 5).Value = WorkBk.Worksheets("Sheet1").Cells(nRows, 5).Value   'Time
                            TemPest.Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete              'Delete empty rows
                        End If
                    nRows = nRows + 1
                    Loop Until nRows = LastRow + 1
                    WorkBk.Close SaveChanges:=False
                End With
                FileName = Dir
            End If
        Loop

'Macro optimization off
    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = xlCalculationAutomatic
    End With

End Sub

***This row is giving me '1004' Application defined or object defined error and I'm not sure why. There might be some other anomalies in my code too, but I haven't made that far yet.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"

Forum statistics

Threads
1,223,901
Messages
6,175,277
Members
452,629
Latest member
SahilPolekar

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top