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:
***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.
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.