# counting

#### montecarlo2012

##### Well-known Member
Hello.
I have a little snipe code able to tell me how many times number 1 is in the column A, I am trying to count how many time 2 is after one, but still I don't know how to say that in VBA. This is the little code:
Code:
Sub countmontecarlo()Dim cell As Range
Dim n As Long
For Each cell In Range("A:A")
If cell = 1 Then

n = n + 1
End If
Next cell
Range("b6").Value = n
End Sub
the code count 6 one's but how to count 5 times 2 under the coordinate 1-2, thank you.
 1 1 2 3 2 1 3 2 5 1 3 2 3 6 1 2 3 1 2 1 2 1 3

<colgroup><col width="64" span="6" style="width:48pt"> </colgroup><tbody>
</tbody>

### Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

#### theBardd

##### Rules violation
Code:
Sub countmontecarlo()
Dim cell As Range
Dim lastrow As Long
Dim n As Long
Dim i As Long
With ActiveSheet

lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row

For i = 1 To lastrow

If .Cells(i, "A").Value = 1 And .Cells(i + 1, "A").Value = 2 Then

n = n + 1
End If
Next i
.Range("B6").Value = n
End With
End Sub

#### montecarlo2012

##### Well-known Member
Thank you So much theBardd. worked perfect.

#### DanteAmor

##### Well-known Member

Code:
Sub counting1_2()
Dim r As Range
Set r = Range("A1", Range("A" & Rows.Count).End(xlUp))
Range("[COLOR=#ff0000]B5[/COLOR]").Value = WorksheetFunction.CountIf(r, 1)
End Sub

#### Rick Rothstein

##### MrExcel MVP

Code:
Since the Evaluate function processes non-text functions as arrays (when an array of values are involved) natively, you can use the SUM function instead of the SUMPRODUCT function for the expression you are giving it. Also, you do not need to include the equal sign for the Evaluate function.
Code:

#### montecarlo2012

##### Well-known Member
Thank you DanteAmor.

#### montecarlo2012

##### Well-known Member
Mr. Rick Rothstein My personal admiration for you Sir, Thank you, So much for your input.

#### montecarlo2012

##### Well-known Member
As you can see, at the beginning the post has a little Matrix, what I was trying to request is to count the value of following position for each number. I don't know if make sence,
like how many times 3 is after 2, and so on, but I really apreciate the answers, I am working now trying to find the way, for me is really dificult, but....

#### DanteAmor

##### Well-known Member
Thank you DanteAmor.

#### montecarlo2012

##### Well-known Member
DanteAmor you are always very helpfull thank you So much.

1,102,867
Messages
5,489,369
Members
407,686
Latest member
Chuck1960

### This Week's Hot Topics

• Timer in VBA - Stop, Start, Pause and Reset
[CODE=vba][/CODE] Option Explicit Dim CmdStop As Boolean Dim Paused As Boolean Dim Start Dim TimerValue As Date Dim pausedTime As Date Sub...
• how to updates multiple rows in muliselect listbox
Hello everyone. I need help with below code. code is only chaning 1st row in mulitiselect list box. i know issue with code...
• Delete Row from Table
I am trying to delete a row from a table using VBA using a named range to find what I need to delete. My Range is finding the right cell. In the...
• Assigning to a variable
I have a for each block where I want to assign the value in column 5 of the found row to the variable Serv. [CODE=vba] For Each ws In...
• Way to verify information
Hi All, I don't know what to call this formula, and therefore can't search. I have a spreadsheet with information I want to reference...
• Active Cell Address – Inactive Sheet
How to use VBA to get the cell address of the active cell in an inactive worksheet and then place that cell address in a location on the current...