vba code to count non-blank cells in a range

tucsondonpepe

New Member
Joined
Jan 30, 2022
Messages
43
Office Version
  1. 365
Platform
  1. Windows
  2. Web
I am trying to count the number of non-blank cells in 16 ranges on sheet "Signup" in column E. The ranges are stored in
LNCaptain(16). I store the results in TeamsInLeague(16).

The code is

1643601579145.png

I receive this error

1643601689466.png

I would appreciate youor help. Thank you.

1643601336955.png
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Hi and welcome to MrExcel.

The ranges are stored in LNCaptain(16)
How did you store the ranges in the LNCaptain variable?

Did you create a named range for each cell range of the LNCaptain?
If so, what is each named range called?
 
Upvote 0
Good morning and thank you for replying,

This is the subroutine.

==================================================================
1643638171750.png

==================================================================

The LNCaptain data is dfined in sheet Leagues.

===============================================================
1643638283376.png

===============================================================

This is output showing the value of LNCaptain(1).

=============================================================
1643638375923.png

=============================================================

This is the error message.

===============================================================================================================================
1643638462890.png

===============================================================================================================================
 
Upvote 0
This code counts the blank cells in E2:E17 range
VBA Code:
Dim Cnt as long
cnt=worksheetfunction.counta(Range("E2:E17")
That actually counts NON blank cells.
And you are missing a right parend on the end, i.e.
VBA Code:
cnt=worksheetfunction.counta(Range("E2:E17"))

If you wanted to count blanks, you would use COUNTBLANK, not COUNTA.
 
Upvote 0
I am trying to count the number of non-blank cells in 16 ranges on sheet "Signup" in column E.
To paste examples from your sheet, you must use the XL2BB tool minisheets. It is easier for us to use data than an image. I show you an example:
varios 31ene2022.xlsm
ABCDE
1LeaguesPoTPlayersLeague LabelLNCaptain
2416E5:E8
328E13:E16
414E21:E24
528E29:E32
6416E37:E40
714E45:E48
8416E53:E56
928E61:E64
1014E69:E72
1128E77:E80
1214E85:E88
13416E93:E96
14416E101:E104
1528E109:E112
16416E117:E120
17416E125:E128
18168
Leagues

NOTE: You must put the ranges without quotes.

____
Try the following code:
VBA Code:
Sub Count_NON_Blank()
  Dim n As Long
  Dim TeamsInLeague As Variant
  Dim x As Variant
  ReDim TeamsInLeague(1 To 16)
  For n = 1 To 16
    x = Sheets("Leagues").Range("E" & n + 1).Value
    TeamsInLeague(n) = WorksheetFunction.CountA(Sheets("Signup").Range(x))
    Debug.Print TeamsInLeague(n)
  Next
End Sub

You also need to comment on what the final goal of doing the count is, because storing the counts in a variable I don't think is the final goal.
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,791
Members
449,095
Latest member
m_smith_solihull

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