+ Reply to Thread
Results 1 to 3 of 3

VBA - Referencing file with changing date

Hybrid View

  1. #1
    Registered User
    Join Date
    12-02-2012
    Location
    Australia
    MS-Off Ver
    Excel 2007
    Posts
    51

    Question VBA - Referencing file with changing date

    Hi all,

    I am trying to write a macro that copies data from a spreadsheet each month, however the name of the file will change each month to reflect the latest month. For example the spreadsheet will be titled 'Book1 Example 300613, then the next month the file will be titled Book1 Example 310713. Is there a way I can amend my code to pick up the latest month?

    Sub Copy_As_Required()
        Dim cl As Range, r As Range
        Dim ThisWB As Workbook
        Set ThisWB = Workbooks("Book1 Example.xlsm")
        Application.ScreenUpdating = False
        For Each cl In ThisWB.Sheets("Sheet1").Range("A2:A" & Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row)
            cl.Offset(, 1).Copy
            Workbooks("Book2.xlsx").Activate
            On Error Resume Next
            Set r = Range("A:A").Find(What:=cl.Value)
            With r.Offset(, 1)
                .PasteSpecial (-4163)
            End With
            Windows("Book1 Example.xlsm").Activate
        Next
        Application.ScreenUpdating = True
    End Sub
    Thanks

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,492

    Re: VBA - Referencing file with changing date

    One way, maybe:

    Dim lDate As Long
    Dim sDate As String
    
    lDate = Application.Evaluate("=DATE(YEAR(TODAY()),MONTH(TODAY())+1,0)")
    sDate = Application.WorksheetFunction.Text(lDate, "dd/mm/yyyy")

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


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

    Re: VBA - Referencing file with changing date

    The EOMONTH() formula will tell you the last day of any specific month.

    This will tell you the last day of this month:
    =EMONTH(TODAY(), 0)

    This will tell you the last day of last month:
    =EMONTH(TODAY(), -1)

    ...etc.

    With that you have all you need to "look" for the file based on end of month dates, say for the last 3 months.


    Sub Copy_As_Required()
    Dim cl As Range, r As Range
    Dim SrcWB As Workbook, fNAME As String, i As Long, DestWS As Worksheet
    
    Set DestWS = ThisWorkbook.Sheets("Sheet1")          'edit to the name of the sheet we are copying values INTO
    
        For i = 0 To 3                                  'check the last 3 months for the EOMONTH file
            fNAME = "C:\2013\Book1 Example " & Format(Evaluate("EOMONTH(TODAY(), " & -i & ")"), "DDMMYYYY") & ".xlsx"
            If Len(Dir(fNAME)) > 0 Then                 'if the file exists, open it
                Application.ScreenUpdating = False
                Set SrcWB = Workbooks.Open(fNAME)
                Exit For
            End If
        Next i
    
        If SrcWB Is Nothing Then                         'if no file was found
            MsgBox "End of Month file not found for the past 3 months."
            Exit Sub
        End If
    
        On Error Resume Next                            'update each value from found file into DestWS
        For Each cl In SrcWB.Sheets("Sheet1").Range("A2:A" & Sheets("Sheet1").Cells(Rows.Count, 1).End(xlUp).Row)
            Set r = DestWS.Range("A:A").Find(cl.Value, LookIn:=xlValues, LookAt:=xlWhole)
            If Not r Is Nothing Then
                r.Offset(, 1).Value = cl.Offset(, 1).Value
            End If
        Next cl
    
        SrcWB.Close False                               'close the opened file
        Application.ScreenUpdating = True
    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!)

+ 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. Referencing in from files: Changing the reference file?
    By AMEY01 in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 05-24-2013, 05:12 AM
  2. referencing file with date/time stamp
    By cabinetguy in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-21-2011, 02:17 PM
  3. Replies: 3
    Last Post: 08-18-2010, 12:58 PM
  4. Referencing Changing File Names
    By Dave1674 in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 11-15-2007, 12:19 PM
  5. Changing cell date in a file
    By Wilberforce in forum Excel General
    Replies: 7
    Last Post: 02-01-2007, 10: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