Pre-built code for checking conditional formatting not working - Type error










0















I'm attempting to use a VBA function created by Bob Phillips to check if conditional formatting has been met in a particular cell. This seems to be a respectable solution, as Google turns up a number of different references to it, but it's throwing a Type error for me. I'm using Excel 2007, and the last update made to the code was in 2005, so I assume there are some compatibility issues involved. I have already fixed one syntax error (Set rng = rng(1, 1) wasn't working until I inserted .Cells) but I'm unable to fix this one because I can't parse what the code is doing.



The function is designed specifically to evaluate conditionals based on a formula, which mine is. Here is the full code:



Public Function IsCFMet2(rng As Range) As Boolean

Dim oFC As FormatCondition
Dim sF1 As String
Dim iRow As Long
Dim iColumn As Long

Set rng = rng.Cells(1, 1)
If rng.FormatConditions.Count > 0 Then
For Each oFC In rng.FormatConditions
If oFC.Type = xlExpression Then
're-adjust the formula back to the formula that applies
'to the cell as relative formulae adjust to the activecell
With Application
iRow = rng.Row
iColumn = rng.Column
sF1 = .Substitute(oFC.Formula1, "ROW()", iRow)
sF1 = .Substitute(sF1, "COLUMN()", iColumn)
sF1 = .ConvertFormula(sF1, xlA1, xlR1C1)
sF1 = .ConvertFormula(sF1, xlR1C1, xlA1, , rng)
End With
**** IsCFMet2 = rng.Parent.Evaluate(sF1)
End If
If IsCFMet2 Then Exit Function
Next oFC
End If

End Function


The Type Error is thrown by the starred line. I don't fully grasp the With Application block or how it's contributing to the detection of a fulfilled conditional formatting rule, so I don't know what the problem is or how to fix it without breaking the code.



Here is the test macro I'm using to check the function:



Sub Test():

Dim Tester As Range

Set Tester = ActiveCell

If IsCFMet2(Tester) Then MsgBox "CF is met!"

End Sub


However I've gotten the same error with various range inputs, so I'm pretty confident the input itself is not the problem - I'm including it just as a precaution.










share|improve this question






















  • Your code ran successfully for me when applied to a cell with a very simple conditional format (Formula1="=G8>16"). Can you add examples of the conditional format your working with?

    – xidgel
    Nov 13 '18 at 18:00











  • @xidgel Oh dear. It's comparing values in the active sheet with values in another - full formula is =C5<>INDEX(INDIRECT("Table234"),$B5,C$4) and I had a hell of a time getting that into a form Conditional Formatting would accept... I'm going to be pretty disappointed if that's what's breaking the check.

    – Alan T.
    Nov 13 '18 at 18:52















0















I'm attempting to use a VBA function created by Bob Phillips to check if conditional formatting has been met in a particular cell. This seems to be a respectable solution, as Google turns up a number of different references to it, but it's throwing a Type error for me. I'm using Excel 2007, and the last update made to the code was in 2005, so I assume there are some compatibility issues involved. I have already fixed one syntax error (Set rng = rng(1, 1) wasn't working until I inserted .Cells) but I'm unable to fix this one because I can't parse what the code is doing.



The function is designed specifically to evaluate conditionals based on a formula, which mine is. Here is the full code:



Public Function IsCFMet2(rng As Range) As Boolean

Dim oFC As FormatCondition
Dim sF1 As String
Dim iRow As Long
Dim iColumn As Long

Set rng = rng.Cells(1, 1)
If rng.FormatConditions.Count > 0 Then
For Each oFC In rng.FormatConditions
If oFC.Type = xlExpression Then
're-adjust the formula back to the formula that applies
'to the cell as relative formulae adjust to the activecell
With Application
iRow = rng.Row
iColumn = rng.Column
sF1 = .Substitute(oFC.Formula1, "ROW()", iRow)
sF1 = .Substitute(sF1, "COLUMN()", iColumn)
sF1 = .ConvertFormula(sF1, xlA1, xlR1C1)
sF1 = .ConvertFormula(sF1, xlR1C1, xlA1, , rng)
End With
**** IsCFMet2 = rng.Parent.Evaluate(sF1)
End If
If IsCFMet2 Then Exit Function
Next oFC
End If

