I'm super new to VBA and hoping I could get some help please ... I'm looking for a succinct way to remove rows from a sheet via Macro and the VBA samples I've tried I cant get to work. I'm hoping it's something simple
The macro would remove a row that DOES NOT contain a specific values (from a specific column) from within an array. I sort of had it working, and full disclosure, I've borrowed code from other examples. The latest sample I'm working with just deletes everything, the other sample I was working with deleted Acro32.exe, but kept everything else. So not yet arrived at a solution.
Background: I have a CSV output from an application auditing tool, which spits out tonnes of app data from various computers. That CSV data will be copied into my master 'reporting' spreadsheet. I am only interested in reviewing and keeping data about specific applications, namely Chrome.exe, Firefox.exe, Acro32.exe and Winword.exe. The application name is always found in Column F. So, any cell contents found in Column F, that DOES NOT contain a value in an array, that entire row needs to be deleted. Ideally and the remaining rows will only include apps I'm interested in as defined in the array.
Any thoughts would be appreciated. I have posted on another forum and will update this forum with the response in case it helps someone else should one occur (and vice versa).
Thanks
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">PublicFunction GetLastRow(ByVal rngToCheck As Range)AsLong
Dim rngLast As Range
Set rngLast = rngToCheck.Find(what:="*", searchorder:=xlByRows, searchdirection:=xlPrevious)
If rngLast IsNothingThen
GetLastRow = rngToCheck.Row
Else
GetLastRow = rngLast.Row
EndIf
EndFunction
Sub Apps_Formatting()
Dim varList AsVariant
Dim lngLastRow AsLong, lngCounter AsLong
Dim rngToCheck As Range, rngFound As Range
Dim rngToDelete As Range, rngDifferences As Range
Dim blnFound AsBoolean
Application.ScreenUpdating =False
With ActiveSheet
lngLastRow = GetLastRow(.Cells)
'we don't want to delete our header row
Set rngToCheck =.Range("A2:A"& lngLastRow)
EndWith
If lngLastRow >1Then
With rngToCheck
'any Cell in Column F that contains one of these values are KEPT
'and if not found in cell, then the entire row is deleted.
varList = VBA.Array("Chrome.exe","Firefox.exe","Acro32.exe")
For lngCounter = LBound(varList)To UBound(varList)
Set rngFound =.Find( _
what:=varList(lngCounter), _
Lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=True)
'check if we found a value we want to keep
IfNot rngFound IsNothingThen
blnFound =True
'if there are no cells with a different value then
'we will get an error
OnErrorResumeNext
Set rngDifferences =.ColumnDifferences(Comparison:=rngFound)
OnErrorGoTo0
IfNot rngDifferences IsNothingThen
If rngToDelete IsNothingThen
Set rngToDelete = rngDifferences
Else
Set rngToDelete = Application.Intersect(rngToDelete, rngDifferences)
EndIf
EndIf
EndIf
Next lngCounter
EndWith
If rngToDelete IsNothingThen
IfNot blnFound Then rngToCheck.EntireRow.Delete
Else
rngToDelete.EntireRow.Delete
EndIf
EndIf
Application.ScreenUpdating =True
EndSub</code>
The macro would remove a row that DOES NOT contain a specific values (from a specific column) from within an array. I sort of had it working, and full disclosure, I've borrowed code from other examples. The latest sample I'm working with just deletes everything, the other sample I was working with deleted Acro32.exe, but kept everything else. So not yet arrived at a solution.
Background: I have a CSV output from an application auditing tool, which spits out tonnes of app data from various computers. That CSV data will be copied into my master 'reporting' spreadsheet. I am only interested in reviewing and keeping data about specific applications, namely Chrome.exe, Firefox.exe, Acro32.exe and Winword.exe. The application name is always found in Column F. So, any cell contents found in Column F, that DOES NOT contain a value in an array, that entire row needs to be deleted. Ideally and the remaining rows will only include apps I'm interested in as defined in the array.
Any thoughts would be appreciated. I have posted on another forum and will update this forum with the response in case it helps someone else should one occur (and vice versa).
Thanks
<code style="margin: 0px; padding: 0px; border: 0px; font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; line-height: inherit; font-family: Consolas, Menlo, Monaco, "Lucida Console", "Liberation Mono", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Courier New", monospace, sans-serif; vertical-align: baseline; box-sizing: inherit; white-space: inherit;">PublicFunction GetLastRow(ByVal rngToCheck As Range)AsLong
Dim rngLast As Range
Set rngLast = rngToCheck.Find(what:="*", searchorder:=xlByRows, searchdirection:=xlPrevious)
If rngLast IsNothingThen
GetLastRow = rngToCheck.Row
Else
GetLastRow = rngLast.Row
EndIf
EndFunction
Sub Apps_Formatting()
Dim varList AsVariant
Dim lngLastRow AsLong, lngCounter AsLong
Dim rngToCheck As Range, rngFound As Range
Dim rngToDelete As Range, rngDifferences As Range
Dim blnFound AsBoolean
Application.ScreenUpdating =False
With ActiveSheet
lngLastRow = GetLastRow(.Cells)
'we don't want to delete our header row
Set rngToCheck =.Range("A2:A"& lngLastRow)
EndWith
If lngLastRow >1Then
With rngToCheck
'any Cell in Column F that contains one of these values are KEPT
'and if not found in cell, then the entire row is deleted.
varList = VBA.Array("Chrome.exe","Firefox.exe","Acro32.exe")
For lngCounter = LBound(varList)To UBound(varList)
Set rngFound =.Find( _
what:=varList(lngCounter), _
Lookat:=xlWhole, _
searchorder:=xlByRows, _
searchdirection:=xlNext, _
MatchCase:=True)
'check if we found a value we want to keep
IfNot rngFound IsNothingThen
blnFound =True
'if there are no cells with a different value then
'we will get an error
OnErrorResumeNext
Set rngDifferences =.ColumnDifferences(Comparison:=rngFound)
OnErrorGoTo0
IfNot rngDifferences IsNothingThen
If rngToDelete IsNothingThen
Set rngToDelete = rngDifferences
Else
Set rngToDelete = Application.Intersect(rngToDelete, rngDifferences)
EndIf
EndIf
EndIf
Next lngCounter
EndWith
If rngToDelete IsNothingThen
IfNot blnFound Then rngToCheck.EntireRow.Delete
Else
rngToDelete.EntireRow.Delete
EndIf
EndIf
Application.ScreenUpdating =True
EndSub</code>