xancalagonx
Board Regular
- Joined
- Oct 31, 2011
- Messages
- 57
So what I am trying to do is delete unwanted rows in one Sheet if they do not meet the validation values from 2nd sheet.
I have a tag list with tags that are associated to a system. Some systems are not part of the project so I need to purge these from the excel list before I can run a status report
So sheet1 has the complete list of all tags in all systems.
And sheet2 has a list of only the valid systems for current project.
My thought was to run a double For-Next loop where I go through all tags in Sheet1, compare the system to all systems in sheet2, and if I get a hit (ie the tag belongs to a valid system) it changes a boolean value to True.
At end of the For-Next loop it checks the boolean value and if it is False it should delete the entire row containing the tag.
However, when I run the code against a sheet with only invalid systems on it then will only delete half the rows. Then I run the code again and it deletes half of the remaining half. I run it again and it deletes half of whats left... and so on.
I felt the logic here was pretty solid... and it works when I run the code... except it only deletes half the rows that it should... and I have to re-run the code multiple times which, to me, is not making sense.
Perhaps some of you more experienced VBA users will see a glaring mistake in here and could give me a heads up on what is causing it or why it is happening (deleting only half the matches that is).
Thanks
I have a tag list with tags that are associated to a system. Some systems are not part of the project so I need to purge these from the excel list before I can run a status report
So sheet1 has the complete list of all tags in all systems.
And sheet2 has a list of only the valid systems for current project.
My thought was to run a double For-Next loop where I go through all tags in Sheet1, compare the system to all systems in sheet2, and if I get a hit (ie the tag belongs to a valid system) it changes a boolean value to True.
At end of the For-Next loop it checks the boolean value and if it is False it should delete the entire row containing the tag.
However, when I run the code against a sheet with only invalid systems on it then will only delete half the rows. Then I run the code again and it deletes half of the remaining half. I run it again and it deletes half of whats left... and so on.
Code:
Sub Delete Rows()
Application.ScreenUpdating = False
Sheet1.Activate
pProjSubRange = Range("A:A").SpecialCells(xlCellTypeConstants) [COLOR=#00ff00]'selects the range of valid systems from sheet1[/COLOR]
Sheet9.Activate
'ccmsSubRange = Range("B:B").SpecialCells(xlCellTypeConstants) [COLOR=#00ff00]'selects the range/column containing the system numbers to be validated in the main sheet[/COLOR]
Dim pProjSubVerified As Boolean [COLOR=#00ff00]'used to trigger if row should be deleted or not[/COLOR]
For Each ccmsDumpSub In Range("B:B").SpecialCells(xlCellTypeConstants)
pProjSubVerified = False [COLOR=#00ff00]'I reset the value to false at start of each for-next loop.[/COLOR]
For Each pProjSub In pProjSubRange [COLOR=#00ff00]'then I run this For-Next loop inside the other one to compare the system against all the valid systems[/COLOR]
If ccmsDumpSub = "SubSystem" Then GoTo Skip[COLOR=#00ff00] 'I use this line to skip the heading to avoid it deleting the heading due to not finding it in the valid subsystem list.[/COLOR]
If ccmsDumpSub = pProjSub Then pProjSubVerified = True [COLOR=#00ff00]'sets boolean to True if it finds a matching system in the valid system list.[/COLOR]
Next pProjSub
If pProjSubVerified = False Then ccmsDumpSub.EntireRow.Delete[COLOR=#00ff00] 'if it found no matches it deletes the row in the main sheet[/COLOR]
Skip:
Next ccmsDumpSub
I felt the logic here was pretty solid... and it works when I run the code... except it only deletes half the rows that it should... and I have to re-run the code multiple times which, to me, is not making sense.
Perhaps some of you more experienced VBA users will see a glaring mistake in here and could give me a heads up on what is causing it or why it is happening (deleting only half the matches that is).
Thanks