![]() |
![]() |
|
|||||||
| Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only. |
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
New Member
Join Date: Mar 2002
Posts: 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!! |
|
|
|
|
|
#2 |
|
MrExcel MVP
Join Date: Feb 2002
Location: Columbus, OH, USA
Posts: 3,519
|
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. |
|
|
|
|
|
#3 |
|
Board Regular
Join Date: Feb 2002
Location: Ahmedabad Gujarat
Posts: 303
|
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 nishith http://www.pexcel.com |
|
|
|
|
|
#4 |
|
MrExcel MVP
Join Date: Mar 2002
Location: Kobe, Japan
Posts: 1,420
|
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 |
|
|
|
|
|
#5 |
|
New Member
Join Date: Apr 2011
Posts: 30
|
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 Thanks |
|
|
|
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|