Formula being stored as text, when assigned from an array in VBA

luker

New Member
Joined
Jun 5, 2023
Messages
6
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I've written the following sub, but when i run it, it doesn't populate A1 and A2 with 2 as one would expect, but instead just places the formulas in the cells as text

VBA Code:
Sub Test()
    Dim dummyArr(0 To 1, 1 To 1) As String
    dummyArr(0, 1) = "=1+1"
    dummyArr(1, 1) = "=1+1"

    Worksheets("Sheet1").Range("A1:A2").Formula = dummyArr
End Sub

1698407260585.png


I've checked all my cell formatting options, calculation mode (and tried recalculating at the end of the subprocedure) but none of this seems to work.

I found this solution on stack overflow which seems to work Evaluate text string as formula in VBA, which seems to be the only thing that works:
VBA Code:
Sub Test()
    Dim dummyArr(0 To 1, 1 To 1) As String
    dummyArr(0, 1) = "=1+1"
    dummyArr(1, 1) = "=1+1"

    Worksheets("Sheet1").Range("A1:A2").Value= dummyArr
    Worksheets("Sheet1").Range("A1:A2").Formula = Worksheets("Sheet1").Range("A1:A2").Value
End Sub

Does anyone know what exactly is going on here, and what exactly is causing Excel to have this behaviour?
 
Well @RoryA , to be fair I did say "maybe" :)

correct on the first point - but it does get a result that I suppose he looks for somehow, on the second point I was not aware - I'm now off to change the second formula to prove your theory .. (which I'm sure is correct of course, I just hadn't seen it in my test of his data)

thanks
Rob

edit: Yep, does what you said it would :)
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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