Loop between two ranges to hide data rows

automation_t4.xlsmI am stuck on a macro. I am trying to use a data range that might change in number of cells used on sheet 1 as a driver to match to a set of values in a column (which might also move locations) as it comes out of a reporter that is used differently by each end user. If the values of the range on sheet 1 match and sheet 2 i want those rows hidden so that only the rows that match show, if the value shows up twice on sheet 2 (isn't unique) choose the first one.   I have used a search to find the row that i need to identify the column regardless of the location move for sheet 2, but i can't get the hide loop down. This is what i have. Any help you provide would be appreciated.

'Sheet 1 data range but again this length could change
Set Fin = WST.Range("B7:B15")


ThisWorkbook.Sheets("Data").Activate

 ' first line of data*.
actC.Select 'starts from J3 on data sheet actC is the result of the search parameter
      ' Set Do loop to stop when an empty cell is reached.
Do Until IsEmpty(ActiveCell)
    If ActiveCell.Value = Cells(Fin, 2).Value Then 'causes mismatch error
        Fin = Fin + 1
    Else
    ActiveCell.EntireRow.Hidden = True
         ' Step down 1 row from present location.
    ActiveCell.Offset(1, 0).Select
    End If
    
Loop

Open in new window

Jenny TaylorAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Please attach your workbook.
0
Jenny TaylorAuthor Commented:
Please find attached workbook
0
Martin LissOlder than dirtCommented:
I'm making a number of changes. Be back later.
0
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Martin LissOlder than dirtCommented:
I placed a 'new comment before each change that I made. I'll be happy to answer any questions you have. Note that my code uses a Collection. Dictionaries are a little better but I use Collections because I'm more familiar with them.
29096222.xlsm
0
Jenny TaylorAuthor Commented:
Thank you for your fast response. I am getting a Run-time error of 457 "they key is already associated with an element of this collection"  for the your version of the randomized section see notes below (line 11). Also why are collections better?

Randomize
Do Until Accounts.Count = HowMany
    RandomNumber = Application.RandBetween(4, lngLastRow)
    On Error Resume Next
    ' This adds a value and a key to the collection. The value is the random number
    ' and the account numbder separated by the pipe symbol (|)
    ' If the key isn't unique then and error occurs (which we ignore)
    ' The key must be a string
    ' I change 10 to "J" because I hate having to count on my fingers to determine what column it is
'    Accounts.Add WSD.Cells(RandomNumber, "J").Value, CStr(WSD.Cells(RandomNumber, "J").Value)
Accounts.Add RandomNumber & "|" & WSD.Cells(RandomNumber, "J").Value, CStr(WSD.Cells(RandomNumber, "J").Value)
    On Error GoTo 0
Loop

Open in new window

0
Martin LissOlder than dirtCommented:
I am getting a Run-time error of 457
That should not be possible unless you commented out line 4. Collections are better than arrays because 1) you need to initially size them or later on worry about the number of records they contain and 2) with the code above you don't get duplicates.
0
Jenny TaylorAuthor Commented:
oh ok. thanks for the information.

I have not commented out line 4. the macro will run, and then if i change the amount to randomize by one sheet 2 lets say 3 times, on the third time it gives me that error and it doesn't hide anything on the data sheet.
0
Martin LissOlder than dirtCommented:
Okay, I did two things (marked by 'newer)
  • Unhid the data rows at the start of each run
  • Cleared the 'Randomized List' at the start of each run
29096222a.xlsm
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Jenny TaylorAuthor Commented:
that worked! thank you so much!
0
Jenny TaylorAuthor Commented:
Changing the arrays to a collection and using for loops to hide to appropriate rows
0
Martin LissOlder than dirtCommented:
You’re welcome and I’m glad I was able to help.

If you expand the “Full Biography” section of my profile you’ll find links to some articles I’ve written that may interest you.

Marty - Microsoft MVP 2009 to 2017
              Experts Exchange Most Valuable Expert (MVE) 2015, 2017
              Experts Exchange Top Expert Visual Basic Classic 2012 to 2017
              Experts Exchange Top Expert VBA (current)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.