Hi all.
I have workbook that contains several hidden worksheets that are used as templates and several visible worksheets that are copies of those templates.
Each visible worksheet contains a cell H2 which contains a text string which is the name of the original hidden template worksheet from which that sheet was created. Each visible worksheet also has a localised defined name "MasterSheet" which refers to cell $H$2 in that worksheet.
I am trying to write a macro that has the following effect:
Reset the formulae contained in any selected range of cells to be the original formulae that resided in those cells when the active worksheet was originally created. These formulae will be the same as the formulae contained in the same cell references of the corresponding hidden template worksheet. (no insertion or deletion of rows or columns is permitted)
Cells may contain single-cell array formulae but not multi-cell array formulae
This is what I have come up with so far:
Sub RestoreFormulae()
Dim rCell As Range
Dim stAddress As String
Dim stFormula As String
If ActiveWindow.SelectedSheets.Count > 1 Then GoTo EndSubRF
If ThisWorkbook.ActiveSheet.Names("Mastersheet") Is Nothing Then GoTo EndSubRF
'FOLLOWING LINE GIVES RUN TIME ERROR 13 (Type mismatch)
With ThisWorkbook.Worksheets(ThisWorkbook.ActiveSheet.Names("MasterSheet"))
For Each rCell In Selection.Cells
stAddress = rCell.Address
'CODE MISSING HERE TO SET THE VALUE OF stFormula
If .Range(stAddress).HasArray Then
rCell.FormulaArray = stFormula
Else '.Range(sAddress).HasArray = False
rCell.Formula = stFormula
End If '.Range(sAddress).HasArray
Next rCell
'To refresh formula bar (Excel bug):
ActiveCell.Next.Activate
ActiveCell.Previous.Activate
End With
EndSubRF:
End Sub 'RestoreFormulae()
As far as I can tell I have two problems:
1) What is the correct syntax of the WITH statement so that it refers to the worksheet described by the defined name "MasterSheet"?
2) How to I define the value of stFormula preparatory to resetting the relevant cells?
Of course there may be something else that I have missed.
Any help appreciated.
Bookmarks