0

The data I have can be simplified to this: https://i.stack.imgur.com/KCr4r.png

In this example, I would like to delete the data associated with track 2, since it has only 3 frames associated with it. All data with more than 3 associated frames can stay.

The frame number does not always start from 1, as I've tried to demonstrate. The track number will always be the same number consecutively for as many frames as are tracked. I was thinking of using a function to append 1 to a variable for every consecutive value in column A, then performing a test to see if this value is equal >= 3. If so, then go onto the next integer in A, if no, then delete all rows marked with that integer (2, in this case).

Is this possible with Visual Basic in an Excel Macro, and can anyone give me some starting tips on what functions I might be able to use? Complete novice here. I haven't found anything similar for VBA, only for R.

1 Answer 1

0

I assume you understand the code by reading it.

Option Explicit
Public Function GetCountOfRowsForEachTrack(ByVal sourceColumn As Range) As _
Scripting.Dictionary
Dim cell As Range
Dim trackValue As String
Dim groupedData As Scripting.Dictionary

Set groupedData = New Scripting.Dictionary
For Each cell In sourceColumn
    trackValue = cell.Value

    If groupedData.Exists(trackValue) Then
        groupedData(trackValue) = cell.Address(False, False) + "," + groupedData(trackValue)
    Else
        groupedData(trackValue) = cell.Address(False, False)
    End If
Next

Set GetCountOfRowsForEachTrack = groupedData
End Function

Public Sub DeleteRowsWhereTrackLTE3()
Dim groupedData As Scripting.Dictionary
Set groupedData = GetCountOfRowsForEachTrack(Range("A2:A15"))

Dim cellsToBeDeleted As String
Dim item
For Each item In groupedData.Items
    If UBound(Split(item, ",")) <= 2 Then
        cellsToBeDeleted = item + IIf(cellsToBeDeleted <> "", "," + cellsToBeDeleted, "")
    End If
Next

Range(cellsToBeDeleted).EntireRow.Delete
End Sub

GetCountOfRowsForEachTrack is a function returning a dictionary (which stores track number as key, cell address associated with that track as string)

DeleteRowsWhereTrackLTE3 is the procedure which uses GetCountOfRowsForEachTrack to get the aggregated info of Track numbers and cells associated with it. This method loops through the dictionary and checks if the number of cells associated with track is <=2 (because splitting the string returns an array which starts from 0). It builds a string of address of such cells and deletes it all at once towards the end.

Note:

  1. Add the following code in a bas module (or a specific sheet where you have the data).
  2. Add reference to "Microsoft Scripting.Runtime" library. Inside VBA, click on "Tools" -> "References" menu. Tick the "Microsoft Scripting.Runtime" and click on OK.
  3. I have used A2:A15 as an example. Please modify it as per your cell range.
  4. The assumption is that you don't have thousands of cells to be deleted, in which case the method could fail.
  5. Make a call to DeleteRowsWhereTrackLTE3 to remove such rows.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.