COUNTA() in VBA

brucew

New Member
Joined
Mar 23, 2002
Messages
4
Hi,

I would like to use counta to count the number of cells containing data in a specific row by using VBA. I know to use lets say:

cells(1,1).formula = "=counta(B1:H1)" to return the value, however, I would like to use variables to substitute for A1 and H1. I would like for the value to dynamically change as I change add or delete cells in the range. I have tried many different ways, but none of them work... I came close with:

dim I,J,K
I=1
J=2
K=8
cells(1,1)= "=COUNTA(range(cells(1,2),cells(1,8)))"

But it doesn't work.

Then I tried like this:

dim I,J,K
I=1
J=2
K=8

application.counta(range(cells(I,J),cells(I,K)))

It returned value but it doesn't dynamically change as I input or delete data from cells. Thanks in advance for the help!!
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
What event are you using to trigger this code? Are you using the cells Change event?

It looks to me like you are just running this code once, so if it's not being triggered by the Change event I recommend using it.

If you need any further help, just repost.
 
Upvote 0
Try this

sub countrange()

' if you have data in b1

dim i as integer

i = [b1].currentregion.rows


[a1] = i

end sub

i hope this will help you..

this is dyanmic.. go on adding data and you can get the counts of data.. if you have header then make minus -1

ni****h
http://www.pexcel.com
 
Upvote 0
If you want to use formula

Sub test()
Dim I As Long, J As Integer, K As Integer
I = 1: J = 2: K = 8
Cells(1, 1).Formula = "=countA(" & Range(Cells(I, J), Cells(I, K)).Address & ")"
End Sub


If you want to use Worksheet Change event...

Private Sub Worksheet_Change(ByVal Target As Excel.Range)
Dim I As Long, J As Integer, K As Integer
I = 1: J = 2: K = 8
If Target.Row <> I Then Exit Sub
MsgBox Application.WorksheetFunction.CountA(Range(Cells(I, J), Cells(I, K)))
End Sub
 
Upvote 0
hie,

I almost have a similar question, I wanted to find out how do i do a:
counta macro for multiple statements:
e.g.
Code:
=COUNTA(TABF10!$F:$F)-2
=COUNTA(TABF124!$G:$K)-2

plus 10 there are tables like this with different lookup ranges and from different worksheets.

Thanks
 
Upvote 0

Forum statistics

Threads
1,212,927
Messages
6,110,695
Members
448,293
Latest member
jin kazuya

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