End Function


The Type Error is thrown by the starred line. I don't fully grasp the With Application block or how it's contributing to the detection of a fulfilled conditional formatting rule, so I don't know what the problem is or how to fix it without breaking the code.



Here is the test macro I'm using to check the function:



Sub Test():

Dim Tester As Range

Set Tester = ActiveCell

If IsCFMet2(Tester) Then MsgBox "CF is met!"

End Sub


However I've gotten the same error with various range inputs, so I'm pretty confident the input itself is not the problem - I'm including it just as a precaution.










share|improve this question






















  • Your code ran successfully for me when applied to a cell with a very simple conditional format (Formula1="=G8>16"). Can you add examples of the conditional format your working with?

    – xidgel
    Nov 13 '18 at 18:00











  • @xidgel Oh dear. It's comparing values in the active sheet with values in another - full formula is =C5<>INDEX(INDIRECT("Table234"),$B5,C$4) and I had a hell of a time getting that into a form Conditional Formatting would accept... I'm going to be pretty disappointed if that's what's breaking the check.

    – Alan T.
    Nov 13 '18 at 18:52













0












0








0








I'm attempting to use a VBA function created by Bob Phillips to check if conditional formatting has been met in a particular cell. This seems to be a respectable solution, as Google turns up a number of different references to it, but it's throwing a Type error for me. I'm using Excel 2007, and the last update made to the code was in 2005, so I assume there are some compatibility issues involved. I have already fixed one syntax error (Set rng = rng(1, 1) wasn't working until I inserted .Cells) but I'm unable to fix this one because I can't parse what the code is doing.



The function is designed specifically to evaluate conditionals based on a formula, which mine is. Here is the full code:



Public Function IsCFMet2(rng As Range) As Boolean

Dim oFC As FormatCondition
Dim sF1 As String
Dim iRow As Long
Dim iColumn As Long

Set rng = rng.Cells(1, 1)
If rng.FormatConditions.Count > 0 Then
For Each oFC In rng.FormatConditions
If oFC.Type = xlExpression Then
're-adjust the formula back to the formula that applies
'to the cell as relative formulae adjust to the activecell
With Application
iRow = rng.Row
iColumn = rng.Column
sF1 = .Substitute(oFC.Formula1, "ROW()", iRow)
sF1 = .Substitute(sF1, "COLUMN()", iColumn)
sF1 = .ConvertFormula(sF1, xlA1, xlR1C1)
sF1 = .ConvertFormula(sF1, xlR1C1, xlA1, , rng)
End With
**** IsCFMet2 = rng.Parent.Evaluate(sF1)
End If
If IsCFMet2 Then Exit Function
Next oFC
End If

End Function


The Type Error is thrown by the starred line. I don't fully grasp the With Application block or how it's contributing to the detection of a fulfilled conditional formatting rule, so I don't know what the problem is or how to fix it without breaking the code.



Here is the test macro I'm using to check the function:



Sub Test():

Dim Tester As Range

Set Tester = ActiveCell

If IsCFMet2(Tester) Then MsgBox "CF is met!"

End Sub


However I've gotten the same error with various range inputs, so I'm pretty confident the input itself is not the problem - I'm including it just as a precaution.










share|improve this question














I'm attempting to use a VBA function created by Bob Phillips to check if conditional formatting has been met in a particular cell. This seems to be a respectable solution, as Google turns up a number of different references to it, but it's throwing a Type error for me. I'm using Excel 2007, and the last update made to the code was in 2005, so I assume there are some compatibility issues involved. I have already fixed one syntax error (Set rng = rng(1, 1) wasn't working until I inserted .Cells) but I'm unable to fix this one because I can't parse what the code is doing.



The function is designed specifically to evaluate conditionals based on a formula, which mine is. Here is the full code:



