+ Reply to Thread
Results 1 to 15 of 15

Macro can't run otherwise working formula

Hybrid View

  1. #1
    Registered User
    Join Date
    02-19-2015
    Location
    Iowa, USA
    MS-Off Ver
    2010
    Posts
    3

    Macro can't run otherwise working formula

    I'm trying to run a short macro to set up an "Audit Selection" population of data. But I always get an error when the script gets to the part of entering the formula:

    =LET(d,FILTER('Policy List'!A4:A5000,'Policy List'!A4:A5000<>""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))

    Another user on this forum helped me with the formula and it works great when I just paste it in the cell. Is there some reason I can't plug it into a macro?

    Audit Selection Macro.txt

    Debug.JPGError Screenshot.JPG
    Last edited by ladylissa21; 07-12-2024 at 01:35 PM.

  2. #2
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,174

    Re: Macro can't run otherwise working formula

    As far as I can determine, the "LET" formula et al cannot be incorporated into VBA (but I could be wrong!).
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: Macro can't run otherwise working formula

    @JT: shouldn’t be a problem, it's just a formula.

    @ladylissa21: Fast answers need visual help. Please read the yellow banner at the top of this page on how to attach a file and a mocked up solution.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,174

    Re: Macro can't run otherwise working formula

    Using FormulaArray places the formula correctly BUtT it does not SPILL as a "native" formula would.

    Over to the gurus!

  5. #5
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: Macro can't run otherwise working formula

    You use Formula2 or Formula2R1C1 with dynamic formulae.

  6. #6
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,174

    Re: Macro can't run otherwise working formula

    @TMS: thank you ... learned something new!

    So hopefully the OP's query has been answered.

    example ....

    Sub x()
    [h33].Formula2 = "=LET(a,M4:Q28,b,M4:M28,FILTER(a,b=""Compi""))"
    
    End Sub
    Last edited by JohnTopley; 07-12-2024 at 04:46 PM.

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: Macro can't run otherwise working formula

    In the absence of a workbook and the other formulae, best guess:

    Option Explicit
    
    Sub Test()
    
    Sheets("Sheet1").Range("A2").Formula2 = _
        "=LET(d,FILTER('Policy List'!A4:A5000,'Policy List'!A4:A5000<>""""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))"
    
    End Sub
    
    Sub AuditSelection_TMS()
    '
    ' AuditSelection Macro
    '
    Dim shNew As Worksheet
    
    ' delete the Adjusted List sheet, if it exists
    On Error Resume Next
    Application.DisplayAlerts = False
    Sheets("Adjusted List").Delete
    Application.DisplayAlerts = False
    On Error GoTo 0
    
    ' create the Adjusted List sheet
    Sheets("Policy List").Copy Before:=Sheets(2)
    Set shNew = ActiveSheet
    
    With shNew
        .Name = "Adjusted List"
        .Range("A4:A" & .Range("A" & .Rows.Count).End(xlUp).Row).ClearContents
        .Range("C4:C" & .Range("C" & .Rows.Count).End(xlUp).Row).ClearContents
        .Range("F4:F" & .Range("F" & .Rows.Count).End(xlUp).Row).ClearContents
        .Range("A4").Formula2 = _
            "=LET(d,FILTER('Policy List'!A4:A5000,'Policy List'!A4:A5000<>"""",""no data""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))"
        .Range("C4").Formula2 = _
            "=LET(d,FILTER('Policy List'!C4:C5000,'Policy List'!C4:C5000<>"""",""no data""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))"
        .Range("F4").Formula2 = _
            "=LET(d,FILTER('Policy List'!F4:F5000,'Policy List'!F4:F5000<>"""",""no data""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))"
        
        ' convert to values
        With .Range("A4:G" & .Range("A" & .Rows.Count).End(xlUp).Row)
            .Value = .Value
        End With '.Range()
    End With 'shNew
    
    End Sub

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: Macro can't run otherwise working formula

    Very strange. If I use the macro in post #7, the formulae are correctly output to the sheet.

    If I then record a macro while I re-enter the formula, this is what I get:

    Option Explicit
    
    Sub Macro1()
    '
    ' Macro1 Macro
    '
    
    '
    Range("A4").Select
    ActiveCell.Formula2R1C1 = _
        "=LET(d,FILTER('Policy List'!RC:R[4996]C,'Policy List'!RC:R[4996]C<>"""",""no data""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))"
    Range("C4").Select
    ActiveCell.Formula2R1C1 = _
        "=LET(d,FILTER('Policy List'!RC:R[4996]C,'Policy List'!RC:R[4996]C<>"""",""no data""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))"
    Range("F4").Select
    ActiveCell.Formula2R1C1 = _
        "=LET(d,FILTER('Policy List'!RC:R[4996]C,'Policy List'!RC:R[4996]C<>"""",""no data""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))"
    Range("F5").Select
    End Sub
    If I then try to run that (on an empty sheet), it fails, as per the OP.
    Last edited by TMS; 07-12-2024 at 07:26 PM.

  9. #9
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: Macro can't run otherwise working formula

    This appears to be a workaround, but I'm sure there must be a better way.

    Sub Macro2()
    '
    ' Macro1 Macro
    '
    
    '
    Range("A4").Select
    ActiveCell.FormulaArray = _
        "=LET(d,FILTER('Policy List'!RC:R[4996]C,'Policy List'!RC:R[4996]C<>"""",""no data""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))"
    ActiveCell.Formula2 = ActiveCell.Formula2
    Range("C4").Select
    ActiveCell.FormulaArray = _
        "=LET(d,FILTER('Policy List'!RC:R[4996]C,'Policy List'!RC:R[4996]C<>"""",""no data""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))"
    ActiveCell.Formula2 = ActiveCell.Formula2
    Range("F4").Select
    ActiveCell.FormulaArray = _
        "=LET(d,FILTER('Policy List'!RC:R[4996]C,'Policy List'!RC:R[4996]C<>"""",""no data""),r,ROUNDUP(ROWS(d)*50%,0),TAKE(SORTBY(d,RANDARRAY(ROWS(d))),r))"
    ActiveCell.Formula2 = ActiveCell.Formula2
    Range("F5").Select
    End Sub

  10. #10
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    31,174

    Re: Macro can't run otherwise working formula

    Odd ... when I tried FormulaArray on my test it did not work i.e did not spill - whereas Formula2 did!!!

  11. #11
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: Macro can't run otherwise working formula

    Formula2 with an A1 formula works. Formula2R1C1 with an R1C1 formula errors. I don't know why.

    FormulaArray with R1C1 formula "works" but doesn’t Spill. It outputs it as an Array Entered formula. But, having got it into the cell, .Formula2 = .Formula does work and Spills.

    I don't usually use R1C1 format formulae so I haven't encountered this behaviour.

  12. #12
    Registered User
    Join Date
    06-25-2024
    Location
    USA
    MS-Off Ver
    Windows 10
    Posts
    13

    Re: Macro can't run otherwise working formula

    So sorry - I got locked out for a little while there! I've attached a workbook mockup with dummy information:

    Workbook Example.xlsm

    Is that what is needed?

  13. #13
    Registered User
    Join Date
    06-25-2024
    Location
    USA
    MS-Off Ver
    Windows 10
    Posts
    13

    Re: Macro can't run otherwise working formula

    Actually, I think the solution in post #7 works! I plugged it in to my original workbook and it's doing what I need it to. Thank you both so much!

  14. #14
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    48,945

    Re: Macro can't run otherwise working formula

    Quote Originally Posted by LadyLissa82 View Post
    Actually, I think the solution in post #7 works! I plugged it in to my original workbook and it's doing what I need it to. Thank you both so much!
    You're welcome. Thanks for the rep.

  15. #15
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2507 (Windows 11 Home 24H2 64-bit)
    Posts
    91,800

    Re: Macro can't run otherwise working formula

    If that takes care of your original question, please choose Thread Tools from the menu link above and mark this thread as SOLVED. You can also access the SOLVED tag by editing the opening post and choosing SOLVED from the drop-down to the left of the title box.

    Also, if you have not already done so, remember that you can reward anyone who offered you help towards a solution for your issue by clicking the small star icon (* Add Reputation) located in the lower left corner of the post in which the help was given. By doing so you can add to the reputation(s) of each of those who offered help.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro not working when called by IF formula
    By millerjj22 in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-01-2017, 03:31 PM
  2. Macro Mid Formula Not Working
    By joshag in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-09-2016, 09:59 AM
  3. [SOLVED] help index and match macro not working but formula working??
    By JEAN1972 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-28-2016, 06:37 AM
  4. [SOLVED] Formula not working in Macro
    By mstoto in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-06-2015, 09:50 AM
  5. Formula in macro not working
    By kosherboy in forum Excel Programming / VBA / Macros
    Replies: 15
    Last Post: 02-06-2014, 03:44 PM
  6. [SOLVED] Formula not working within macro
    By theletterh in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-13-2013, 12:32 PM
  7. formula is not working in macro
    By Jamnes in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-30-2012, 10:32 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1