Results 1 to 5 of 5

Thread: VBA in Excel: How to keep relevant rows only? [Resolved by NeedSomeAnswers]

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    3

    Resolved VBA in Excel: How to keep relevant rows only? [Resolved by NeedSomeAnswers]

    Hi everybody!

    Do you know, guys, about some easy way how to tell VBA in Excel to go thourgh all cells in worksheet and keep those ones with highest number of each 'value' in list similar to the following?

    value1 6
    value1 3
    value1 5
    value1 7
    value2 5
    value2 3
    value3 5
    value4 2
    value4 6

    Basically, is it possible to get via some universal VBA from the list above this selection below?

    value1 7
    value2 5
    value3 5
    value4 6

    Any ideas, please feel free to post!

    Regards,
    John
    Last edited by jl88; Oct 1st, 2004 at 03:33 AM.

  2. #2
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Hi John,

    The following code should work, but first to make it work you need to create a Range.

    To do this you simply highlight the cells with the "value 1" , "value 2" (this should be only 1 column, i.e B1 TO B20) data in them and go to :

    Insert
    -- Name
    -- Define

    and name the range "colb"

    VB Code:
    1. Private Sub GetHighestValues()
    2.  
    3. Dim strValues() As String
    4. Dim lngAmounts() As Long
    5. Dim n, i As Integer
    6. Dim max As Long
    7.  
    8. n = 1
    9. i = 1
    10.  
    11. For Each cell In Range("colb")
    12.     If n > 1 Then
    13.         If cell.Value <> strValues(i - 1) Then
    14.             ReDim Preserve strValues(i)
    15.             ReDim Preserve lngAmounts(i)
    16.             strValues(i) = cell.Value
    17.             lngAmounts(i) = cell.Offset(0, 1).Value
    18.             i = i + 1
    19.         Else
    20.             If cell.Offset(0, 1).Value > lngAmounts(i - 1) Then
    21.                 lngAmounts(i - 1) = cell.Offset(0, 1).Value
    22.             End If
    23.         End If
    24.     Else
    25.         ReDim Preserve strValues(i)
    26.         ReDim Preserve lngAmounts(i)
    27.         strValues(i) = cell.Value
    28.         lngAmounts(i) = cell.Offset(0, 1).Value
    29.         i = i + 1
    30.     End If
    31.     n = n + 1
    32. Next cell
    33.  
    34. max = UBound(strValues)
    35. n = 1
    36.  
    37. For Each cell In Range("D1", "D" & CStr(max))
    38.     cell.Value = strValues(n)
    39.     cell.Offset(0, 1).Value = lngAmounts(n)
    40.     n = n + 1
    41. Next
    42.  
    43. End Sub


    The results are placed by the second "FOR" loop, in column D, but you can obviously change that if you want !

    If you have any problems with the code, post back !

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2004
    Posts
    3

    Thumbs up

    Hi,

    thanks a lot, it's just perfect solution! As a newbie here, I am really amazed that you (unknown) took the time to sit down and think of this issue. Since

    I speak in VB for the last three days only, I still don't understand how could you get it, but I am sure I will get it (one day...)

    Thanks again and have a nice day!

    John

  4. #4
    Superbly Moderated NeedSomeAnswers's Avatar
    Join Date
    Jun 2002
    Location
    Manchester uk
    Posts
    2,657
    Hi John, no problem !

    i have recieved help quite a few times from this forum, and so i like to help back when i can.

    Richard

  5. #5
    New Member
    Join Date
    Nov 2004
    Posts
    1
    Hello,
    I also have a Excel problem and im a total newbie with VB. Can someone explain this code to me? Its suppose to delete rows with "0" but when its ran its skips a block of rows on my sheet. Also can you explain the Range(selection) row.
    Thanks,


    Delete all rows with no account field
    Application.WindowState = xlMinimized
    Sheets("MAIN").Select
    Workbooks.Open Filename:=Range("A200")
    Selection.End(xlDown).Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.EntireRow.Delete

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width