Searching a document for multiple criteria using VBA Macro

Peteor

Board Regular
Joined
Mar 16, 2018
Messages
152
Ok I have been stuck on this for a while, and hope someone can help. I have an Excel Workbook with useful data in Column A and Column K. I would like to accomplish the following using a VBA macro on Worksheet 30:

Dim D04C_MMF_Count As Integer

D04C_MMF_Count = 0

If the contents of a cell in column A = D04C AND the contents of a cell in column K = MMF, Then

D04C_MMF_Count = D04C_MMF_Count +1

Else
End If


I have Googled every resource I can find, and reviewed several threads, and I can't find a solution. I plan to scale this up to many more examples, but can't seem to get the Logic/Syntax right. I would like to search all cells in each column. It was just pointed out to me Concatenating strings MAY help, but I would like to avoid that if at all possible. Any input would be helpful. Thank You.
 
Last edited:

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
How about
Code:
Sub Peteor()
   Dim D04C_MMF_Count As Long
   Dim Cl As Range

   For Each Cl In Range("A2", Range("A" & Rows.Count).End(xlUp))
      If Cl.Value = "D04C" And Cl.Offset(, 10).Value = "MMF" Then
         D04C_MMF_Count = D04C_MMF_Count + 1
      End If
   Next Cl
   MsgBox D04C_MMF_Count
End Sub
 
Upvote 0
with the assumption you are looking for a row match/combination on the same row as a count

Code:
Option Explicit
Sub CrapCount()
    Dim D04C_MMF_Count As Long
        D04C_MMF_Count = 0
    Dim ER As Long
        ER = ThisWorkbook.ActiveSheet.Range("A" & Rows.Count).End(xlUp).Row
    Dim Cell As Range
    Dim CriteriaRange As Range
        Set CriteriaRange = ThisWorkbook.ActiveSheet.Range("A1:A" & ER)
            For Each Cell In CriteriaRange
                If Cell.Value = "D04C" And Cell.Offset(, 10).Value = "MMF" Then
                    D04C_MMF_Count = D04C_MMF_Count + 1
                End If
            Next Cell
        MsgBox (D04C_MMF_Count)
End Sub
 
Upvote 0
Holy Smokes!!! You both are awesome!!!!! I ended up using bsquad's code, and it worked absolutely perfectly! It actually showed some inconsistencies in my data-set! You both rock!
 
Upvote 0
You're welcome & thanks for the feedback
 
Upvote 0
Why not just:

Code:
D04C_MMF_Count = WorksheetFunction.CountIfs(Sheets("Worksheet 30").Range("A:A"), "D04C", Sheets("Worksheet 30").Range("K:K"), "MMF")
 
Upvote 0

Forum statistics

Threads
1,224,923
Messages
6,181,785
Members
453,065
Latest member
jfrsanders

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