VBA: Recorded Formula in Macro Not Working in VBA

datastudent

Board Regular
Joined
Sep 7, 2021
Messages
91
Office Version
  1. 365
Platform
  1. Windows
Hi,

I recorded a Macro adding a formula in my Sheet. However when I tried to run it through VBA its not working and getting an error. Please help me troubleshoot.

Formula:
=LET(b,Note!$F$2:$F$80,c,Note!$E$2:$E$80,mf,{"Mobile","Fixed"},XLOOKUP(A2,c,b,XLOOKUP(TRUE,ISNUMBER(SEARCH(mf,A2)),mf,"",0),0))


Range("C2").Formula2R1C1 = _
"=LET(b,Note!R2C6:R80C6,c,Note!R2C5:R80C5,mf,{""Mobile"",""Fixed""},XLOOKUP(RC[-2],c,b,XLOOKUP(TRUE,ISNUMBER(SEARCH(mf,RC[-2])),mf,"""",0),0))"
Range("C3").Select
 
The technique I always use to debug the error when I have written an equation with vBA is:
Remove the equals sign from the VBA, so that you are just writing text to the cell, Then select the cell on the worksheet and manually put the equals sign back in again, EXCEL will then show you where the error is.
e.g:
VBA Code:
Range("C2").FormulaR1C1 = _
"LET(b,Note!R2C6:R80C6,c,Note!R2C5:R80C5,mf,{""Mobile"",""Fixed""},XLOOKUP(RC[-2],c,b,XLOOKUP(TRUE,ISNUMBER(SEARCH(mf,RC[-2])),mf,"""",0),0))"
 
Upvote 0
You need to change the name of the variable c if you are using R1C1 references.
 
Upvote 0
The technique I always use to debug the error when I have written an equation with vBA is:
Remove the equals sign from the VBA, so that you are just writing text to the cell, Then select the cell on the worksheet and manually put the equals sign back in again, EXCEL will then show you where the error is.
e.g:
VBA Code:
Range("C2").FormulaR1C1 = _
"LET(b,Note!R2C6:R80C6,c,Note!R2C5:R80C5,mf,{""Mobile"",""Fixed""},XLOOKUP(RC[-2],c,b,XLOOKUP(TRUE,ISNUMBER(SEARCH(mf,RC[-2])),mf,"""",0),0))"
I tried it and I'm getting that error message you get when the formula is incorrect then when I click ok it highlight R80C6.

I still don't understand :cry:
 
Upvote 0
In that case try
VBA Code:
Range("C2").Formula2 = _
"=LET(b,Note!$F$2:$F$80,c,Note!$E$2:$E$80,mf,{""Mobile"",""Fixed""},XLOOKUP(A2,c,b,XLOOKUP(TRUE,ISNUMBER(SEARCH(mf,A2)),mf,"""",0),0))"
 
Upvote 0
Solution
In that case try
VBA Code:
Range("C2").Formula2 = _
"=LET(b,Note!$F$2:$F$80,c,Note!$E$2:$E$80,mf,{""Mobile"",""Fixed""},XLOOKUP(A2,c,b,XLOOKUP(TRUE,ISNUMBER(SEARCH(mf,A2)),mf,"""",0),0))"
It worked! Thank you so much!
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0
Just to clear up the mystery: You have to change c in all places in the formula. This code works for me:
VBA Code:
    Range("C2").Formula2R1C1 = _
    "=LET(blabla,Note!R2C6:R80C6,chacha,Note!R2C5:R80C5,mf,{""Mobile"",""Fixed""},XLOOKUP(RC[-2],chacha,blabla,XLOOKUP(TRUE,ISNUMBER(SEARCH(mf,RC[-2])),mf,"""",0),0))"
 
Upvote 0

Forum statistics

Threads
1,226,812
Messages
6,193,114
Members
453,777
Latest member
Miceal Powell

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