+ Reply to Thread
Results 1 to 5 of 5

Copying and inserting data from one worksheet to another based on Date Range

  1. #1
    Registered User
    Join Date
    05-15-2019
    Location
    Victoria, Australia
    MS-Off Ver
    365
    Posts
    3

    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 LonglastTargetRow As LonglastCol As LongtRow As Long
        Dim source 
    As Stringtarget As String
        Dim ThisValue 
    As Date
        
         source 
    "Final"
         
    target "Template"
         
         
    FinalRow Sheets(source).Range("F" Rows.Count).End(xlUp).Row
         lastCol 
    Sheets(source).Cells(1Columns.Count).End(xlToLeft).Column
         lastTargetRow 
    Sheets(target).Range("F" Rows.Count).End(xlUp).Row
         tRow 
    lastTargetRow 1
        
    For lRow 2 To FinalRow
         ThisValue 
    Sheets(source).Cells(lRow7).Value
                  
            
    If ThisValue tempdate Then
             
    For lCol 1 To lastCol
                    Sheets
    (target).Cells(tRowlCol).Value Sheets(source).Cells(lRowlCol).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 Dateenddate As Date
        Dim rng 
    As RangedestRow As LongAs Longlastrow As Long
        Dim shtSrc 
    As WorksheetshtDest As Worksheet
        Dim c 
    As Range
        

        Set shtSrc 
    Sheets("Final")
        
    Set shtDest Sheets("Template")
        
    Set c shtSrc.Range("A1:F400")
        
        
    lastrow shtSrc.Cells(Rows.Count1).End(xlUp).Row
        
        destRow 
    6
        startdate 
    CDate(InputBox("Begining Date"))
        
    enddate CDate(InputBox("End Date"))
        
        For 
    2 To lastrow
        
        Set rng 
    Application.Intersect(shtSrc.Range("F1"), shtSrc.UsedRange)
        

        For 
    Each c In rng.Cells
            
    If c.Value >= startdate And c.Value <= enddate Then
            destRow 
    shtDest.Cells(Rows.Count1).EndOffset(10).Row
            
            
                
                c
    .Offset(00).Resize(16).Copy shtDest.Cells(destRow7)
                
                                             
            
    destRow destRow 1
            
                
            
    'shtSrc.Range(Cells(i, 1), Cells(i, 6)).Copy
            
            '
    shtDest.Activate
            
            
    'shtDest.Cells(destRow + 1, 1).Insert Shift:=xlDown
                    
            
            

            End If
        Next
        Next
        Application.CutCopyMode = False
        

    End Sub 
    Thanks guys.
    Attached Files Attached Files

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Copying and inserting data from one worksheet to another based on Date Range

    Please Login or Register  to view this content.
    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.

  3. #3
    Forum Expert dangelor's Avatar
    Join Date
    09-06-2011
    Location
    Indiana, USA
    MS-Off Ver
    365 Pro Plus
    Posts
    2,274

    Re: Copying and inserting data from one worksheet to another based on Date Range

    Possibly...
    Please Login or Register  to view this content.

  4. #4
    Registered User
    Join Date
    05-15-2019
    Location
    Victoria, Australia
    MS-Off Ver
    365
    Posts
    3

    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?

  5. #5
    Registered User
    Join Date
    05-15-2019
    Location
    Victoria, Australia
    MS-Off Ver
    365
    Posts
    3

    Re: Copying and inserting data from one worksheet to another based on Date Range

    Thanks for the response, This one didn't seem to work for me. Have tried to manipulate, but to no avail. Thanks for trying though.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 5
    Last Post: 06-07-2016, 12:57 PM
  2. [SOLVED] copying range based on date
    By freshfruit in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-29-2014, 01:49 AM
  3. [SOLVED] Copying data from one worksheet to another based on date
    By Siimo in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-09-2013, 06:53 PM
  4. Copying row (within specified range of date) to another worksheet
    By dinosaur1993 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-07-2013, 05:32 AM
  5. Replies: 1
    Last Post: 05-13-2013, 09:25 PM
  6. Copying data from one sheet to another based on variable date range
    By Colin1226 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 08-08-2010, 02:38 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1