Copying and inserting data from one worksheet to another based on Date Range
Hi Guys, First time here, I have been working with Macro's for a while and this one for a couple of months on and off trying to perfect! Cannot seem to get it to work, so trying here.
I will attach the worksheet.
Basically what I have is 3 worksheets. (Sheet2, Final & Template)
"Sheet 2" holds all the data which when connected to our company VPN can be updated to include latest data from server.
"Final" is a sheet the is created each week by a Macro which is working fine and sorts out the unnecessary columns. Leaving 6 columns of data of which column F has dates in it. (The range where I need to search to match the data)
"Template" is exactly that, a sheet that has standard info in it that is filled out by the staff each week. (This is also where I need the data from the "Final" Worksheet to end up based on dates.
The staff enter the Week ending date in Cell B3 in the "Template" Worksheet.
What I am trying to achieve is once the date is entered, staff will press a button and the macro will search column "F" in the "Final" Worksheet for any dates matching that date as well as the previous 5 days (Working week) and then copy those rows with the correct date range and Insert that data into the "Template" worksheet beginning at Row 5. (Have some headings above this).
Seems simple, and have found a number of ways which come close, but for some reason can't seem to get it to work. Any help would be appreciated. Here are a couple of codes I have tried so far.
PHP Code:
Sub Copyfinal()
Dim FinalRow As Long, lastTargetRow As Long, lastCol As Long, tRow As Long Dim source As String, target As String Dim ThisValue As Date
If ThisValue = tempdate Then For lCol = 1 To lastCol Sheets(target).Cells(tRow, lCol).Value = Sheets(source).Cells(lRow, lCol).Value Next lCol tRow = tRow + 1 End If Next lRow
End Sub
Here is the second - I was trying it with input boxes and a different way. This one came close but didn't insert, just copied in the wrong places.
PHP Code:
Sub Copy_Click()
Dim startdate As Date, enddate As Date Dim rng As Range, destRow As Long, i As Long, lastrow As Long Dim shtSrc As Worksheet, shtDest As Worksheet Dim c As Range
Set shtSrc = Sheets("Final") Set shtDest = Sheets("Template") Set c = shtSrc.Range("A1:F400")
Surround your VBA code with CODE tags e.g.;
[CODE]your VBA code here[/CODE]
The # button in the forum editor will apply CODE tags around your selected text.
Re: Copying and inserting data from one worksheet to another based on Date Range
Thank you so much. I didn't get it to enter into Row 5 and insert, however, have changed the template to allow it in at the bottom.
Just one question, It seems to freeze for a very long time? Is there something that may be causing this? Do I need to have it end, or empty clipboard?
Bookmarks