Results 1 to 8 of 8

Restoring original formula by VBA

Threaded View

1eyedjack Restoring original formula by... 02-21-2010, 11:19 AM
JeanRage Re: Restoring original... 02-21-2010, 11:30 AM
1eyedjack Re: Restoring original... 02-21-2010, 12:03 PM
1eyedjack Re: Restoring original... 02-21-2010, 04:27 PM
shg Re: Restoring original... 02-21-2010, 04:28 PM
1eyedjack Re: Restoring original... 02-21-2010, 05:43 PM
1eyedjack Re: Restoring original... 02-21-2010, 11:41 AM
1eyedjack Re: Restoring original... 02-21-2010, 11:42 AM
  1. #1
    Forum Contributor
    Join Date
    11-14-2007
    Posts
    142

    Restoring original formula by VBA

    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.
    Last edited by 1eyedjack; 02-21-2010 at 05:52 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1