8

I'm trying to create some conditional formatting at runtime (huzzah) for an add-in (double huzzah), and have found that, apparently, some functions cannot be used as they would in a normal worksheet. (I just get an invalid procedure call error 5 when trying to create the CF referencing a VBA function I could call in a cell, even though it's in the add-in and not the workbook; I can create the CF fine with a built-in function.) The clearest confirmation I've found for this is here, but it doesn't really explain what the problem is; that's the esoteric part, would love to hear more about what I can expect with this.

The rubber-meets-road part is: can I avoid VBA altogether, and use a series of Excel-only, built-in functions to verify whether a given cell contains a constant (i.e. a value entered by a user), a formula (i.e. some kind of calculation, logical operation, etc.--pretty much starts with an =), or a link (i.e. a reference to a cell in another worksheet or another workbook)? I know Excel has this determination at its fingertips; witness the uses and speed of GoTo/Special. How can I get at it though?

Thanks in advance for your help.

Community
  • 1
  • 1
downwitch
  • 1,362
  • 3
  • 19
  • 40
  • The CELL() function could be useful if the constant is a string, see here: http://www.informit.com/articles/article.aspx?p=440961 (Use CELL("type", A1), where A1 is your cell.) I'm not sure what's the difference between a "formula" and a "link" in your question. – Gintautas Miliauskas Oct 18 '10 at 22:19
  • Yes, I know the CELL function, thanks--had started with that, but it's (a) slow over large ranges and (b) not specific enough. In my nomenclature, all links are formulas, but not all formulas are links. A very simple example: This is a formula: =SUM(A1:A5) This is a link: =SUM(A1:A5,Sheet1!B6) – downwitch Oct 18 '10 at 22:40
  • What do you expect for indirect references where the actual cell used is perhaps no known until runtime (ie .. it may be what you call a "link" sometimes) – Dr. belisarius Oct 22 '10 at 12:58
  • For my purposes, use of the INDIRECT() formula constitutes a formula, not a link. I'm not trying to build a trace routine. And Excel knows full well whether a cell's "formula" represents some kind of calculation or a constant value. The fact that that information is not exposed to users as a formula is frustrating, and silly. All it takes to determine "link" status is an exclamation point in the formula. Again, not difficult... once you can actually read a formula string. – downwitch Oct 25 '10 at 13:23

3 Answers3

1

Updated for Excel 2013:

For Office versions 2013 and higher, the ISFORMULA¹ function is available. Combining this with the NOT function, AND function and either the COUNTBLANK, ISBLANK or LEN function can produce a formula to determine whether a cell contains a constant.

The standard formulas in E2:F2 are,

=ISFORMULA(D2)
=AND(NOT(ISFORMULA(D2)), LEN(D2))

      ISFORMULA_update

If further information on the nature of the cell value is required the TYPE function can be used to determine if the cell contents are a number, text, boolean, error or array.

When used in concert the native worksheet functions discussed here can reproduce the results available from VBA's Range.SpecialCells method and its xlCellTypeConstants or xlCellTypeFormulas xlCellType enumeration.


¹ The ISFORMULA function was introduced with Excel 2013. It is not available in earlier versions.

Community
  • 1
  • 1
  • At long last, a winner--someone at MSFT was just as frustrated as I, I suppose. I've been using 2013 for a while and didn't know about this, thanks! – downwitch Feb 25 '16 at 18:22
0

Not sure if this is what you want, but it seems to do what you are asking, at least some of it.

http://www.ozgrid.com/VBA/special-cells.htm

It's the range.specialcells method.

It returns a range that contains only constants, or only formulas, etc.

An example of how this code would be used is shown below:

Sub CheckForConstants()
    Dim x As Range
    Set x = Selection.SpecialCells(xlCellTypeConstants, xlNumbers)
    MsgBox "address of cells that contain numbers only is " & x.Address
    Set x = Selection.SpecialCells(xlCellTypeConstants)
    MsgBox "address of cells that contain constant of any type is " & x.Address
End Sub

You select a range and then execute this macro and it will return the address of those cells that meet the requirements.

The first x looks for cells that contains numbers only. The second x looks for cells that contains any constants

The range in this case was selection, but you can set to what you want, i.e. range("a1:b5"), etc.

I went back to the worksheet and used the goto special method.

Apparently it also uses the range.special method.

I used the record macro option and this is what I got.

Selection.SpecialCells(xlCellTypeConstants, 23).Select
    Range("M7").Select
    Selection.SpecialCells(xlCellTypeFormulas, 23).Select
    Range("I6:J16").Select
    Selection.SpecialCells(xlCellTypeConstants, 1).Select
    Range("L9").Select
    ActiveWindow.ScrollWorkbookTabs Position:=xlFirst
    Sheets("CP").Select
    Application.CutCopyMode = False
    Range("I21").Select
    ActiveSheet.DrawingObjects.Select
    Application.Goto Reference:="GoToSpecialRoutine"

The goto special feature on the worksheet uses the special cells method for some of what it does.

It also uses others as well. In the last 5 lines of codes I changed worksheet and asked it to go to objects.

It doesn't really go to them. It just selects them.

worksheet CP contained objects and it used the code in the last 3 lines to select all the objects on the worksheet.

Best bet to see the code behind goto special is to record a macro and then use the goto / special feature in the worksheet.

When finished, Stop recording and view the macro you recorded.

I don't know of any other features to select by type of cell, but I'm just a newby so it could be there very easily and not be known by me.

guitarthrower
  • 5,624
  • 3
  • 29
  • 37
THEO
  • 1
  • Thanks, but as the bolded part of the question above states, the solution must be Excel-only and built-in, not VBA. I have VBA functions that determine these cell-content types (Constant,Formula,Formula with link) just fine. The original post explains why. – downwitch Oct 20 '10 at 14:29
0

I don't think you can avoid VBA altogether but you can create a simple UDF and use it within Excel

Eg

Function IsFormula(Check_Cell As Range)
  IsFormula = Check_Cell.HasFormula
End Function

and

Function IsLink(Check_Cell As Range)

  If InStr(1, Check_Cell.Formula, "!", vbTextCompare) Then
   IsLink = Check_Cell.HasFormula
  End If
End Function

=IsFormula(A1) will return TRUE if there is a formula in A1 and FALSE otherwise =IsLink(A1) will return TRUE if there is a formula in A1 containing '!' otherwise FALSE

You could combine these and create a string output "Formula","Link","Value"

  • Thanks, but as the original post states, you cannot call VBA functions (which I have written also) for conditional formatting from an add-in. – downwitch Apr 18 '11 at 02:23
  • You don't have to call a function. Force an Excel recalculation and the UDF should update automatically. –  Apr 18 '11 at 09:57
  • How does that help? I'm trying to set conditional formats, not put UDFs into cells. You can't embed a UDF in a conditional-formatting formula call if the UDF lives in an Excel add-in. I urge you try it; it produces this error: "You cannot use reference to other worksheets or workbooks for Conditional Formatting criteria." – downwitch Apr 18 '11 at 13:35
  • Interesting, I didn't know that about conditional formatting. looks like your only option is to build a "scan and format" function that you run over all used cells in each sheet of the workbook. not really what your after, but no i don't believe you could achieve your requirements using only excel builtin functions. Also try not being so agressive, just because some of the people answering your question apparently can't read a question is no reason to have a go at them. – Anonymous Type Jul 15 '13 at 04:07