Using an array to store formulas, replace part of the string and then insert to identical sheet

mountainman88

Board Regular
Joined
Jun 22, 2019
Messages
109
Office Version
  1. 2016
Platform
  1. Windows
I was using the below code to copy the usedrange formulas from a sheet called 'template' store them in an array, replace part of the formula string within the array, copy to another array, and then insert that array's contents to an identical new tab, multiple times based on a list of tabs.

The replacearray is working and holds the formulas I need with the replace text, but inserting to the new tab doesn't occur.

This was working in a previous iteration of my work but now it errors on this line:

Code:
ws3.UsedRange.Formula = ReplaceArray

I really don't understand as the usedrange is identical on both sheets (because one is a copy of the other) and I have proved this. Im sure this worked before now I don't know what changed!

VBA Code:
Sub Step4xArrayreplace()

Dim ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet, c As Range, l As Range, r As Range, TempArray() As Variant, ReplaceArray() As Variant

'Application.Calculation = xlCalculationManual
'Application.ScreenUpdating = False

Set ws2 = Sheets("template")
Set ws1 = Sheets("list")

ws1.Activate

RowCount = ws1.Range("a1", Range("a2").End(xlDown)).Count

'populate array with template formulas

TempArray = ws2.UsedRange.Formula

'start cycling through the replace tabs to insert the formulas based on the list of tabs

For Each l In ws1.Range("a1", "a" & RowCount)

        Set ws3 = Sheets(l.Value)
       
'copy the template formulas from template array to replace array
       
ReplaceArray = TempArray

'replace the formulas in the replace array with the tab name

For i = 1 To UBound(ReplaceArray)
    For j = 1 To UBound(ReplaceArray, 2)

    ReplaceArray(i, j) = Replace(ReplaceArray(i, j), "TOTAL'!", l.Value & "'!")
     Next
Next

'copy from the replace array to the replace sheet

        ws3.UsedRange.Formula = ReplaceArray
     
Next l

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True


End Sub

Many thanks in advance for your help!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
What's the error?
 
Upvote 0
Solution

Forum statistics

Threads
1,215,025
Messages
6,122,731
Members
449,093
Latest member
Mnur

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
Back
Top