1

I've read the post on this VBA problem, but my VBA script is still not working.

Public Sub Test()

Dim arrNames As Variant                     'Declare array named "arrNames"
arrNames = Sheet1.Range("F2:F1000")         'Array filled with column F
intN = Application.CountIf(arrNames, "*")   'does not work intent: count cells w/info
'intN = Application.CountA(arrNames)        'works but MsgBox displays value of 999
MsgBox (intN)

End Sub

How do I get the number of cells in my array containing any value?

EDITED version after help

Public Sub Test()

Dim arrNames As Variant                       'Declare array named "arrNames"
Dim i As Long

arrNames = Sheet1.Range("F2:F1000")           'Array filled with column F

For i = LBound(arrNames) To UBound(arrNames)
    If (arrNames(i,1) = "") Then
        EmptyCounter = EmptyCounter + 1
    End If
Next i

End Sub
4
  • You haven't declared Dim EmptyCounter As Long Commented Jul 3, 2014 at 22:00
  • Can you not use the simplest approach like MsgBox Application.WorksheetFunction.CountBlank(Range("F2:F1000"))?? Commented Jul 4, 2014 at 7:25
  • @Greg, did my solution below help? Commented Jul 7, 2014 at 21:40
  • No and Yes. arrNames needs to be changed on Line 9 after the If statement as such: If arrNames(i,1) otherwise an error subscript out of range results. Commented Jul 10, 2014 at 17:08

2 Answers 2

2

There is no direct way to do it, as far as I understand. But you could run a simple loop to check if the values are equal to "" assuming string data.

For e.g.

For i = LBound(ArrayName) to UBound(ArrayName)
    If (ArrayName(i) = "") Then
        EmptyCounter = EmptyCounter + 1
    End If
Next i

If it's numeric or other type of data, you may try variations of the above loop using functions such as IsEmpty(VariableName) etc.

Sign up to request clarification or add additional context in comments.

8 Comments

It usally just a string, e.g mixed data like S12-9
If (arrNames(i) = "") Then 'Script hangs on this line
what do you mean 'hangs'? What's the error that pops up?
Run Time Error '9':Subscript out of Range
That is very very odd, if it's going from LBound to Ubound, it's almost impossible for that. Is i declared as Long? or Integer? Please declare it as Long and see again.
|
1

You can try this:

intN = Worksheets("Sheet1").Range("F2:F1000").Cells.SpecialCells(xlCellTypeConstants).Count
MsgBox intN

100% it works.

6 Comments

This does work, however I need "count" for an array as my arrays are manipulated in the script.
I'm nit sure I get you right on this one. But if I do you can try somthing like this: intN = arrNames.Cells.SpecialCells(xlCellTypeConstants).Count - I'm assuming that you had declared arrNames as Range before. Does that help?
What I meant was your answer really evaluates the values that will be placed into the array, not the array iteself. But there are times I will need to count the array after I have "sorted", removed duplicates etc from the array. In that case the intN = Worksheets... would not work.
ok, still not giving up on this one - you can re-define your range in terms of rows like this: Dim TheLastRow As Long TheLastRow = Worksheets("Sheet1").Range("F2:F1000").Cells.SpecialCells(xlCellTypeLastCell).Row MsgBox (TheLastRow) - that one finds the last non-empty row. then you can fire up the code I proposed in my first answer.
This makes sense and intN will give you an interger value. However how do I enter that last row value in setting the array range? The following does not work. arrNames = Sheet1.Range("F2":intN)
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.