+ Reply to Thread
Results 1 to 2 of 2

Copy data from one worksheet to another based on date

Hybrid View

  1. #1
    Registered User
    Join Date
    07-08-2008
    Location
    Arkansas
    Posts
    32

    Copy data from one worksheet to another based on date

    I am using Ron De Bruin's macro shown below to copy data from one worksheet to another and it works well until the operator has a problem, runs the macro again, and the data gets copied twice, then I have duplicate data. What I need to know, can the code be adapted or a new macro to only copy today's data and if run again it would not add today's data twice to the other worksheet.

    Is this possible? Thank you for your help!



    Sub Copy_To_Another_Workbook() 
        Dim SourceRange As Range 
        Dim DestRange As Range 
        Dim DestWB As Workbook 
        Dim DestSh As Worksheet 
        Dim LR As Long 
        Dim rCell As Range 
        Dim rChange As Range 
         
         
         
        With Application 
            .ScreenUpdating = False 
            .EnableEvents = False 
        End With 
         
         'Change the file name (2*) and the path/file name to your file
        If bIsBookOpen_RB("Shipping Manifest Database.xlsm") Then 
            Set DestWB = Workbooks("Shipping Manifest Database.xlsm") 
        Else 
            Set DestWB = Workbooks.Open("G:\CSA\Shipping Data\Shipping Manifest Database.xlsm") 
        End If 
         
         'Change the Source Sheet and range
        Set SourceRange = ThisWorkbook.Sheets("4 PM").Range("I6:N150") 
         'Change the sheet name of the database workbook
        Set DestSh = DestWB.Worksheets("Shipping Data") 
         
        LR = LastRow(DestSh) 
        Set DestRange = DestSh.Range("A" & LR + 1) 
         
         'We make DestRange the same size as SourceRange and use the Value
         'property to give DestRange the same values
        With SourceRange 
            Set DestRange = DestRange.Resize(.Rows.Count, .Columns.Count) 
        End With 
        DestRange.Value = SourceRange.Value 
         
        DestWB.Close savechanges:=True 
         
        With Application 
            .ScreenUpdating = True 
            .EnableEvents = True 
        End With 
    End Sub 
    Function LastRow(sh As Worksheet) 
        On Error Resume Next 
        LastRow = sh.Cells.Find(What:="*", _ 
        After:=sh.Range("A1"), _ 
        LookAt:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        SearchOrder:=xlByRows, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Row 
        On Error GoTo 0 
    End Function 
    Function LastCol(sh As Worksheet) 
        On Error Resume Next 
        LastCol = sh.Cells.Find(What:="*", _ 
        After:=sh.Range("A1"), _ 
        LookAt:=xlPart, _ 
        LookIn:=xlFormulas, _ 
        SearchOrder:=xlByColumns, _ 
        SearchDirection:=xlPrevious, _ 
        MatchCase:=False).Column 
        On Error GoTo 0 
    End Function 
    Function bIsBookOpen_RB(ByRef szBookName As String) As Boolean 
         ' Rob Bovey
        On Error Resume Next 
        bIsBookOpen_RB = Not (Application.Workbooks(szBookName) Is Nothing) 
    End Function

  2. #2
    Registered User
    Join Date
    07-08-2008
    Location
    Arkansas
    Posts
    32

    Re: Copy data from one worksheet to another based on date

    I found another code from Ron De Bruin that deletes the entire row if the date equal today so this work for me.

    Thanks to everyone who looked at this post and a big thank you to Ron De Bruin for the code shown below, this code and the one on the original post work flawlessly.

    Sub Loop_Example2()
        Dim Firstrow As Long
        Dim Lastrow As Long
        Dim Lrow As Long
        Dim CalcMode As Long
        Dim ViewMode As Long
    
        With Application
            CalcMode = .Calculation
            .Calculation = xlCalculationManual
            .ScreenUpdating = False
        End With
    
        'We use the ActiveSheet but you can replace this with
        'Sheets("MySheet")if you want
        
        With Workbooks("UPS Database.xlsx").Sheets("UPS Data")
    
            'We select the sheet so we can change the window view
            .Select
    
            'If you are in Page Break Preview Or Page Layout view go
            'back to normal view, we do this for speed
            ViewMode = ActiveWindow.View
            ActiveWindow.View = xlNormalView
    
            'Turn off Page Breaks, we do this for speed
            .DisplayPageBreaks = False
    
            'Set the first and last row to loop through
            
            Firstrow = 2
            Lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    
    
            'Firstrow = .UsedRange.Cells(1).Row
            'LastRow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    
            'We loop from Lastrow to Firstrow (bottom to top)
            For Lrow = Lastrow To Firstrow Step -1
    
                'We check the values in the A column in this example
                With .Cells(Lrow, "A")
    
                    If Not IsError(.Value) Then
    
                        If .Value = Date Then .EntireRow.Delete 'Date example #8/28/2015#
                        'This will delete each row with the Value "ron"
                        'in Column A, case sensitive.
    
                    End If
    
                End With
    
            Next Lrow
    
        End With
    
        ActiveWindow.View = ViewMode
        With Application
            .ScreenUpdating = True
            .Calculation = CalcMode
        End With
    
    End Sub

+ 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. Copy data from one worksheet to another based on date
    By tgallo307 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-14-2014, 08:08 AM
  2. Copy data from another worksheet based on today date.
    By aneshdas in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 12-07-2013, 02:31 AM
  3. Copy a row from worksheet to another based on date occurring
    By Ideal Partner in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 06-13-2013, 07:44 AM
  4. Copy Paste row from one worksheet to another based on DATE
    By niketmohan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-21-2011, 08:29 AM
  5. How to Copy Row Into Different Worksheet Based on Date Criteria?
    By geli7 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-24-2011, 01:37 PM
  6. Copy row based on date and then paste into a new worksheet based on section number
    By calmlaunch in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-25-2011, 11:40 PM
  7. Copy row to a worksheet based on date.
    By Lasant in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 07-20-2009, 05:25 PM

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