• Status: Solved
  • Priority: Low
  • Security: Public
  • Views: 36
  • Last Modified:

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

0
Jenny Taylor
Asked:
Jenny Taylor
  • 6
  • 5
2 Solutions
 
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now