VBA Finding matches occurring more than 2 times, then copy to new worksheet based upon multiple criteria

jusmith

New Member
Joined
Mar 8, 2022
Messages
2
Office Version
  1. 2016
Platform
  1. Windows
I am new and trying to teach myself Excel VBA. I volunteered for a task and now think that vba can automate the task versus my doing it manually. The task is to find an account number occurring more than 2 times in Col A. If found, then if Col C AND Col D = "no" AND Col E = "yes" copy Col A, Col B, AND Col E to new worksheet. The worksheet contains 45000 rows of data. I am comfortable with IF statements but looping through rows and columns based specific criteria is beyond my understanding. Any assistance would be sincerely appreciated. I've attached an image of my table data
 

Attachments

  • MyExcelTask.png
    MyExcelTask.png
    9.6 KB · Views: 8

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hi there,
welcome to the forum. If I understand you correctly, the only rows that matter are the ones with no,no,yes in column C,D&E? If so, start with a filter on those columns as that shortens your list considerably. Secondly, is this a task that you'd need to do often? If not, it might be much quicker to simply go with formulas. This code hopefully gets you started:
VBA Code:
Sub FilterLoop()
    
    'Based on: https://newbedev.com/easiest-way-to-loop-through-a-filtered-list-with-vba
    
    'Remove any filters
    ActiveSheet.AutoFilterMode = False

    'Set your range including headers
    Set rRange = ActiveSheet.Range("A1:D10")
    'Set your criterias and filters one by one
    rRange.AutoFilter Field:=2, Criteria1:="no"
    rRange.AutoFilter Field:=3, Criteria1:="no"

    'Filter, offset(to exclude headers)
    Set filRange = rRange.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow

    For Each Rng In filRange
        'Looping over the visible rows
        Debug.Print Rng.Address
        
    Next

    'Remove any filters
    ActiveSheet.AutoFilterMode = False
End Sub
Cheers,
Koen
 
Upvote 0
Thank you so much, Koen. Yes, this routine will be used often so I thought going the VBA route would be more efficient. I sincerely appreciate your help. This is a great start!
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,851
Members
449,051
Latest member
excelquestion515

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