COUNTA reports a value of 1 when there's nothing to count

BlackieHamel

Board Regular
Joined
May 9, 2014
Messages
93
I have a macro that reports how many rows have data in Column A of a subsidiary worksheet. The code is

Code:
ActiveCell.FormulaR1C1 = "=counta(indirect(RC[-1] & ""!A:A""))"

Thus if the cell to the left says "BUY" I get the number of rows with data in worksheet BUY.

My problem is that if BUY does not exist, it reports 1, not 0. Some worksheets do have just one row in them, so this leads to confusion: is there just one, or does the sheet not exist?

How can I adjust this to report that the sheet does not exist?

Thanks.

Blackie
 
Check for spaces in column A in BUY Sheet. Even a small space may reports as non blank cell.
 
Upvote 0
COUNTA counts the total number of cells that are not truly blank in the range that is its argument. If you have a formula in that range that is returning "" COUNTA will count that as a non-empty cell.
 
Upvote 0
Joe,

Does this mean there's no solution to my problem? Is there no way to distinguish between a worksheet that has a value in one row and a non-existent worksheet?

Blackie
 
Upvote 0
Joe,

Does this mean there's no solution to my problem? Is there no way to distinguish between a worksheet that has a value in one row and a non-existent worksheet?

Blackie

If you want to know if the sheet named BUY exists in the workbook your code is in you can use;
Code:
If SheetExists("BUY") then
'do something
Else
'do something else
End If
'Rest of code
End Sub
Function SheetExists(shName As String) As Boolean
SheetExists = False
For Each sh In ThisWorkbook.Sheets
    If sh.Name = shName Then
        SheetExists = True
        Exit For
    End If
 Next sh
End Function
 
Upvote 0

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