Hello,
I am trying to write some code that will select two sheets from an existing file and copy them into a new workbook. My sheets are named via code.
This is the code that I tried, and I can see that my dateToday is "10-08-24" String and yesterdayDate is "10-07-24" also as string in my locals window. So that looks good to me.
however when I try to run it, my todaySheet and yesterdaySheet are both empty and nothing is selected. What am I doing wrong? My sheets will always be today's and yesterday's (last business day so no weekends or holiday) and they will always be the last 2 sheets that were created right now they are sheet187 (10-07-24) and sheet188 (10-08-24)
I am trying to write some code that will select two sheets from an existing file and copy them into a new workbook. My sheets are named via code.
VBA Code:
Dim dateToday As String
dateToday = Format(Date, "mm-dd-yy")
Sheets("Sheet1").Name = dateToday
This is the code that I tried, and I can see that my dateToday is "10-08-24" String and yesterdayDate is "10-07-24" also as string in my locals window. So that looks good to me.
VBA Code:
Dim ws As Worksheet
Dim dateToday As String
Dim yesterdayDate As String
Dim newWorkbook As Workbook
' Get today's and yesterday's dates in "MM-DD-YY" format
dateToday = Format(Date, "mm-dd-yy")
yesterdayDate = Format(Application.Evaluate("=WORKDAY(TODAY(),-1)"), "mm-dd-yy")
'loop through the sheets to select today and yesterday
For Each ws In ThisWorkbook.Worksheets
If ws.Name = dateToday Then
Set todaySheet = ws
ElseIf ws.Name = yesterdayDate Then
Set yesterdaySheet = ws
End If
Next ws
' Create a new workbook
Set newWorkbook = Workbooks.Add
however when I try to run it, my todaySheet and yesterdaySheet are both empty and nothing is selected. What am I doing wrong? My sheets will always be today's and yesterday's (last business day so no weekends or holiday) and they will always be the last 2 sheets that were created right now they are sheet187 (10-07-24) and sheet188 (10-08-24)
Last edited by a moderator: