+ Reply to Thread
Results 1 to 14 of 14

Macro to copy and replace based on dates

Hybrid View

ScabbyDog Macro to copy and replace... 11-16-2012, 12:41 PM
ScabbyDog Re: Macro to copy and replace... 11-16-2012, 02:21 PM
arlu1201 Re: Macro to copy and replace... 11-16-2012, 02:25 PM
ScabbyDog Re: Macro to copy and replace... 11-16-2012, 02:31 PM
ScabbyDog Re: Macro to copy and replace... 11-16-2012, 03:57 PM
ScabbyDog Re: Macro to copy and replace... 11-16-2012, 06:10 PM
ScabbyDog Re: Macro to copy and replace... 11-16-2012, 07:13 PM
ScabbyDog Re: Macro to copy and replace... 11-16-2012, 07:43 PM
ScabbyDog Re: Macro to copy and replace... 11-16-2012, 08:09 PM
ScabbyDog Re: Macro to copy and replace... 11-16-2012, 08:34 PM
jeffreybrown Re: Macro to copy and replace... 11-16-2012, 09:08 PM
ScabbyDog Re: Macro to copy and replace... 11-16-2012, 09:16 PM
ScabbyDog Re: Macro to copy and replace... 11-17-2012, 07:13 AM
arlu1201 Re: Macro to copy and replace... 11-18-2012, 09:28 AM
  1. #1
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Macro to copy and replace based on dates

    I want a macro that will compare two different sheets. In both sheets column B will contain dates. I want the macro to look at sheet1 at the last row of data in column B and add 1 day. I then want it to go to sheet2 and look for this date and when it finds it copy all the data from then downwards to sheet1 at the very bottom.

  2. #2
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to copy and replace based on dates

    Trying desperately to find a solution online but have come up blank! Anyone any knowledge of how i would do/start this?

  3. #3
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro to copy and replace based on dates

    Do you have a sample file that you can upload?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  4. #4
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to copy and replace based on dates

    Here you go.

    sample.xlsm

  5. #5
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to copy and replace based on dates

    So in the sample I've uploaded in Sheet1, look at the last row of data in column B and add 1 day to the date that you find. Then go to sheet2 and search for this date and when it finds the first instance of this date copy everything on that row and below it and paste it below the last used row in sheet1.

  6. #6
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to copy and replace based on dates

    What about this workaround? Find the last date used in column B sheet 1 and add one day and paste the value in a random cell and then search for the date in the cell in sheet 2 and when found copy and paste that row and everything below to the bottom of sheet 1?

  7. #7
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to copy and replace based on dates

    I've managed to come up with this which finds the last date in column B sheet 1 and pastes the value into sheet2.
    Sub SelectLastCellInColumnOptionB()
    
    Dim Rws As Long, Rng As Range
    
    Rws = Cells(Rows.Count, "B").End(xlUp).Row
    
    Cells(Rws, 2).Copy
    
    Sheets("Sheet2").Select
    Range("W1").Select
    ActiveSheet.Paste
    
    
    End Sub
    I now want code that will Find the date in sheet2 which is now in cell W1 in sheet 2 and when it finds it copy that row and all rows below it that are in use.

  8. #8
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to copy and replace based on dates

    Have gotten this far with the following but doesn't work although I think I'm on the right tracks. Any help fixing the below code??

    Sub testtttttt()
    
        Dim FindWord As String, Found As Range
        Dim wsDest As Worksheet, ws As Worksheet, wb As Workbook
        Dim Nextrow As Long, Lastrow As Long
    
        Set wsDest = ThisWorkbook.Sheets("Sheet1")
        FindWord = ThisWorkbook.Sheets("Sheet2").Range("W1").Value 'I want to find the date that will be in W1
        
        Application.ScreenUpdating = False
     
                 With ThisWorkbook.Worksheets("Sheet2")
                 Set Found = ws.Range("B:B").Find(What:=FindWord, _
                                                     LookIn:=xlValues, _
                                                     LookAt:=xlPart, _
                                                     SearchOrder:=xlByRows, _
                                                     SearchDirection:=xlNext, _
                                                     MatchCase:=False)
    
                    If Not Found Is Nothing Then
                        Nextrow = wsDest.Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1 ' Next empty row on Sheet1
                        Lastrow = ws.Cells.Find("*", , , , xlByRows, xlPrevious).Row         ' Last used row on sheet2
                        ' Copy\Paste found sheet2 data to the next empty row on Sheet1
                        ws.Range(Found, Found.End(xlToRight)).Resize(Lastrow - Found.Row + 1).Copy _
                            Destination:=wsDest.Range("B" & Nextrow)
                    End If
           
        Application.ScreenUpdating = True
        
    End With
    End Sub

  9. #9
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to copy and replace based on dates

       Set Found = ws.Range("B:B").Find(What:=FindWord, _
                                                     LookIn:=xlValues, _
                                                     LookAt:=xlPart, _
                                                     SearchOrder:=xlByRows, _
                                                     SearchDirection:=xlNext, _
                                                     MatchCase:=False)
    The above is causing the issue here with error message Object variable or With block variable not set.

  10. #10
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to copy and replace based on dates

    The code below runs through and gives me the message box but doesn't paste everything below where it finds value W2 into the bottom of sheet1. Why?!?! I'm going crazy trying to work this one out!!!!!!!!!!
    Sub test222()
    Dim FindWord As String, Found As Range
        Dim wsDest As Worksheet, ws As Worksheet, wb As Workbook
        Dim Nextrow As Long, Lastrow As Long
         
        Set ws = ThisWorkbook.Sheets("Sheet2")
        Set wsDest = ThisWorkbook.Sheets("Sheet1")
        
    FindWord = ThisWorkbook.Sheets("Sheet1").Range("W2").Value
    
    Set Found = ws.Range("B:B").Cells.Find(What:=FindWord, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
     
    If Not Found Is Nothing Then
                        Nextrow = wsDest.Cells.Find("*", , , , xlByRows, xlPrevious).Row + 1 ' Next empty row on Sheet1
                        Lastrow = ws.Cells.Find("*", , , , xlByRows, xlPrevious).Row         ' Last used row on sheet2
                        ' Copy\Paste found sheet2 data to the next empty row on Sheet1
                        ws.Range(Found, Found.End(xlToRight)).Resize(Lastrow - Found.Row + 1).Copy _
                            Destination:=wsDest.Range("B" & Nextrow)
                    End If
       
        Application.ScreenUpdating = True
        MsgBox "Copy complete.", vbInformation, "Copy Data"
    
    End Sub

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Macro to copy and replace based on dates

    If you are going to cross post...

    I'm afraid your post does not comply with Rule 8 of our Forum RULES. Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser)to the cross-post. Expect cross-posts without a link to be closed a message will be posted by the moderator explaining why. We are here to help so help us to help you!

    Read this to understand why we ask you to do this, and then please edit your first post to include links to any and all cross-posts in any other forums (not just this site).
    HTH
    Regards, Jeff

  12. #12
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to copy and replace based on dates

    http://www.mrexcel.com/forum/excel-q...find-copy.html

    The above link has the same issue with the most recent code that needs fixing so my macro will work

  13. #13
    Valued Forum Contributor
    Join Date
    03-03-2009
    Location
    UK
    MS-Off Ver
    MS365 Subscription Excel for Mac
    Posts
    1,017

    Re: Macro to copy and replace based on dates

    sample.xlsm

    This might make it simpler. Attached is a workbook with sheet1 and sheet2 and how sheet1 is supposed to look after the macro is run. The added data after the macro is run is in red font.

  14. #14
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,167

    Re: Macro to copy and replace based on dates

    So only 1 date should be added to sheet1? What if you have data till 11/12/2012 in Sheet 1 and data till 11/16/2012 in Sheet2. If you add one date in sheet1, it will be 11/13/2012. You find that data and copy. Then do you repeat and put in 11/14/2012? Till what date should the macro continue doing this?

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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