What does the 'Count' function do in vba? For example; Dim Count As Integer OR Count + 1
What does the 'Count' function do in vba? For example; Dim Count As Integer OR Count + 1
The code you've posted doesn't use Count as a function, but as a variable.Originally Posted by JLSHARP92
In VBA, the same "thing" can have different uses.
From VBA Help
as inCount Property
Returns the number of objects in a collection.
[VBA]Sub Test1()
Dim rng As Range
Set rng = Range("A1:A1003")
MsgBox rng.Cells.Count
End Sub
[/VBA]
However, Properties can also be used as variable names as in
[VBA]Sub Test2()
Dim Count As Integer
Dim i As Integer
For i = 1 To 100
Count = Count + i
Next
MsgBox Count
End Sub[/VBA]
It is good practice not to use a Property as a Variable name to avoid confusion. Your code may work, but will be harder to comprehend.
[VBA]Sub Test3()
Dim rng As Range
Dim Count As Integer
Dim i As Integer
For i = 1 To 100
Count = Count + i
Next
Set rng = Range("A1:A" & Count)
MsgBox Count & vbCr & rng.Cells.Count
End Sub
[/VBA]
MVP (Excel 2008-2010)
Post a workbook with sample data and layout if you want a quicker solution.
To help indent your macros try Smart Indent
Please remember to mark threads 'Solved'
Thank you very much for this.Originally Posted by mdmackillop