sandcastl3s
New Member
- Joined
- Apr 10, 2016
- Messages
- 6
This is a bit of a follow-up to my question here, where I inquired on using an array for in-string values to not delete a row. Thanks again to Peter, this saved me a ton of time.
I'm now trying to work out the opposite, more or less: to delete a row if any value in an array is present. I'm sure it's not much of a tweak to the existing solution but I can't for the life of me work it out.
Example: I have a list of thousands of names:
Will
William
Jim
Bob
Jim
Jimmy
James
The array is ("Will", "Jim")
The result would be
Bob
James
as at least one value in the array was present in all of the other rows.
~~
For reference, this is the scenario description and working script from the above-mentioned thread:
Will
William
Jim
Bob
Jim
Jimmy
James
The below script produces this after removing all rows that do not include any value in the array:
Will
William
Jim
Jim
Jimmy
I've tried hacking this code a thousand ways but can't get it to work. Any help is much appreciated!
I'm now trying to work out the opposite, more or less: to delete a row if any value in an array is present. I'm sure it's not much of a tweak to the existing solution but I can't for the life of me work it out.
Example: I have a list of thousands of names:
Will
William
Jim
Bob
Jim
Jimmy
James
The array is ("Will", "Jim")
The result would be
Bob
James
as at least one value in the array was present in all of the other rows.
~~
For reference, this is the scenario description and working script from the above-mentioned thread:
Will
William
Jim
Bob
Jim
Jimmy
James
The below script produces this after removing all rows that do not include any value in the array:
Will
William
Jim
Jim
Jimmy
Code:
[COLOR=darkblue]Sub[/COLOR][COLOR=#333333] Del_Rows()[/COLOR]
[COLOR=darkblue]Dim[/COLOR] a, b, dontDelete
[COLOR=darkblue]Dim[/COLOR] i [COLOR=darkblue]As[/COLOR] [COLOR=darkblue]Long[/COLOR], j [COLOR=darkblue]As[/COLOR] Long
dontDelete = Array("Will", "Jim")
a = Range("A1").CurrentRegion.Value
[COLOR=darkblue]ReDim[/COLOR] b(1 [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](a), 1 [COLOR=darkblue]To[/COLOR] 1)
[COLOR=darkblue]For[/COLOR] i = 1 [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](a)
[COLOR=darkblue]For[/COLOR] j = [COLOR=darkblue]LBound[/COLOR](dontDelete) [COLOR=darkblue]To[/COLOR] [COLOR=darkblue]UBound[/COLOR](dontDelete)
[COLOR=darkblue]If[/COLOR] InStr(1, a(i, 1), dontDelete(j), vbTextCompare) > 0 [COLOR=darkblue]Then[/COLOR]
b(i, 1) = 1
[COLOR=darkblue]Exit[/COLOR] [COLOR=darkblue]For[/COLOR]
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
[COLOR=darkblue]Next[/COLOR] j
[COLOR=darkblue]Next[/COLOR] i
Application.ScreenUpdating = [COLOR=darkblue]False[/COLOR]
[COLOR=darkblue]With[/COLOR] Range("A1").Resize(UBound(a), 2)
.Columns(2).Value = b
.Sort Key1:=.Columns(2), Header:=xlNo, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, DataOption1:=xlSortNormal
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]Resume[/COLOR] Next
.Columns(2).SpecialCells(xlBlanks).EntireRow.Delete
[COLOR=darkblue]On[/COLOR] [COLOR=darkblue]Error[/COLOR] [COLOR=darkblue]GoTo[/COLOR] 0
.Columns(2).ClearContents
[COLOR=darkblue]End[/COLOR] [COLOR=darkblue]With[/COLOR]
Application.ScreenUpdating = [COLOR=darkblue]True[/COLOR] [COLOR=darkblue]End[/COLOR][COLOR=darkblue]Sub[/COLOR]
I've tried hacking this code a thousand ways but can't get it to work. Any help is much appreciated!