+ Reply to Thread
Results 1 to 12 of 12

Copy, Paste loop based on today's Date

Hybrid View

  1. #1
    Registered User
    Join Date
    01-14-2014
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Copy, Paste loop based on today's Date

    Hi all,

    I’ve been searching and searching for the solution to this problem for a couple of days now, however given my inexperience with excel VBA programming it’s proving difficult to pin down a solution to my problem. I’d appreciate any help you can offer. Here’s what I’m after:

    In the attached spreadsheet I’d like to copy the value in “A1” (IRS) and paste to (Extract) only if the adjacent cells date is equal to today’s date. I need something that’s going to work through the visible cells (I’ve been looking at For each) until the end of the row, and also not do anything if the sheet is blank, which will sometimes occur.

    If you need more information please let me know!

    Thank heaps,
    Oscarmiike.
    Attached Files Attached Files

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy, Paste loop based on today's Date

    Maybe like so:

    Option Explicit
    
    Sub IFS()
    Dim CopyRNG As Range, c As Range
    
    For Each c In Sheets("IRS").Range("B2:B" & Rows.Count).SpecialCells(xlConstants, 1)
        If CDate(c.Value) = CDate(Date) And Rows(c.Row).Hidden = False Then
            If CopyRNG Is Nothing Then
                Set CopyRNG = c.Offset(, -1)
            Else
                Set CopyRNG = Union(CopyRNG, c.Offset(, -1))
            End If
        End If
    Next c
    
    If Not CopyRNG Is Nothing Then CopyRNG.Copy Sheets("Extract").Range("B5")
    
    End Sub
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Registered User
    Join Date
    01-14-2014
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy, Paste loop based on today's Date

    Thanks JBeaucaire for your prompt reply.

    Unfortunately I'm not getting any results from the code, it's a little too complicated for me to reverse engineer also. Any thoughts?

    Cheers!

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy, Paste loop based on today's Date

    The macro reads:

    For each numeric cell in column B of the IRS sheet, if the date in that cell matches today's date and the row it is on is not hidden, copy the value from column A of that row and paste all the found cells into column B of the Extract sheet.


    From the sample data you posted, I had to change several dates to 1/14 before i got results, too. Did you do that?

  5. #5
    Registered User
    Join Date
    01-14-2014
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy, Paste loop based on today's Date

    Hi JBeaucaire! I was able to make it work based on your advice, thanks so much. Unfortunately I've discovered that the columns which I'm referencing aren't static! I need to be able to pull the data based on the title of the column in row 1, so the offset isn't going to work any more can you help me make the CopyRNG more dynamic?

    Sub IRS_TradeNumber()
    
    Dim CopyRNG As Range
    Dim c As Range
    
    For Each c In Sheets("IRS").Range("YX2:YX1000")
        If c.Value = Date And Rows(c.Row).Hidden = False Then
            If CopyRNG Is Nothing Then
                Set CopyRNG = c.Offset(, 2)
            Else
                Set CopyRNG = Union(CopyRNG, c.Offset(, 2))
            End If
        End If
    Next c
    
    If Not CopyRNG Is Nothing Then CopyRNG.Copy Sheets("Extract").Range("D1048576").End(xlUp).Offset(1)
    
    End Sub
    Cheers!
    Oscarmiike

  6. #6
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy, Paste loop based on today's Date

    Sure, dynamic to what specification?

  7. #7
    Registered User
    Join Date
    01-14-2014
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy, Paste loop based on today's Date

    Based on a string value, in this specific one the title of the column is "Trade Number"

    Thanks :D

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy, Paste loop based on today's Date

    Try this:

    Option Explicit
    
    Sub IFS()
    Dim CopyRNG As Range, Col As Long, c As Range
    
    With Sheets("IRS")
        On Error Resume Next
        Col = WorksheetFunction.Match("Trade Number", .Rows(1), 0)
        If Col = 0 Then
            MsgBox "The 'Trade Number' column was not found"
            Exit Sub
        End If
    
        For Each c In .Range(.Cells(2, Col), .Cells(.Rows.Count, Col)).SpecialCells(xlConstants, 1)
            If CDate(c.Value) = CDate(Date) And Rows(c.Row).Hidden = False Then
                If CopyRNG Is Nothing Then
                    Set CopyRNG = .Range("A" & c.Row)
                Else
                    Set CopyRNG = Union(CopyRNG, .Range("A" & c.Row))
                End If
            End If
        Next c
    
        If Not CopyRNG Is Nothing Then CopyRNG.Copy Sheets("Extract").Range("D" & Rows.Count).End(xlUp).Offset(1)
    End With
    
    End Sub
    Last edited by JBeaucaire; 01-22-2014 at 11:59 AM. Reason: Corrected DIM

  9. #9
    Registered User
    Join Date
    01-14-2014
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy, Paste loop based on today's Date

    Thanks for the quick reply! I can't get it to work though, doesn't seem to find the Column. I've attached a sample:

    FYI the sample doesn't have enough data to get through the full procedure, however once it finds the column the rest will work on my version
    Attached Files Attached Files
    Last edited by oscarmiike; 01-21-2014 at 08:07 PM.

  10. #10
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy, Paste loop based on today's Date

    My apologies, the error was in the variable type, it should be LONG. Edited above and tested as working on your workbook.

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy, Paste loop based on today's Date

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

  12. #12
    Registered User
    Join Date
    01-14-2014
    Location
    Brisbane, Australia
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: Copy, Paste loop based on today's Date

    You're a legend! Works perfectly - Could I trouble you for one more little adjustment though?

    Is it possible to do a 'match' function to find the date column also? So match the "Trade Number" column, then match "Trade Date", then perform the 'for each' copy procedure based on the "Trade Date" being today? I've tried hacking up your code.. not surprisingly with no success

    I really appreciate your help, this should be the final request!

    Cheers,
    oscarmiike
    Attached Files Attached Files

+ 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. [SOLVED] Macro needed to copy rows from sheet1 and paste to sheet2 if date in colmn h is >today
    By Rekoons38 in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 01-10-2014, 12:44 PM
  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. [SOLVED] VBA to copy specific data from one workbook to another based on today's date?
    By hiprhit in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-09-2012, 02:21 PM
  4. Copy/Paste based on encountered condition within a loop
    By msmick in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-05-2011, 06:37 PM
  5. Copy and Paste Loop based on corresponding value in another column
    By gibby8 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 01-14-2011, 03: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