Public Function IsCFMet2(rng As Range) As Boolean

Dim oFC As FormatCondition
Dim sF1 As String
Dim iRow As Long
Dim iColumn As Long

Set rng = rng.Cells(1, 1)
If rng.FormatConditions.Count > 0 Then
For Each oFC In rng.FormatConditions
If oFC.Type = xlExpression Then
're-adjust the formula back to the formula that applies
'to the cell as relative formulae adjust to the activecell
With Application
iRow = rng.Row
iColumn = rng.Column
sF1 = .Substitute(oFC.Formula1, "ROW()", iRow)
sF1 = .Substitute(sF1, "COLUMN()", iColumn)
sF1 = .ConvertFormula(sF1, xlA1, xlR1C1)
sF1 = .ConvertFormula(sF1, xlR1C1, xlA1, , rng)
End With
**** IsCFMet2 = rng.Parent.Evaluate(sF1)
End If
If IsCFMet2 Then Exit Function
Next oFC
End If

End Function


The Type Error is thrown by the starred line. I don't fully grasp the With Application block or how it's contributing to the detection of a fulfilled conditional formatting rule, so I don't know what the problem is or how to fix it without breaking the code.



Here is the test macro I'm using to check the function:



Sub Test():

Dim Tester As Range

Set Tester = ActiveCell

If IsCFMet2(Tester) Then MsgBox "CF is met!"

End Sub


However I've gotten the same error with various range inputs, so I'm pretty confident the input itself is not the problem - I'm including it just as a precaution.







excel vba compatibility






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 16:57









Alan T.Alan T.

1036




1036












  • Your code ran successfully for me when applied to a cell with a very simple conditional format (Formula1="=G8>16"). Can you add examples of the conditional format your working with?

    – xidgel
    Nov 13 '18 at 18:00











  • @xidgel Oh dear. It's comparing values in the active sheet with values in another - full formula is =C5<>INDEX(INDIRECT("Table234"),$B5,C$4) and I had a hell of a time getting that into a form Conditional Formatting would accept... I'm going to be pretty disappointed if that's what's breaking the check.

    – Alan T.
    Nov 13 '18 at 18:52

















  • Your code ran successfully for me when applied to a cell with a very simple conditional format (Formula1="=G8>16"). Can you add examples of the conditional format your working with?

    – xidgel
    Nov 13 '18 at 18:00











  • @xidgel Oh dear. It's comparing values in the active sheet with values in another - full formula is =C5<>INDEX(INDIRECT("Table234"),$B5,C$4) and I had a hell of a time getting that into a form Conditional Formatting would accept... I'm going to be pretty disappointed if that's what's breaking the check.

    – Alan T.
    Nov 13 '18 at 18:52
















Your code ran successfully for me when applied to a cell with a very simple conditional format (Formula1="=G8>16"). Can you add examples of the conditional format your working with?

– xidgel
Nov 13 '18 at 18:00





Your code ran successfully for me when applied to a cell with a very simple conditional format (Formula1="=G8>16"). Can you add examples of the conditional format your working with?

– xidgel
Nov 13 '18 at 18:00













@xidgel Oh dear. It's comparing values in the active sheet with values in another - full formula is =C5<>INDEX(INDIRECT("Table234"),$B5,C$4) and I had a hell of a time getting that into a form Conditional Formatting would accept... I'm going to be pretty disappointed if that's what's breaking the check.

– Alan T.
Nov 13 '18 at 18:52





@xidgel Oh dear. It's comparing values in the active sheet with values in another - full formula is =C5<>INDEX(INDIRECT("Table234"),$B5,C$4) and I had a hell of a time getting that into a form Conditional Formatting would accept... I'm going to be pretty disappointed if that's what's breaking the check.

– Alan T.
Nov 13 '18 at 18:52












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285998%2fpre-built-code-for-checking-conditional-formatting-not-working-type-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53285998%2fpre-built-code-for-checking-conditional-formatting-not-working-type-error%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Museum of Modern and Contemporary Art of Trento and Rovereto

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully