+ Reply to Thread
Results 1 to 11 of 11

VBA - See if a cell has a formula that refers to another sheet

  1. #1
    Registered User
    Join Date
    12-05-2017
    Location
    London
    MS-Off Ver
    I don't Know
    Posts
    9

    VBA - See if a cell has a formula that refers to another sheet

    Hello

    I am simply trying to make a macro that colours cells in the current selection in blue, for those cells having a formula with external (i.e. other sheets or workbooks) references. The current code is below, but where I am stuck is: how can i find a way to define "hasformula" better and add the "external link" component. Any idea? That'd be super helpful ! Thanks a lot

    Please Login or Register  to view this content.
    End sud

  2. #2
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: VBA - See if a cell has a formula that refers to another sheet

    Try:
    Please Login or Register  to view this content.
    You can say "THANK YOU" for help received by clicking the Star symbol at the bottom left of the helper's post.
    Practice makes perfect. I'm very far from perfect so I'm still practising.

  3. #3
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: VBA - See if a cell has a formula that refers to another sheet

    As far as I know there isnt a property or method that evaluates a formulas contents for this. You would need to take the formula from the cell and break it down to evaluate it yourself. As far as I can tell conditional formatting also doesnt have a way to evaluate this.

    I think what you are asking to do is simple to say but would be complex to do. Judging from your question I would guess we cannot presume or look for only a certain sheet or workbook?

    The external workbook part can be done (I think I have done that before). I would imagine the hard part is the evaluating if a sheet reference is within the same workbook.

    Why is it you are trying to do this, maybe there is an easier way? Also if your not sure what version of Excel you have, please try and find out so we dont waste your time (and ours) recommending things that are not options for you.

  4. #4
    Forum Expert
    Join Date
    10-02-2014
    Location
    USA
    MS-Off Ver
    2016
    Posts
    1,222

    Re: VBA - See if a cell has a formula that refers to another sheet

    So I checked and do have code to check if a formula has external references in it. This alone is pretty complex (as I was extracting them and checking if they were valid links still). Consider the following:
    • A formula could have multiple external references within it
    • The syntax for an external reference is different if the reference is also a table
    • Its almost impossible in my opinion to evaluate say the result of an INDIRECT as an external reference (as the resulting reference only exists during calculation).

    My code is long, complex and quite frankly would likely not help anyone to just dump it here so I will share the pieces I think will help. I would first evaluate if the "string" of the formula has within it any of the following (".xlsx", ".xlsm", ".xlsb", ".xls") as any external reference to another workbook will have the file name in it ending with one of those extensions (maybe more, but those are the common ones).

    This could simply be an IF OR statement like (after having checked Hasformula):

    Please Login or Register  to view this content.
    That alone may be enough for you to consider the formula having an external reference and coloring it. I presume it doesnt matter to you how many external references are in the cell.

    A sheet would be more difficult to evaluate as the syntax is different depending on if the sheet name has spaces or not and is also uses pretty common characters/syntax that may be hard to set apart from other text in a formula/cell. With a space the syntax is
    Please Login or Register  to view this content.
    so you may be able to look for the single quote flowed by the quote with the exclamation and grab everything in between. However without a space its just a sheetname! so the only character that signifies it is a ! after the name, which makes it very difficult to tell when the sheet name starts and ends.

    That part may take some research to see if there is a viable way.

  5. #5
    Registered User
    Join Date
    12-05-2017
    Location
    London
    MS-Off Ver
    I don't Know
    Posts
    9

    Re: VBA - See if a cell has a formula that refers to another sheet

    thanks a lot both, this is helpful. I will read trhrough tonight and give it a go with your recommendations!

  6. #6
    Registered User
    Join Date
    12-05-2017
    Location
    London
    MS-Off Ver
    I don't Know
    Posts
    9

    Re: VBA - See if a cell has a formula that refers to another sheet

    I have used this, but replacing "Sheet" by "!" which is a caracter that only ever appears in a formula when referencing to another sheet (whether same or different workbook), and so far it works !! Thanks a lot again both, truly helpful. Don't hesitate to comment if you think there is any flaw of course

    Please Login or Register  to view this content.

  7. #7
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA - See if a cell has a formula that refers to another sheet

    The possible flaws I can think of would be:
    1. It is possible that the sheet name before the exclamation mark is the same sheet the formula is on.
    2. A named range can be on another sheet and if a formula uses that there would not necessarily be an exclamation mark- unless it's in another workbook.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

  8. #8
    Registered User
    Join Date
    12-05-2017
    Location
    London
    MS-Off Ver
    I don't Know
    Posts
    9

    Re: VBA - See if a cell has a formula that refers to another sheet

    makes sense, brilliant. Thanks !
    I don't find the "solved" button in this forum, need a new pair of glasses

  9. #9
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: VBA - See if a cell has a formula that refers to another sheet

    Use the Thread Tools dropdown at the top of the page.

  10. #10
    Registered User
    Join Date
    12-05-2017
    Location
    London
    MS-Off Ver
    I don't Know
    Posts
    9

    Re: VBA - See if a cell has a formula that refers to another sheet

    thank you, done

  11. #11
    Forum Expert Mumps1's Avatar
    Join Date
    10-10-2012
    Location
    Toronto, Canada
    MS-Off Ver
    Excel 2010, 2013
    Posts
    7,820

    Re: VBA - See if a cell has a formula that refers to another sheet

    Glad it worked out.

+ 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. [SOLVED] Formula that refers to a cell on a sheet that may or may not exist.
    By k1989l in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 10-01-2015, 02:50 PM
  2. Vlookup Different workbook Cell refers to sheet
    By RandomPezzer in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 07-12-2015, 05:42 PM
  3. [SOLVED] Add formula to cell that refers to first empty cell in another row.
    By Onweerwolf in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 12-28-2014, 05:51 PM
  4. Forumla which refers to a cell which refers to a sheet name
    By awiller2 in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 12-03-2012, 09:57 PM
  5. [SOLVED] COUNTA calculation - Formula in cell refers to arange with additional numbers adjacent?
    By hennakao in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 04-27-2012, 07:07 AM
  6. Replies: 2
    Last Post: 11-23-2005, 08:18 PM
  7. [SOLVED] Cell has a value of #N/A when the formula refers to nothing
    By Man Utd in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-16-2005, 05:05 PM

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