Inserting Cell Text into an Existing Formula

kazoli

New Member
Joined
Jan 22, 2020
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am struggling with the syntax to combine a COUNTIF function with an INDIRECT function embedded within (or something similiar). Here's my current formula in cell B1:

=IF(A1 = "all",COUNTIFS(Sheet1!Q:Q , Sheet2!M4))
(If A1 in the current sheet = "all", then run a countif to see how many instances of Sheet2!M4 exist within column Q in Sheet1)

What I would like to do is store text in different cells that I could "tack on" to this formula as needed. E.g. Cell A2 contains the text ",Sheet1!R:R, "<>" & "exclude". Then, in my main countifs formula in B1, I'd like to be able to reference Cell A2 in order to add more criteria to the countifs. So, something like =IF(A1 = "all",COUNTIFS(Sheet1!Q:Q , Sheet2!M4 & A2)) would actually be read as =IF(A1 = "all",COUNTIFS(Sheet1!Q:Q , Sheet2!M4 , Sheet1!R:R , "<>" & "exclude"))

I've tried various combinations of using INDIRECT with no luck. Any insight is greatly appreciated - thank you!
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Welcome to the MrExcel Board!

INDIRECT (or OFFSET, or INDEX) is used to dynamically change a range reference, not an entire formula. You're asking to change the formula itself. This is not entirely impossible, but it's tricky and depends on your exact needs as to whether it will work. It also requires some variant of VBA.

Open a copy of your workbook. Press Alt-F11 to open the VBA editor. Press Alt-IM to insert a module. In the sheet that opens, paste this code:

VBA Code:
Public Function MyCountIfs(ByVal r1 As Range)

    Application.Volatile
    myfunc = "=IF(A1=""all"",COUNTIFS("
    For Each c In r1
        If c <> "" Then myfunc = myfunc & c.Value & ","
    Next c
    Mid(myfunc, Len(myfunc), 1) = ")"
    MyCountIfs = Evaluate(myfunc & ")")
   
End Function

Press Alt-Q to close the editor. Now look at this sheet:

Book1
AB
1all4
2Sheet1!Q:Q,Sheet2!M4
3Sheet1!R:R,"<>exclude"
Sheet5
Cell Formulas
RangeFormula
B1B1=MyCountifs(A2:A5)


Voila! There you go. You can add more ranges in A2:A5 and the UDF will add them to the formula it is dynamically creating, then the EVALUATE function in VBA will give you the answer. Not really friendly though. If you use a UDF (User Defined Function) it may be easier to code your actual requirements rather than building a formula.

The EVALUATE function isn't directly available from the worksheet, although there are ways to do it. See here:


But if you go that route, be aware that it still makes your workbook a macro-enabled workbook. You'd also need the TEXTJOIN function to put the pieces of your COUNTIFS together. You'd need Excel 365 or Excel 2019 for that.

Not the answer you were looking for, but those are your options.
 
Upvote 0

Forum statistics

Threads
1,223,884
Messages
6,175,175
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top