What I am trying to do is iterate through a range containing worksheet names, and if the cell is not empty then add the result of CountA
function to the count variable.
So the count variable should be equal to number of non-blank cells in range B9:B28
on the worksheets I'm iterating through, but strangely the value is equal to the number of non empty cells in the range I'm going through (sheet1!d5:d24
).
What am I doing wrong? Here's the code I am using:
For Each c In Worksheets("Sheet1").Range("d5:d24").Cells
If Not IsEmpty(c) Then
count = count + WorksheetFunction.CountA(c & "!b9:b28")
End If
Next
I tried an alternative method to loop through second range and if the cells are not empty, then increment the variable by 1 but that's giving a Run time error 13 type mismatch error. This is what I am doing now:
For Each c In Worksheets("Sheet1").Range("d5:d24")
If Not IsEmpty(c) Then
For Each c2 In Worksheets(c).Range("b9:b28")
If Not IsEmpty(c2) Then
'count = count + WorksheetFunction.CountA(c & "!b9:b28")
count = count + 1
End If
Next
End If
Next
Please help me out. Thanks a lot in advance to all those who take out time to reply.
.CountA
with a range object and not a string. Try.CountA(Worksheets(c).Range("B9:B28"))
– Peter Albertc.Value
and it worked. Thanks a lot guys. – Amit Kumar Jha