counting

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
519
Office Version
2010
Platform
Windows
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 123
2 1
3 25
1 3
2
36
1
2
3
1
2
1
2
1
3

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

Some videos you may like

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

theBardd

Rules violation
Joined
Jan 21, 2012
Messages
912
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
 

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
12,095
Office Version
2007
Platform
Windows
How about:

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)
    Range("[COLOR=#ff0000]B6[/COLOR]").Value = Evaluate("=SUMPRODUCT((" & r.Address & "=1)*(" & r.Offset(1).Address & "=2))")
End Sub
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,027
Office Version
2010
Platform
Windows
How about:

Code:
    Range("[COLOR=#ff0000]B6[/COLOR]").Value = Evaluate("=SUMPRODUCT((" & r.Address & "=1)*(" & r.Offset(1).Address & "=2))")
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:
Range("B6").Value = Evaluate("SUM((" & r.Address & "=1)*(" & r.Offset(1).Address & "=2))")
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
519
Office Version
2010
Platform
Windows
Mr. Rick Rothstein My personal admiration for you Sir, Thank you, So much for your input.
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
519
Office Version
2010
Platform
Windows
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....
 

montecarlo2012

Well-known Member
Joined
Jan 26, 2011
Messages
519
Office Version
2010
Platform
Windows
DanteAmor you are always very helpfull thank you So much.
 

Watch MrExcel Video

Forum statistics

Threads
1,101,817
Messages
5,483,080
Members
407,377
Latest member
JennaWashburn

This Week's Hot Topics

  • Finding issue in If elseif else with For each Loop
    Finding issue in If elseif else with For each Loop I have tried this below code but i'm getting in Y column filled with W005. Colud you please...
  • MsgBox Error
    Hi Guys, I have the below error show up when i try and run my macro in File1 but works fine if i copy and paste the same code into file2. [ATTACH...
  • CELL FORMAT - IF CONDITION
    My Cell Format is [B]""0.00" Cr". [/B]But in the cell, it is showing 123.00 for editing. (123 is entry figure). (Data imported from other...
  • Show numbers nearly the same
    Is this possible. I have a number that can change very time eg 0.00001234 Then I have a lot of numbers 0.0000001, 0.0000002, 0.00000004...
  • Please i need your help to create formula
    I need a formula in cell B8 to do this >>if b1=1 then multiply ( cell b8) by 10% ,if b1=2 multiply by 20%,if=3 multiply by 30%. Thank you in...
  • Got error while adding column and filter
    Got error while adding column and filter In column Z has some like "Success" and "Error". I want to add column in AA if the Z cell value is...
Top