+ Reply to Thread
Results 1 to 12 of 12

HasFormula to find only specific formulas

  1. #1
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    HasFormula to find only specific formulas

    I am trying to format cells based on whether they contain a refernce to a named range - i.e.
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    I know how to use HasFormula to find any cells containing formulas but don't know how to narrow this down. So I currently have:

    Please Login or Register  to view this content.
    Any ideas on how I can exclude all formulas except references to named ranges?

    Strud

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: HasFormula to find only specific formulas

    You don't need to test with HasFormula, you can set your range using the SpecialCells method and choose only formula cells:

    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: HasFormula to find only specific formulas

    [QUOTE=JBeaucaire;3845576]You don't need to test with HasFormula, you can set your range using the SpecialCells method and choose only formula cells:

    Thanks that does work, but is there a way to do it without specifying the range names in the code, as this isn't really suitable as a solution for me(?)

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: HasFormula to find only specific formulas

    The solution offered is based on the question asked. Can you rephrase your specifications?

  5. #5
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: HasFormula to find only specific formulas

    Well, I tried to say it in the first specification - maybe it is not clear sorry.

    But I am looking for a way to test if the cells contain a named range of any description NOT a specific range with a specific name.

    So that when I add a new defined range I do not have to add it into the code. So the code tests whether the cell contains a reference to ANY named range.

    I could standardise my named ranges and test for a formula containing a specific word- could work but I don't know how to code it.

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,259

    Re: HasFormula to find only specific formulas

    You'd have to loop through the names in the workbook populating an array/collection/dictionary or whatever with the names, then for each formula cell you'd have to test each name (exiting after you found one).
    Remember what the dormouse said
    Feed your head

  7. #7
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: HasFormula to find only specific formulas

    Ok thanks romperstomper, I was hoping that was not going to be the case, seems like less work to go with JBeaucaire's solution to be honest, so I'll just enter the names into the code.

    Thanks Guys!

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: HasFormula to find only specific formulas

    I don't know, this seems like it should work and is pretty simple:
    Please Login or Register  to view this content.

  9. #9
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,259

    Re: HasFormula to find only specific formulas

    To be thorough, you'd need to cater for local names used on the same worksheet too - e.g. if you have a range named Sheet1!somename and a formula on sheet1 refers to it, the formula would probably only use =somename, not =Sheet1!somename (though it could use either).

  10. #10
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: HasFormula to find only specific formulas

    Thanks JBeaucaire that works perfectly.

    Romper I hear what you're saying but JBeaucaire's solution works fine for my needs in this instance.

    Cheers guys!

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: HasFormula to find only specific formulas

    That code would also find a name that is a substring of another name, e.g., Bob in Bobo.
    Entia non sunt multiplicanda sine necessitate

  12. #12
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: HasFormula to find only specific formulas

    Great tip, shg. I believe named ranges that are possibly substrings of other named ranges would be ok in this project since he's flagging all named ranges references anyway.

    If the formula references a named range that is a substring of an actual SHEET name, then we would get false results here.

    So a Named Range of "MyCat" would accidentally give a TRUE match in a cell that referenced a sheet called "MyCatalogs". Strud, be sure to check for that possibility by reviewing your named ranges and your sheet names.

+ 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. Replies: 2
    Last Post: 03-05-2013, 07:52 AM
  2. Find all instances of formulas refering to a specific cell?
    By ThomasCarter in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-16-2013, 11:47 AM
  3. [SOLVED] simple... HasFormula ?
    By mark kubicki in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-11-2006, 11:10 AM
  4. [SOLVED] Help with ActiveCell.HasFormula
    By Paul Fenton in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-20-2006, 10:10 AM
  5. HasFormula
    By John Wirt in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-07-2006, 12:10 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