Excel Indirect with Count - excel-formula

I have a workbook with different sheets for each month. I made another sheet where I put the months in A1 down.
I wanted to use Counta (for A:A in the months' sheets) and indirect together but I am getting the result "1" with my formula.
Please help:
=COUNTA(INDIRECT(SheetNames!A1&"!A:A"))
I would also eventually like to do the same for countif. If you can please explain what I missed that will help me.
Thanks!

Trying out your formula it works perfectly unless you've got a space in the worksheet name.
Try this formula:
=COUNTA(INDIRECT("'" & SheetNames!A1 & "'!A:A"))
When a sheet name contains a space then the sheet name must be enclosed within apostrophes. So =COUNTA('Sheet 4'!A:A) is a valid formula, while =COUNTA(Sheet 4!A:A) will not work and causes all kinds of problems.
The INDIRECT formula needs a valid reference as a string (rather than an actual reference) to calculate - so "'" & SheetNames!A1 & "'!A:A" returns the string 'Sheet 4'!A:A.

Related

Cell() not recognizing Sheet! in cell reference

I'm trying to create a formula that will take the contents from a cell on a different sheet. I don't want to just give the direct cell reference, because I want to be able to cut & paste this into different places and refer to a reference number to know which cell to get.
So, I'm using the Cell() function. But, when I enter something like =CELL("contents", Sheet1!A2), it returns the value of A2 on the current sheet, not on Sheet 1. Every other function I've tried recognizes Sheet 1 when it is given. (This example is just a trial. In reality I have to use Concat() & Indirect() to create the cell reference).
If it matters, I'm using Excel365

Excel - creating Formula in VBA using structured name causes SOME column names to get "#" (same column) tag? [duplicate]

This question already has an answer here:
Excel VBA - How to add dynamic array formula
(1 answer)
Closed 1 year ago.
I'm stumped and hoping for an answer to this weird issue.
In my VBA code, I'm creating a formula that access a column in my table called "Agent". (tbl_CSV_Import_Values[Agent]). The Agent column is in column 8 on my sheet. The table gets created/removed sometimes, and can have a different number of rows. After deleting the table, I recreate it, then run the VBA code to rewrite the formulas. For other formulas this works fine. But...
When I use the below code to stuff the formula into cell F3, it adds "#" to TWO of the "Agent" column References. For no reason I can discern. But it weirdly leaves one of the 3 Agent references alone. (See below)
My VBA:
ActiveSheet.Range("F3").Formula = "= ""Number of Agents: "" & SUM(IF(tbl_CSV_Import_Values[Agent]<>"""",1/COUNTIF(tbl_CSV_Import_Values[Agent], tbl_CSV_Import_Values[Agent]), 0))"
The Formula that get's place in cell F3 (generating an error due to those "#"s in front of Agent:
= "Number of Agents: " & SUM(IF(tbl_CSV_Import_Values[#Agent]<>"",1/COUNTIF(tbl_CSV_Import_Values[Agent], tbl_CSV_Import_Values[#Agent]), 0))
Of course, the formula fails... unless I simply delete those "#" signs from the formula manually. (The excel error says "A value used in the formula is a wrong data type".) With the two #'s deleted, the formula immediately works fine.
Does anyone have any idea why Excel is deciding to whack my column references (and only 2 of the 3?!) like that, and what I can do to stop it?
-Daniel
If the first parameter in IF (IF(tbl_CSV_Import_Values[Agent]<>"""",...) and the second parameter in COUNTIF (COUNTIF(..., tbl_CSV_Import_Values[Agent])) shall be a reference to a range of cells rather than a single cell, then the formula would must be an array formula. So in former Excel versions the formula would must be applied as an array formula by pressing Ctrl+Shift+Enter.
Since Excel 365 the decision whether array formula or not is tried to choose automatically. But when set via VBA using Range.Formula, it always sets a normal formula and not an array formula. And in a normal formula first parameter in IF and second parameter in COUNTIF cannot be column references but must be references to single cells. That's why the adding of the #.
To avoid this do explicitly set an array formula using VBA:
ActiveSheet.Range("F3").FormulaArray = "= ""Number of Agents: "" & SUM(IF(tbl_CSV_Import_Values[Agent]<>"""",1/COUNTIF(tbl_CSV_Import_Values[Agent], tbl_CSV_Import_Values[Agent]), 0))"

Reference Sheet name in Index Match by referring to value in cell

I want to use dynamic formulas...
I know that you can refer to a Sheet name in Excel using INDIRECT() with a value in the current sheet, e.g.
=INDIRECT("'" & INDIRECT("B" & ROW()) & "'! T32")
However, I want to refer to a sheet name using INDIRECT() within an INDEX MATCH Formula. The previous formula is not dynamic and can't be copied across all cells in a block.
The Manual version that works is:
=INDEX('Sheet1'!$T$33:$AE$33,MATCH(INDIRECT(ADDRESS(3,COLUMN())),'Sheet1'!$T$32:$AE$32,0))
I have tried various ways to include an INDIRECT() as above instead of using the actual sheet name but have had no luck so far... any suggestions? The problem may be coming from the fact that I am referring to a range of cells, but I a still not sure how to get around this...
An example of what I have tried:
=INDEX(INDIRECT("'" & INDIRECT("B", Row()) & "'!$T$33:$AE$33"),MATCH(INDIRECT(ADDRESS(3,COLUMN())),'Sheet1'!$T$32:$AE$32,0))
The error I get is a #REF! error...
Thanks in advance!

Excel cell referencing a variable sheet

So I have a tab for every month's worth of data, and then I have a summary that references the current month's sheet. Is there any way I can put "April 2015" (this would be the name of the sheet) in cell A1, and then have formulas like:
=COUNTIFS('(String in A1)'!$I$10:$I$149,B4,'(String in A1)'!$L10:$L149,"")
That way I'd only have to edit the 1 cell and I'd know all the references are correct whereas if one of the formulas don't get replaced, I'd be referring to a month behind on one random cell and likely never even know.
Seems like that should be very simple, but I can't seem to figure out how.
Be careful to preserve those single quotes:
=COUNTIFS(INDIRECT("'" & A1 & "'!$I$10:$I$149"),B4,INDIRECT("'" & A1 & "'!$L10:$L149"),"")

Excel: Specify sheetname in another cell for countif formula

My intention is to use the indirect formula. Here is the code I want to edit:
=COUNTIF('sheetname'!R:R;"x")
This code gives a #ref! error:
=INDIRECT("COUNTIF('"&D85&"'!R:R;"&B98&")")
Cell D85 contains sheetname and B98 contains "x".
My version of Excel needs ; in formulas instead of ,.
Thanks in advance for your expertise and time.
Try this formula:
=COUNTIF(INDIRECT("'"&D85&"'!R:R");B98)
My read on Indirect says that it simply uses the cell reference contained in the cell you specify in the function:
Indirect( cellContainingReference )
In this case, you don't need to specify the second parameter of Indirect.
So, using the assumptions:
sheetName is in cell D85
cellRange is always R:R
criteria for counting is in cell B98 (which does not need Indirect to work)
Your excel uses ';' rather than ','
Your formula for CountIf would be:
=countif(indirect("'"&D85&"'!R:R"); B98)
With some modification it works, I hope this is a good compromise for you. The modified formula is
=COUNTIF(INDIRECT(E1); F1)
and in this case that E1 should contain something like
''sheetname'!R:R
(See the double apostrophes at the beginning.)
F1 should contain the "X" or other value you want to count.

Resources