How to count negative values in a table with VBA

Romano_odK

Active Member
Joined
Jun 4, 2020
Messages
379
Office Version
  1. 365
Platform
  1. Windows
Good morning,

Got table and I would like to count the how many lines there are where the value in column J is negative. Table starts at line 6 and is called "Locatie". Can this be done?

Thank you for your time.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
What is the table 'Locatie' heading in column J?
 
Upvote 0
How about
VBA Code:
Option Explicit
Sub countNeg()
Dim myCol As Range
Set myCol = Application.Intersect(Range("locatie"), Columns("J")) ' column J in that table
MsgBox WorksheetFunction.CountIf(myCol, "<0")
End Sub
Untitled.png
 
Upvote 0
How about
VBA Code:
Option Explicit
Sub countNeg()
Dim myCol As Range
Set myCol = Application.Intersect(Range("locatie"), Columns("J")) ' column J in that table
MsgBox WorksheetFunction.CountIf(myCol, "<0")
End Sub
View attachment 100447
Could you instead of reporting it in a message box place the result in cell J1 for example?
 
Upvote 0
If it is a formal table and you want the result in J1, why not just use a simple formula in that cell?

Romano_odK.xlsm
J
13
2
3
4
5
6Voorraad
7
81
95
10-3
11
120
13-2.3
14-3
154
16
17
Sheet1
Cell Formulas
RangeFormula
J1J1=COUNTIF(Locatie[Voorraad],"<0")
 
Upvote 0
If it really needs to be vba then this one-liner should do it.
VBA Code:
Sub CountLessThanZero()
  Range("J1").Value = Application.CountIf(Range("Locatie[Voorraad]"), "<0")
End Sub
 
Upvote 0
Solution
If it is a formal table and you want the result in J1, why not just use a simple formula in that cell?

Romano_odK.xlsm
J
13
2
3
4
5
6Voorraad
7
81
95
10-3
11
120
13-2.3
14-3
154
16
17
Sheet1
Cell Formulas
RangeFormula
J1J1=COUNTIF(Locatie[Voorraad],"<0")
That is a good remark, I will eventually use this to report in a command button. So this is step 1. Thank you for your reply and have a great day.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,241
Messages
6,123,823
Members
449,127
Latest member
Cyko

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