Consulting

Results 1 to 7 of 7

Thread: Excel 365 - VBA to assign a formula

  1. #1
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location

    Excel 365 - VBA to assign a formula

    I am trying to assign a forumla in Excel 365 using VBA but run into something that I don't quite understand....
    ActiveCell.Formula = "=Unique(A3:D" & LastRow & ")"  ' I wanted to do this
    
    ActiveCell.Formula = "'=Unique(A3:D" & LastRow & ")"  ' at the end I use this then I delete the '
    so, the first formula gets put into my cell, but it contains this symbol "@", because of that symbol, I only have one cell. As soon as I delete the symbol, my whole range appears??!! So, I did the second way to avoid that problem and works.
    so, why is Excel putting that symbol into my formula and yet, it does not give me what I want.... How does one overcome this quirk to make it work?

  2. #2
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    Last bullet

    UNIQUE is a dynamic array command so I think Excel is trying to help you

    https://www.excelcampus.com/function...-spill-ranges/

    Dynamic Array Formulas Are Coming!As I mentioned, these functions are not yet available to the general public. The current availability is limited to a portion of users on Microsoft's Office Insiders Program (Insider channel). The program is free for Office 365 subscribers. There is no set release date to all Office 365 users yet, but hopefully that will be soon.
    As of now, there are 7 new dynamic array functions:

    • Filter – allows you to filter a range of data based on criteria you define.
    • RandArray – returns an array of random numbers.
    • Sequence – allows you to generate a list of sequential numbers in an array, such as 1, 2, 3, 4.
    • Sort – sorts the contents of a range or array.
    • SortBy – sorts based on the values in a corresponding range or array.
    • Unique – returns a list of unique values in a list or range.
    • Single – returns a single value at the intersection of a cell's row or column. Update: The Single function has been removed from Excel and the @ symbol is now used instead for backward compatibility.


    There's also the # marker (?) to refer to the entire Spill range

    However, Excel's implementation of the spill range and spill refs (A4#) is different (at the time of this writing). It opens up a whole new world of possibility and simplicity with Excel formulas and other features.


    Edit --------------------------------------------------------------------------------

    Use .Formula2 =

    ActiveCell.Formula2 = "=Unique(A3:D" & LastRow & ")" ' I wanted to do this
    Last edited by Paul_Hossler; 09-28-2021 at 01:01 PM. Reason: Didn't paste it all in
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  3. #3
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Paul:
    Thanks, that did the trick!

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    FWIW, I really like the new 365 functions, but not everyone has 365 so I have to do things the old way
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  5. #5
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Quote Originally Posted by Paul_Hossler View Post
    FWIW, I really like the new 365 functions, but not everyone has 365 so I have to do things the old way
    I like it too.... However, with the "@" sign, I only get one Unique cell, instead of my range that I specified, that I don't like

  6. #6
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    (Guessing again)

    I think Excel was trying to be helpful


    In your line ...

    ActiveCell.Formula = "=Unique(A3:D" & LastRow & ")"  ' I wanted to do this


    ... Excel probably assumed only a single cell was required since you used the pre-dynamic array .Formula (no '2')



    https://a4accounting.com.au/new-form...mbol-in-excel/

    The extended use symbol @

    ....
    ....

    Summing up
    The # symbol follows the reference and it makes it incredibly easy to refer to spilled ranges. You use the # after the reference to the cell in the top left corner of the spilled range.


    The @ symbol precedes the reference and it forces Excel to treat the reference as it would have pre-dynamic arrays.

    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  7. #7
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    Thank you for the link, now I understand and know what to do, Paul.

Posting Permissions

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