Accessing Files in a Directory with Varying Titles

Grace1918

New Member
Joined
Feb 24, 2020
Messages
13
Office Version
  1. 365
Hi there,

I am wanting to copy information from other sheets and paste it on my current sheet. The other sheets are pay rate sheets and a new one comes out every year. The beginning of the file names are all the same but the year changes for each year(ex. Rate Sheet 2020, Rate Sheet 2021).

I was just wondering if there was a way for a macro to go through and open all the rate sheets that are the current year and future years but not open past years. I saw other Forums about doing this with missing title parts but i want to make sure it only starts at the current year. I thought maybe (=YEAR(TODAY())) could be used.

This is what I have so far

Code:
Dim year As Date
Dim filefound As Boolean

    Windows("Rate Sheet 2020.xlsx").Activate
     Filename = "P:\Project Forecasts\Forecast Reference Files\Rate Sheet " & =YEAR(TODAY()) & ".xlsx"
     Workbooks.Open Filename:= _
        "P:\Project Forecasts\Forecast Reference Files\Rate Sheet 2020.xlsx"
    Range("B2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Selection.Copy
    Windows("P0## 201# - PROJECT NAME R .0.5 Grace.xlsm").Activate
    Range("B2").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
        
      
    Windows("Rate Sheet 2021.xlsx").Activate
    Workbooks.Open Filename:= _
        "P:\Project Forecasts\Forecast Reference Files\Rate Sheet 2021.xlsx"
    Range("B2").Select
    Range(Selection, Selection.End(xlDown)).Select
    Range(Selection, Selection.End(xlToRight)).Select
    Application.CutCopyMode = False
    Selection.Copy
    Windows("P0## 201# - PROJECT NAME R .0.5 Grace.xlsm").Activate
    Range("B20").Select
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False

or

Code:
'get current  year
    
   year = Format(Now(), "YYYY")
    
'open rate sheet
    Filename = "P:\Project Forecasts\Forecast Reference Files\Rate Sheet " & year & ".xlsx"
    
    On Error GoTo FileReadError1
    noerror = False
    
    If Not Dir(Filename, vbDirectory) = vbNullString Then
        Set wkb = Workbooks.Open(Filename)
        noerror = True
    End If
    
'the only way to go in here is if the Rate Sheet file is not the right name, or is not in the right path
    If noerror = False Then
FileReadError1:
        MsgBox "The file: " & Filename & " does not exist!  Please select an alternative location for this file."
    'Select new location for the Rate Sheet
        filefound = False
        Do Until filefound = True
            Set fDialog = Application.FileDialog(msoFileDialogFilePicker)
            With fDialog
                .InitialFileName = "C:\"
                .AllowMultiSelect = False
                .Title = "Select the Rate Sheet " & year & " file"
                If .Show = True Then
                    Filename = Dir(.SelectedItems(1))
                Else
                    MsgBox "No rate sheet selected!  Exiting routine."
                    GoTo JUMPTOEND
                End If
            End With
            
            If Filename = "Rate Sheet " & year & ".xlsx" Then
                filefound = True
                Set wkb = Workbooks.Open(Filename, ReadOnly:=True)
            Else
                MsgBox "You selected the file: " & """" & Filename & """" & ".  Please select the file: " & """" _
                & "Rate Sheet " & year & ".xlsx" & """"
            End If
        Loop
    End If
    
'get data from the rate sheet and close it
    Set SHT = wkb.Worksheets(1)
    maxrows = SHT.Range("B1048576").End(xlUp).Rows(1).Row
    maxcol = SHT.Range("ZZ2").End(xlToLeft).Columns(1).Column
    RateData = SHT.Range(SHT.Cells(1, "A"), SHT.Cells(maxrows, maxcol))
    wkb.Close (False)

But I want the macro to change to only contain relevant years.

Thanks in advance for anyone who can help
 

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.

Forum statistics

Threads
1,214,912
Messages
6,122,200
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top