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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
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)
 
Upvote 0
Solution
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 :)
 
Upvote 0

Forum statistics

Threads
1,214,965
Messages
6,122,495
Members
449,088
Latest member
Melvetica

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