EVALUATE

footoo

Well-known Member
Joined
Sep 21, 2016
Messages
3,644
Office Version
  1. 365
Platform
  1. Windows
Is there a way to use EVALUATE to enter the results of the formula directly into the range instead of doing it the following way?
VBA Code:
Sub v()
Dim r As Range: Set rng = [C59:C73].Address
rng.FormulaR1C1 = "=IFERROR(INDEX(INDIRECT(""'""&RC2&""'!N:N""), MATCH(9.99999999999999E+307, INDIRECT(""'""&RC2&""'!N:N""), 1)), """")"
rng.Value = rng.Value
End Sub
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
A couple of observations in your macro, you declare the variable r as Range, but you don't use it in your macro.
This statement: Set rng = [C59:C73].Address marks an error for me. This is not how it should be, it should be: Set r = [C59:C73]

I tried a few ways to put the Evaluate statement, but it didn't work with a range, so it works for me in a loop:

VBA Code:
Sub v3()
  Dim r As Range
  For Each r In Range("C59:C73")
    r.Value = Evaluate("=IFERROR(INDEX(" & r.Offset(, -1) & "!N:N,MATCH(9.99E+307," & r.Offset(, -1) & "!N:N, 1)),"""")")
  Next
End Sub
 
Upvote 0
A couple of observations in your macro, you declare the variable r as Range, but you don't use it in your macro.
This statement: Set rng = [C59:C73].Address marks an error for me. This is not how it should be, it should be: Set r = [C59:C73]

I tried a few ways to put the Evaluate statement, but it didn't work with a range, so it works for me in a loop:

VBA Code:
Sub v3()
  Dim r As Range
  For Each r In Range("C59:C73")
    r.Value = Evaluate("=IFERROR(INDEX(" & r.Offset(, -1) & "!N:N,MATCH(9.99E+307," & r.Offset(, -1) & "!N:N, 1)),"""")")
  Next
End Sub
Oops. Also, the formula makes no sense.
Thanks for your input but I want to avoid looping.
The following achieves this but I was hoping to write the results directly to the range (instead of writing a formula then converting to value).

VBA Code:
Sub v()
Dim rng As Range: Set rng = [C59:C73]
rng.Offset(0, 1).FormulaR1C1 = "=LOOKUP(9.99999999999999E+307,INDIRECT(""'"" & RC2 & ""'!N:N""))"
rng.Value = rng.Value
End Sub
 
Upvote 0
I could do per below, but would still like to know if it is possible to write the results directly to the range by using EVALUATE.
Or perhaps there is a formula that does not use INDIRECT?
VBA Code:
Sub UpdateValuesWithArray2()
Dim rng As Range, dataArray, i&
Set rng = [C59:C73]
dataArray = rng
For i = 1 To UBound(dataArray, 1)
    dataArray(i, 1) = Application.WorksheetFunction.Lookup _
    (9.99999999999999E+307, Evaluate("'" & rng(i)(1, 0) & "'!N:N"))
Next i
rng = dataArray
End Sub
 
Upvote 0
If you want a formula that doesn't use INDIRECT, but want to take the sheet name of a cell, it's not possible. But with the macro it could be:

VBA Code:
Sub v3a()
  Dim r As Range
  For Each r In Range("C59:C73")
    r.Value = Evaluate("=LOOKUP(9.99E+307,'" & r.Offset(, -1).Value & "'!N:N)")
  Next
End Sub

I tried to use EVALUATE directly, but when you use it on a range it doesn't work for this case.
 
Upvote 0
If you want a formula that doesn't use INDIRECT, but want to take the sheet name of a cell, it's not possible. But with the macro it could be:

VBA Code:
Sub v3a()
  Dim r As Range
  For Each r In Range("C59:C73")
    r.Value = Evaluate("=LOOKUP(9.99E+307,'" & r.Offset(, -1).Value & "'!N:N)")
  Next
End Sub

I tried to use EVALUATE directly, but when you use it on a range it doesn't work for this case.
Yes, that was the same as I experienced.
I think I'll stick with the macro in the original post (corrected, of course).
 
Upvote 0

Forum statistics

Threads
1,223,894
Messages
6,175,252
Members
452,623
Latest member
Techenthusiast

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