VBA to hide unwanted rows

Godders199

Active Member
Joined
Mar 2, 2017
Messages
313
Office Version
  1. 2013
Hello, i want to write some code that will hide rows when a set criteria is met . example sheet below.

[TABLE="width: 864"]
<tbody>[TR]
[TD]Agent Code[/TD]
[TD]Agent Name[/TD]
[TD]advtype[/TD]
[TD]Risk1[/TD]
[TD]Risk2[/TD]
[TD]CIN[/TD]
[TD]Date Allocated[/TD]
[TD]Allocated To[/TD]
[TD]Queue Status[/TD]
[/TR]
[TR]
[TD]780206[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]MPM Holding pot[/TD]
[TD]Appointment Booked[/TD]
[/TR]
[TR]
[TD]781356[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Simon [/TD]
[TD]Review Required[/TD]
[/TR]
[TR]
[TD]783972[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Jeff [/TD]
[TD]Review Required[/TD]
[/TR]
[TR]
[TD]764119[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Pete[/TD]
[TD]Review Required[/TD]
[/TR]
</tbody><colgroup><col span="9"></colgroup>[/TABLE]


Agent code is column D, allocated to is column K

Effectively what i want to do is look at column K, if cell value is not MPM Holding Pot, PRM Holding Pot, Outbound unrequired or "", then look at column D, and hide all codes with the corresponding agent code. for example, column K Simon, hide all rows where agent code is 781356 couild be 1 or 100 rows with that number

Hope this makes sense, if anyone can help
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Try this:

Code:
Public Sub Godders199()

Dim lastRow As Long
Dim thisRow As Long
Dim myDic As Object

Set myDic = CreateObject("Scripting.Dictionary")

lastRow = Cells(Rows.Count, "D").End(xlUp).Row
For thisRow = 2 To lastRow
    If myDic.Exists(Cells(thisRow, "D").Value) Then
        Cells(thisRow, "A").EntireRow.Hidden = True
    Else
        Select Case LCase(Cells(thisRow, "K").Value)
            Case "mpm holding pot", "prm holding pot", "outbound unrequired", ""
            Case Else
                Cells(thisRow, "A").EntireRow.Hidden = True
                myDic.Add Cells(thisRow, "D").Value, "Hide"
        End Select
    End If
Next thisRow

End Sub

WBD
 
Upvote 0
Programmatically, you can just set:

Code:
Cells(thisRow, "A").EntireRow.Hidden = False

In the UI, select everything, right-click menu and Unhide.

WBD
 
Upvote 0
Sorry, just testing this , on the following example as one of the rows is not "outbound unrequired" then all rows for 758577 should have been hidden. currently it only appears to only hide rows where there are only rows associated with the agent code, which do not include "mpm holding pot, prm holding pot , outbound unrequired or blank. ( so where there is a mixture of a name or one of these 4, it leaves all the rows instead of deleting all.)

[TABLE="width: 768"]
<tbody>[TR]
[TD]Agent Code[/TD]
[TD]Agent Name[/TD]
[TD]advtype[/TD]
[TD]Risk1[/TD]
[TD]Risk2[/TD]
[TD]CIN[/TD]
[TD]Date Allocated[/TD]
[TD]Allocated To[/TD]
[/TR]
[TR]
[TD]758577[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]simon[/TD]
[/TR]
[TR]
[TD]758577[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]OUTBOUND UNREQUIRED[/TD]
[/TR]
</tbody><colgroup><col span="8"></colgroup>[/TABLE]
Sub Godders199()
Dim lastRow As Long
Dim thisRow As Long
Dim myDic As Object
Set myDic = CreateObject("Scripting.Dictionary")
Sheets("subs").Select
lastRow = Cells(Rows.Count, "D").End(xlUp).Row
For thisRow = 2 To lastRow
If myDic.Exists(Cells(thisRow, "D").Value) Then
Cells(thisRow, "A").EntireRow.Hidden = True
Else
Select Case LCase(Cells(thisRow, "K").Value)
Case "mpm holding pot", "prm holding pot", "outbound unrequired", ""
Case Else
Cells(thisRow, "A").EntireRow.Hidden = True
myDic.Add Cells(thisRow, "D").Value, "Hide"
End Select
End If
Next thisRow
End Sub
 
Upvote 0
Sorry, still testing, think we are nearly there, where allocated to a name all rows are hidden correctly, however it also appears to be hiding rows containing "MPM HOLDING POT", "PRM HOLDING POT" AND OUTBOUND UNREQUIRED", for example , below all of 700012 cases (3) are in PRM holdingpot but the vba Hides them .

here is my code and below an example.

Sub Godders199()
Dim lastRow As Long
Dim thisRow As Long
Dim myDic As Object
Set myDic = CreateObject("Scripting.Dictionary")
Sheets("subs").Select
lastRow = Cells(Rows.Count, "D").End(xlUp).Row
For thisRow = 2 To lastRow
If myDic.Exists(Cells(thisRow, "D").Value) Then
Cells(thisRow, "A").EntireRow.Hidden = True
Else
Select Case LCase(Cells(thisRow, "K").Value)
Case "MPM HOLDINGPOT", "PRM HOLDINGPOT", "OUTBOUND UNREQUIRED", ""
Case Else
Cells(thisRow, "A").EntireRow.Hidden = True
myDic.Add Cells(thisRow, "D").Value, "Hide"
End Select
End If
Next thisRow
End Sub


As per my original post if row K does not contain, one of the 4 options, it needs to look at column D, and hide all rows with that agent code. ( this might include rows containing one of the 4 options)



[TABLE="width: 864"]
<tbody>[TR]
[TD]Agent Code[/TD]
[TD]Agent Name[/TD]
[TD]advtype[/TD]
[TD]Risk1[/TD]
[TD]Risk2[/TD]
[TD]CIN[/TD]
[TD]Date Allocated[/TD]
[TD]Allocated To[/TD]
[TD]Queue Status[/TD]
[/TR]
[TR]
[TD]700012[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]PRM HOLDINGPOT[/TD]
[TD]Review Required[/TD]
[/TR]
[TR]
[TD]700012[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]PRM HOLDINGPOT[/TD]
[TD]Review Required[/TD]
[/TR]
[TR]
[TD]700012[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]PRM HOLDINGPOT[/TD]
[TD]Review Required
[/TD]
[/TR]
</tbody><colgroup><col span="9"></colgroup>[/TABLE]
 
Upvote 0
I worried about this because your original requirements were a bit ambiguous. For example, what should happen here:


Book1
ABCDEFGHI
1Agent CodeAgent NameadvtypeRisk1Risk2CINDate AllocatedAllocated ToQueue Status
2780206SimonReview Required
3780206MPM HOLDINGPOTAppointment Booked
4781356SimonReview Required
5783972JeffReview Required
6781356PRM HOLDINGPOTReview Required
Sheet1


The original requirements suggested that rows 2, 3, 4 and 6 should be hidden.

WBD
 
Upvote 0
sorry , in this example, rows 2 and 3 ( same agent code) should be hidden, as even though one allocated to MPM holdingpot one is allocated to simon, row 4 hidden as allocated to simon, row 5 hidden as allocated to jeff, row 6 visible as alloacate to holdingpot.
 
Upvote 0
But row 6 has the same agent code as row 4 so why shouldn't it be hidden? Also, what about this:


Book1
ABCDEFGHI
1Agent CodeAgent NameadvtypeRisk1Risk2CINDate AllocatedAllocated ToQueue Status
2780206MPM HOLDINGPOTAppointment Booked
3780206SimonReview Required
4780206MPM HOLDINGPOTAppointment Booked
5781356SimonReview Required
6783972JeffReview Required
7781356PRM HOLDINGPOTReview Required
Sheet1


Which ones of rows 2, 3, 4 should be hidden?

WBD
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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