Copy formulas from one sheet to another

JARichard74

Board Regular
Joined
Dec 16, 2019
Messages
112
Office Version
  1. 365
Platform
  1. Windows
When I open the workbook, a new sheet is created using today's date as the name e.g. 18-July (The WorksheetFunction.Text(DateToday, "[$-C0C]dd-mmmm") is so that the name would be in french). In the same script, I copy the content of a template onto the new sheet. Where I am having trouble is with three cells B39:B41 where I need these cells to refer to the cells D39:D41 from the previous day i.e. the sheet named 17-July. ='17-July'!D39. Here is the script that I have so far. Any help would be appreciated.
VBA Code:
Private Sub Workbook_Open()
 
Dim TodaysDate As String
Dim NewSheetName As String
Dim DateToday As String

'Get Todays Date
DateToday = Now()
NewSheetName = WorksheetFunction.Text(DateToday, "[$-C0C]dd-mmmm")
'Check Todays Sheet Doesn't Already Exist
Dim SheetExists As Boolean
For sheetnames = Worksheets.Count To 1 Step -1
    If Worksheets(sheetnames).Name = NewSheetName Then
        SheetExists = True
        Exit For
    End If
Next
    
If SheetExists = False Then
Sheets.Add(Before:=Sheets(Sheets.Count)).Name = NewSheetName
Sheets("demo").Range("A1:Z100").Copy Destination:=ActiveSheet.Range("A1:Z100")


End If


End Sub
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

RobP

Active Member
Joined
May 8, 2008
Messages
427
Office Version
  1. 365
Platform
  1. Windows
Hi, this seems to be able to reference what you need. Only caveat is that when you open your workbook, It expects yesterdays sheet to be active. eg. before you actually add a new sheet to it.

First store the sheet name you used yesterday - you can put this just after you dimension your variables.

VBA Code:
yesterdayssheet = ActiveSheet.Name

Then, use this to copy the data from cells D of the old sheet into Cells B of your new one. You can add this line to the end of your code.

VBA Code:
ActiveSheet.Range("b39:b41") = Sheets(yesterdayssheet).Range("d39:d41").Value

When I tried your code, it was taking exception to your new "sheetname" when you were creating the sheet, but I assume you had that working on your machine given the problem you mentioned above ...
(it appears you cannot use / or : for a sheetname...)

Rob
 

JARichard74

Board Regular
Joined
Dec 16, 2019
Messages
112
Office Version
  1. 365
Platform
  1. Windows
Thanks Rob. Works well with the assumption yesterdays sheet is active when the workbook is open. That will likely be true for 99% of the time. How would I make yesterdayssheet always today - 1 day e.g. if today is 19-July how would I make yesterdayssheet sheet.name is 18-July and it keeps rolling like that till the end of the month.
 

JARichard74

Board Regular
Joined
Dec 16, 2019
Messages
112
Office Version
  1. 365
Platform
  1. Windows
I added the following code and it seems to work. DateHier is yesterday's date in french...
VBA Code:
Dim DateHier As String
DateHier = Now() - 1
yesterdayssheet = WorksheetFunction.Text(DateHier, "[$-C0C]dd-mmmm")
 

RobP

Active Member
Joined
May 8, 2008
Messages
427
Office Version
  1. 365
Platform
  1. Windows
Great - thanks for the feedback - glad to have been of assistance.
cheers
Rob
 
Solution

Forum statistics

Threads
1,181,950
Messages
5,932,925
Members
436,869
Latest member
ABGTH

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
Top