Results 1 to 11 of 11

Thread: [RESOLVED] Rank - VBA

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Resolved [RESOLVED] Rank - VBA

    Hi Experts!

    I am in great trouble help me out with this.

    Here is my excel sheet view

    Rank Name
    1 A
    1 B
    1 C
    2 A
    2 B
    2 C
    3 A
    3 B
    3 C

    Question

    Now I want when I run a macro I should get only Top two values from each Rank.
    Example output
    1 A
    1 B
    2 A
    2 B
    3 A
    3 B

    Could you please give me a code for this?

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Rank - VBA

    Excel VBA question moved to Office Development

  3. #3
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Rank - VBA

    I should get only Top two values from each Rank.
    Get them where?
    Do you want the values to be
    returned in an array?
    written to a new worksheet?
    written to the current worksheet?
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  4. #4

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Re: Rank - VBA

    We can put them in new worksheet

  5. #5
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Rank - VBA

    Try this
    VB Code:
    1. Sub TopTwo()
    2. Dim rngData As Range
    3. Dim asOutput() As String
    4. Dim lRowNum As Long
    5. Dim bOn2ndRow As Boolean
    6. Dim wksNewSheet As Worksheet
    7.  
    8.     'You will need to change this line to correctly refer
    9.     'to your data range (excluding the header row)
    10.     Set rngData = ThisWorkbook.Worksheets(1).Range("A2:B10")
    11.    
    12.     'Initialize the array
    13.     ReDim asOutput(0 To 1, 0 To 0)
    14.    
    15.    
    16.     With rngData
    17.         'Sort by Rank and then name
    18.         .Sort Key1:=.Cells(1, 1), Order1:=xlAscending, _
    19.             Key2:=.Cells(1, 2), Order2:=xlAscending, _
    20.             Header:=xlNo, OrderCustom:=1, MatchCase:=False, _
    21.             Orientation:=xlTopToBottom, DataOption1:=xlSortNormal, _
    22.             DataOption2:=xlSortNormal
    23.            
    24.        
    25.         'load the first record into the array
    26.         asOutput(0, 0) = .Cells(1, 1).Value
    27.         asOutput(1, 0) = .Cells(1, 2).Value
    28.         'note that we are next loading a "2nd" row
    29.         bOn2ndRow = True
    30.        
    31.         'Loop through the rest of the rows
    32.         For lRowNum = 2 To .Rows.Count
    33.            
    34.             'If the Next rank is different from the last recorded...
    35.             If .Cells(lRowNum, 1).Value <> asOutput(0, UBound(asOutput, 2)) Then
    36.                
    37.                 '...Add a new record to the array
    38.                 ReDim Preserve asOutput(0 To 1, UBound(asOutput, 2) + 1)
    39.                 asOutput(0, UBound(asOutput, 2)) = .Cells(lRowNum, 1)
    40.                 asOutput(1, UBound(asOutput, 2)) = .Cells(lRowNum, 2)
    41.                
    42.                 '...and note that we are next loading a "2nd" row
    43.                 bOn2ndRow = True
    44.                            
    45.             'If we are loading a "2nd" Row
    46.             ElseIf bOn2ndRow Then
    47.                
    48.                 '...Add a new record to the array
    49.                 ReDim Preserve asOutput(0 To 1, UBound(asOutput, 2) + 1)
    50.                 asOutput(0, UBound(asOutput, 2)) = .Cells(lRowNum, 1)
    51.                 asOutput(1, UBound(asOutput, 2)) = .Cells(lRowNum, 2)
    52.                
    53.                 '...and note that we are next loading a "1st" row
    54.                 bOn2ndRow = False
    55.             End If
    56.         Next lRowNum
    57.     End With
    58.    
    59.     'Add a new Sheet
    60.     Set wksNewSheet = ThisWorkbook.Worksheets.Add
    61.    
    62.     With wksNewSheet.Range("A1")
    63.         'Write the Headers
    64.         .Value = "Rank"
    65.         .Offset(0, 1).Value = "Name"
    66.        
    67.         'Copy the array onto the new sheet
    68.         For lRowNum = 0 To UBound(asOutput, 2)
    69.             .Offset(1 + lRowNum, 0) = asOutput(0, lRowNum)
    70.             .Offset(1 + lRowNum, 1) = asOutput(1, lRowNum)
    71.         Next lRowNum
    72.     End With
    73.    
    74.     'Clear Object variables
    75.     Set wksNewSheet = Nothing
    76. End Sub
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Re: Rank - VBA

    hey I found out...how about this :

    VB Code:
    1. Sub getNames()
    2. Dim i As Integer
    3. Dim wx As Integer
    4. Dim nx As Integer
    5. Dim mik As Boolean
    6. Dim ctch As String
    7. Dim chng As String
    8. mik = True
    9.  
    10. i = 3
    11. wx = 3
    12. nx = 0
    13.     While (mik)
    14.     Range("a" & i).Select
    15.     ctch = ActiveCell.Value
    16.         If ctch = "" Then
    17.         mik = False
    18.         End If
    19.         If chnge = ctch Then
    20.         nx = nx + 1
    21.         Else
    22.         nx = 0
    23.         End If
    24.             If nx < 2 Then
    25.                 If ctch = "" Then
    26.                 Else
    27.                 Range("e" & wx).Select
    28.                 ActiveCell.Value = "Rank : " & ctch
    29.                 Range("f" & wx).Select
    30.                 ActiveCell.Value = Range("b" & i).Text
    31.                 wx = wx + 1
    32.                 End If
    33.             End If
    34.    
    35.    
    36.     chnge = ctch
    37.     i = i + 1
    38.     Wend
    39.    
    40. End Sub

    I can see that your code is little more complex but what I want to know is that...can u find any difference in that?? If yes then i would love to know coz I cannot find any...

    Not good with the excel so cant read ur code much

    Thanks!!!!
    Vikas Bhandari

  7. #7
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Rank - VBA

    I can find lots of differences.
    First off, you should avoid using the ActiveCell method whenever possible - it can lead to some really bad code. You should also avoid using the Select method.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  8. #8

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Re: Rank - VBA

    Any specific reasons?

  9. #9

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Re: Rank - VBA

    Any other thoughts that why using activecell is bad...apart of a fact that it slows down the processing time......

    Thanks,Vikas

  10. #10
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: Rank - VBA

    Yes, it cases when you need to cede control to other processs while your code is running, by envoking DoEvents, there is the potential for the user to select a cell. This will change the activecell and your code could subsequently call the activecell, this will result in your procedure getting an incorrect value.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  11. #11

    Thread Starter
    Member
    Join Date
    Jun 2006
    Posts
    35

    Re: Rank - VBA

    darnnnnn..i never thought so..... yeah its right!!! Thanks for the clarification...

    Regards,Vikas

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