Sheet names that change with date

Hedgingthehedge

New Member
Joined
May 6, 2015
Messages
22
I have the below code which is quite efficient for grabbing a sheet from a closed work book and then pasting it where it needs to go. How ever I cant find consistency when the date changes in the sheet name itself...

Sub UpdateRecord()
Dim SourceWB As String, SourceFile As String, SourceWS As Worksheet, DestWS As Worksheet
SourceWB = "DwightPDTrials.xlsm"
'Configure Destination Data Location
Set DestWS = Workbooks("TestD.xlsm").Sheets("Trial")

'Configure Source Data Location
SourceFile = "A:\Group\ECDI\Dwight\TRIAL IMPORT\" & SourceWB
Workbooks.Open Filename:=SourceFile
Set SourceWS = ActiveWorkbook.Sheets("T QAHZ A 22MAY15") '--> The date changes everyday which is my problem. The end goal is to save over a constant locaiton so the file is good, its just the sheets. I need the identifyer to only need "T QAHZ A" to locate the right sheet but when I try to only do that, I get a subscript out of range error. What is the best way to set SourceWS based only off the part of the sheet name that will always remain constant?
SourceWS.Activate

'Inspect Source Data Record
Cells.Select
Selection.Copy
DestWS.Activate
Cells.Select
ActiveSheet.Paste
'Close Source Workbook
Workbooks(SourceWB).Close SaveChanges:=False
End Sub

Thanks,
HTH
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Code:
Set oWB = Workbooks.Open(Filename:=SourceFile)
For Each oWS In oWB.Sheets
    If oWS.Name Like "T QAHZ A *" Then
        Set SourceWS = oWS
        Exit For
    End If
Next oWS

Debug.Print SourceWS.Name
 
Upvote 0

Forum statistics

Threads
1,196,325
Messages
6,014,650
Members
441,834
Latest member
GHOSTOF309

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