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?
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
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

View attachment 101081

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?
Try this:

VBA Code:
Sub Test()
Dim dummyArr(0 To 1, 1 To 1) As String
Dim i As Integer

    dummyArr(0, 1) = "=1+1"
    
    dummyArr(1, 1) = "=1+1"

    For i = LBound(dummyArr) To UBound(dummyArr)
        Worksheets("Sheet2").Range("A" & i + 1).Formula2 = dummyArr(i, 1)
    Next i
        
End Sub
 
Upvote 0
Try this:

VBA Code:
Sub Test()
Dim dummyArr(0 To 1, 1 To 1) As String
Dim i As Integer

    dummyArr(0, 1) = "=1+1"
   
    dummyArr(1, 1) = "=1+1"

    For i = LBound(dummyArr) To UBound(dummyArr)
        Worksheets("Sheet2").Range("A" & i + 1).Formula2 = dummyArr(i, 1)
    Next i
       
End Sub
I had this initially, but for my actual use case, it was doing 1000's of formulas, and doing the write call from VBA 1000's of times is significantly slower than calling it once and assigning an array as i am doing.
 
Upvote 0
I had this initially, but for my actual use case, it was doing 1000's of formulas, and doing the write call from VBA 1000's of times is significantly slower than calling it once and assigning an array as i am doing.
Give us some realistic information to go on.

What formulas, surely not just "=1+1", are you trying to apply, how do you assign them to the array and do they reference values in any of the cells.
 
Upvote 0
In the first test sub in post #1, just change your arr data type to variant.

Rich (BB code):
Sub Test_OP()
    Dim dummyArr(0 To 1, 1 To 1) As Variant
    dummyArr(0, 1) = "=1+1"
    dummyArr(1, 1) = "=1+1"

    Worksheets("Sheet1").Range("A1:A2").Formula = dummyArr
End Sub
 
Upvote 0
Solution
In the first test sub in post #1, just change your arr data type to variant.

Rich (BB code):
Sub Test_OP()
    Dim dummyArr(0 To 1, 1 To 1) As Variant
    dummyArr(0, 1) = "=1+1"
    dummyArr(1, 1) = "=1+1"

    Worksheets("Sheet1").Range("A1:A2").Formula = dummyArr
End Sub
How peculiar, I wonder why this works? If I dim a variable as string and assign it to a cell, I dont get the issue, only when assigning the array as type string
 
Upvote 0
Give us some realistic information to go on.

What formulas, surely not just "=1+1", are you trying to apply, how do you assign them to the array and do they reference values in any of the cells.
Obviously my workload is slightly more complex, but given it breaks for this simple example, im pretty sure its not my formulas that im constructing. I only used 1+1 as an example to better highlight the issue I saw
 
Upvote 0
maybe this could help you:

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 = Evaluate(dummyArr)
    
End Sub
 
Upvote 0
maybe this could help you:
That won't put a formula into the cells, and it will only evaluate the first formula in the array, not all of them individually.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,322
Members
452,635
Latest member
laura12345

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