counting a number of values in a column.

l33tn4

New Member
Joined
Apr 17, 2013
Messages
5
ok so i need some help here i need a function to find the number of a specific value (x) in a column
been trying this for a while but any way that i think of doing it i get errors. i would thing that this would be easy so changes are it is right in front of my face and i just don't know how to do it. this is what i have so far:

Sub notsureagooodname()

Dim lastrow As Long

'find last row

With Sheets("DATA")
If Application.WorksheetFunction.CountA(.Cells) <> 0 Then
lastrow = .Cells.Find(What:="*", _
After:=.Range("A1"), _
Lookat:=xlPart, _
LookIn:=xlFormulas, _
SearchOrder:=xlByRows, _
SearchDirection:=xlPrevious, _
MatchCase:=False).Row
Else
lastrow = 1
End If
End With

MsgBox lastrow
'running through data
Dim x As Integer
Dim count As Long

x = 2176

Dim rngTarget As Range
Dim rngSearched As Range

For Each rngTarget In Sheets("DATA").Range("D1:D" & lastrow)

If Sheets("DATA").Range("D" & rngTarget) = x Then

count = count + 1

End If

Next rngTarget

MsgBox count

End Sub


i am guessing that i am not using the range function right, but idk. if someone could not only show me how to get this working but tell me what i am doing wrong that would be great.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Welcome to the board!

Code:
If Sheets("DATA").Range("D" & rngTarget) = x Then------>If rngTarget.Value=x

Hope it helps.
 
Upvote 0
l33tn4,

How about:


Excel 2007
DEF
121765
22
32176
44
52176
6
72176
8
92176
10
112176
12
Data
Cell Formulas
RangeFormula
F1=COUNTIF(D:D,E1)
 
Upvote 0
worked like a charm knew that this would be simple but sadly i need the function to check another row for a value if the data = x, thus the loop and if then statement. The plan was to add to the code.

any idea how i can do that?
 
Upvote 0
l33tn4,

Sample raw data:


Excel 2007
ABCD
1
222
332176
444
552176
666
772176
888
992176
101010
11112176
1212
1313
14
Data


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub l33tn4()
Dim x As Long, lastrow As Long, n As Long
With Sheets("DATA")
  lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
  x = 2176
  n = Application.CountIf(.Range("D2:D" & lastrow), x)
  MsgBox "x = " & x & " , was found in range D2:D" & lastrow & ", " & n & " times."
End With
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the l33tn4 macro.

When you run the macro, you will get a message box that will display:

x = 2176 , was found in range D2:D13, 5 times.
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,167
Members
448,554
Latest member
Gleisner2

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