Count the number of times the If statement is true

owen4512

Board Regular
Joined
Dec 10, 2014
Messages
71
Hi all,

I have a cell in sheet1 where i want to store the number of times my if statement is true. In 'Records' i have a value of 0 and i want it so that every time 'Search' matches a record in sheet2 it adds value 1 to the cell 'N13'.

When testing this, i have made sure that multiple items in column 'B' match but the value in 'N13' remains as 1 and wont go any higher.

Any help would be much appreciated :)


VBA Code:
Sub Search()

    Dim i As Integer
    Dim last_row As Integer
    Dim Search As Long
    Dim Records As Long
    
    Search = Sheet1.Range("N11").Value
    Records = Sheet1.Range("N13").Value
    last_row = Application.WorksheetFunction.CountA(Sheet2.Range("A:A"))

    
    For i = 2 To last_row
        If Sheet2.Range("B" & i).Value = Search Then
            
            Sheet1.Range("N13").Value = Records + 1
            Sheet1.Range("D11").Value = Sheet2.Range("C" & i).Value
            
        Else
            
        End If
         
   
    Next i


End Sub
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

AlphaFrog

MrExcel MVP
Joined
Sep 2, 2009
Messages
16,437
Rich (BB code):
Sub Search()

    Dim i As Integer
    Dim last_row As Integer
    Dim Search As Long
    Dim Records As Long
   
    Search = Sheet1.Range("N11").Value
    'Records = Sheet1.Range("N13").Value
    last_row = Application.WorksheetFunction.CountA(Sheet2.Range("A:A"))

   
    For i = 2 To last_row
        If Sheet2.Range("B" & i).Value = Search Then
           
            Records = Records + 1
            Sheet1.Range("D11").Value = Sheet2.Range("C" & i).Value
           
        Else
           
        End If
        
   
    Next i
    Sheet1.Range("N13").Value = Records

End Sub

Or you could use a COUNTIF formula

Excel Formula:
=COUNTIF(Sheet1!B:B, Sheet1!N11)
 
Solution

owen4512

Board Regular
Joined
Dec 10, 2014
Messages
71
Rich (BB code):
Sub Search()

    Dim i As Integer
    Dim last_row As Integer
    Dim Search As Long
    Dim Records As Long
  
    Search = Sheet1.Range("N11").Value
    'Records = Sheet1.Range("N13").Value
    last_row = Application.WorksheetFunction.CountA(Sheet2.Range("A:A"))

  
    For i = 2 To last_row
        If Sheet2.Range("B" & i).Value = Search Then
          
            Records = Records + 1
            Sheet1.Range("D11").Value = Sheet2.Range("C" & i).Value
          
        Else
          
        End If
       
  
    Next i
    Sheet1.Range("N13").Value = Records

End Sub

Or you could use a COUNTIF formula

Excel Formula:
=COUNTIF(Sheet1!B:B, Sheet1!N11)

That's worked great - Thank you :)
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,571
Messages
5,770,920
Members
425,652
Latest member
Pemby

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
Top