Try:
.Formula = "='" & strProjectName & "'!" & Cells(2, 7).Address
If your worksheet name ( strProjectName ) has spaces, you need to include single quotes in the formula line.
If this does not solve the problem, provide additional information about the specific error or error.
Update
In the comments, you indicate that you are replacing spaces with underscores. Perhaps you are doing something like:
strProjectName = Replace(strProjectName," ", "_")
But if you do not push this change to the Worksheet.Name property, you can expect this to happen:
- A file view dialog box appears.
- Formula returns
#REF error
The reason that you are passing a link to a sheet that does not exist is why you get the #REF error. The file dialog box is an attempt to allow you to fix this link by specifying a file that contains the sheet name . When you cancel, the #REF error is expected.
So you need to do:
Worksheets(strProjectName).Name = Replace(strProjectName," ", "_") strProjectName = Replace(strProjectName," ", "_")
Then your formula should work.
source share