Try something like this:
VBA Code:
Sub MyCopyMacro()
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lr As Long, r As Long, nr As Long
Dim stDate As Date, endDate As Date
Application.ScreenUpdating = False
' Set this original file with data and macro as wb1
Set wb1 = ThisWorkbook
' Set the worksheet with the data you are copying from
'***YOU MAY NEED TO EDIT THIS***
Set ws1 = wb1.Sheets("Sheet3")
' Set the open workbook you are copying to
'***YOU WILL NEED TO EDIT THIS, OR PROMPT THE USER FOR VALUE***
Set wb2 = Workbooks("Book2.xlsx")
' Set the worksheet where you are copying to
'***YOU MAY NEED TO EDIT THIS***
Set ws2 = wb2.Sheets("Sheet1")
' Prompt for start date and end date
stDate = InputBox("Please enter the start date")
endDate = InputBox("Please enter the end date")
' Check date entry
If endDate < stDate Then
MsgBox "Start Date must be prior to End Date", vbOKOnly, "ENTRY ERROR! PLEASE TRY AGAIN!"
Exit Sub
End If
' Find first new row on destination sheet
wb2.Activate
ws2.Activate
nr = Cells(Rows.Count, "B").End(xlUp).Row + 1
' Find last row with data in column B on source sheet
wb1.Activate
ws1.Activate
lr = Cells(Rows.Count, "B").End(xlUp).Row
' Loop through all rows of data starting with row 2
For r = 2 To lr
' See if date in column B falls in our range
If (Cells(r, "B") >= stDate) And (Cells(r, "B") <= endDate) Then
' Copy to destination sheets
Rows(r).Copy
wb2.Activate
Cells(nr, "A").Select
ActiveSheet.Paste
Application.CutCopyMode = False
' Increment next row counter
nr = nr + 1
' Go back to original workbook
wb1.Activate
End If
Next r
Application.ScreenUpdating = True
MsgBox "Macro complete!"
End Sub
Note that I indicated parts of the code that will need you to update with your particular details.
Good evening Joe4,
Thank you for your fast helping.
I try your code to my workbooks, but unfortunately nothing happened.
1st when I select Date range approx 5 day ( than means around 16 or 20 rows) I need to wait during execution of code approx. 3-4min. I don't know why? May b you know better than me.
2nd I created two simply files with same construction like mine ( w/o full data which I have). Unfortunately when I copy your code to my workbooks and execute it I received the MsgBox - Macro Complete , but the data are no copied .
Because I don't know how to use xl2bb add-in I try to explain here.
The file from which I want to copy is " Data from department.xlsx" with Sheet name "Sheet1"
The fiel to which I want to copy is "Whole colleted data.xlsx" with Sheet name "Sheet2"
I paste your code to vba "Whole collected data.xlsx" / Microsoft excel object / sheet2
the fulfilled code is:
Sub MyCopyMacro()
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
Dim lr As Long, r As Long, nr As Long
Dim stDate As Date, endDate As Date
Application.ScreenUpdating = False
' Set this original file with data and macro as wb1
Set wb1 = Workbooks("Data from department.xlsx")
' Set the worksheet with the data you are copying from
'***YOU MAY NEED TO EDIT THIS***
Set ws1 = wb1.Sheets("Sheet1")
' Set the open workbook you are copying to
'***YOU WILL NEED TO EDIT THIS, OR PROMPT THE USER FOR VALUE***
Set wb2 = Workbooks("Whole collected data.xlsx")
' Set the worksheet where you are copying to
'***YOU MAY NEED TO EDIT THIS***
Set ws2 = wb2.Sheets("Sheet2")
' Prompt for start date and end date
stDate = InputBox("Please enter the start date")
endDate = InputBox("Please enter the end date")
' Check date entry
If endDate < stDate Then
MsgBox "Start Date must be prior to End Date", vbOKOnly, "ENTRY ERROR! PLEASE TRY AGAIN!"
Exit Sub
End If
' Find first new row on destination sheet
wb2.Activate
ws2.Activate
nr = Cells(Rows.Count, "B").End(xlUp).Row + 1
' Find last row with data in column B on source sheet
wb1.Activate
ws1.Activate
lr = Cells(Rows.Count, "B").End(xlUp).Row
' Loop through all rows of data starting with row 2
For r = 2 To lr
' See if date in column B falls in our range
If (Cells(r, "B") >= stDate) And (Cells(r, "B") <= endDate) Then
' Copy to destination sheets
Rows(r).Copy
wb2.Activate
Cells(nr, "A").Select
ActiveSheet.Paste
Application.CutCopyMode = False
' Increment next row counter
nr = nr + 1
' Go back to original workbook
wb1.Activate
End If
Next r
Application.ScreenUpdating = True
MsgBox "Macro complete!"
End Sub
Would you help me.
Thank you again.