VBA: Convert Complex Formula to VBA

zero269

Active Member
Joined
Jan 16, 2023
Messages
253
Office Version
  1. 365
Platform
  1. Windows
Hello,

I'm looking to see if I can convert a complex formula that I got from @Peter_SSs in this thread:


I was looking into VBA's WorksheetFunction's but LET isn't listed.
This led me to look at one of the other three formulas in that thread. @Phuoc uses just the AGGREGATE function, but after testing it again, I realized it's not finding the first missing number.
So, perhaps there's a way to still use the one provided by Peter?

Excel Formula:
=LET(s,SEQUENCE(1000,,990001),AGGREGATE(15,6,s/ISNA(MATCH(s,B4:B14,0)),1))

Or perhaps there's another way to achieve the same results using VBA?

Thank you,
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Hi zero269

In my eyes, it is not useful to use such a function in VBA,
it is enough to traverse the lines with a loop, and when a cell is empty, take the value of the cell of the line above +1

Regards
Brian
 
Upvote 0
Hi,

To make your life easier, have you tested : Evaluate("yourComplexFormula") ???
 
Upvote 0
Hi,

To make your life easier, have you tested : Evaluate("yourComplexFormula") ???
Hi James,

Thanks for the suggestion. I took a look at the VBA Evaluate method, but it didn't like it.
It flagged the LET function. Unfortunately, it's not an available WorksheetFunction object.

1678466699324.png
1678466664808.png
 
Upvote 0
Can you explain why you are trying to 'convert' anything?
Hi Peter,

Your code works perfectly. However, I was hoping I could move it into VBA to free up cell A1 where it currently resides.
When I would add that value to my list of books, I would enter it manually. Later I took the VBA approach to speed up that process:

VBA Code:
'Insert the next available non-AR number into the active cell.
Sub EnterQuizNumber()
    Range("A1").Copy
    ActiveCell.PasteSpecial xlValues
    Application.CutCopyMode = False
    ActiveCell.Offset(1).Select 'Select next cell below
End Sub

This made entering that formulated value easier.

I figure if I could somehow use your formula with my Copy/Paste formula, that would be optimal.
But... LET is being rejected by VBA.

Thanks,
 
Upvote 0
But... LET is being rejected by VBA.
It is not actually rejecting LET, it is just that LET happens to be at the start of where you have a syntax error. That is, where the "Expected: expression" should start.

@James006's question was:
have you tested : Evaluate("yourComplexFormula")
But you have tested
Evaluate(yourComplexFormula)
Can you spot the difference?

Using this sample from that earlier thread (post #3) ..

Zero269_1.xlsm
C
4990001
5990002
6
7990004
8990005
9990006
10990007
11990008
12990009
13990010
14
Next Available


.. and this code ..

VBA Code:
Sub Test()
  Dim NextNum As Long
 
  NextNum = Evaluate("=LET(s,SEQUENCE(1000,,990001),AGGREGATE(15,6,s/ISNA(MATCH(s,C4:C14,0)),1))")
  MsgBox NextNum
End Sub
.. I get this result ..

1678491844851.png
 
Upvote 1
Solution
my 2 cents worth
The formula version is more flexible.
Lambda would work but there are few advantages to using it with this challenge see C2

T202303a.xlsm
CDEF
1
2990003
399000398000910046
499000198000110011
599000298000210022
698000310033
79900049800044
899000598000510055
99900069800061006
109900079800071007
1199000898000810088
1299000910099
13990010980010101010
14
1d
Cell Formulas
RangeFormula
C2C2=MissingNum()
C3:F3C3=LET(s,SEQUENCE(10,,C4,1),AGGREGATE(15,6,s/ISNA(MATCH(s,C4:C14,0)),1))
 
Upvote 0
Hi Peter,

Can you spot the difference?
Yes, I can see where I went wrong on trying to use the Evaluate method. I needed to enclose it as written in double-quotes. Lesson learned for sure.
I think this is far easier than the example I saw from Microsoft: Application.Evaluate method (Excel)
It was confusing as hell...
.. and this code ..
I ran your correct version of Evaluate in my workbook and it definitely produced the same value that cell A1 is showing.

1678572193481.png
The beauty of this is that I can now use your solution for getting the Next Quiz Number and then Pasting it into the Active Cell.
VBA Code:
Sub EnterNextQuizNumber()

    Dim NextNum As Long
    NextNum = Evaluate("=LET(s,SEQUENCE(1000,,990001),AGGREGATE(15,6,s/ISNA(MATCH(s,tblBooks[Quiz],0)),1))")
    ActiveCell.Value = NextNum
    ActiveCell.Offset(1).Select 'Select next cell below
    
End Sub

This was extremely helpful! (y)

Thanks again Peter for all your help.
 
Upvote 0
The formula version is more flexible.
Hi Dave,
Yes, for most situations I would agree that approach is best. Especially in very large data sets and ranges.
However, for my situation, I only need that value so I can add it back into the worksheet when needed.
VBA Code:
Sub EnterNextQuizNumber()

    Dim NextNum As Long
    NextNum = Evaluate("=LET(s,SEQUENCE(1000,,990001),AGGREGATE(15,6,s/ISNA(MATCH(s,tblBooks[Quiz],0)),1))")
    ActiveCell.Value = NextNum
    ActiveCell.Offset(1).Select 'Select next cell below
    
End Sub
Thank you and best regards,
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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