+ Reply to Thread
Results 1 to 7 of 7

Thread: Ranking with vba or ?

  1. #1
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2003
    Posts
    129

    Ranking with vba or ?

    Hello All - - I have a task around ranking. I have a file that contains about 200 rows. Column A has a name of a company, column B has total revenue. I want to rank these from 1 to whatever. But the problem comes in, I only want to rank every other row. So, I may start with cell B15 for example, then every other row after that, to cell B202. My question is a 'either or' type of thing, can the =rank function do this? Or would vba do that.

    If with vba i was thinking there could be an input box that might ask for a starting and ending cell (like B15, B202) and then do it's work. I've attached a sample data file, basically I want all the 'grayed' rows ranked but not the none grayed. Any thoughts suggestions would be great!

    UPdated - - Sorry - - I was just looking at the file, and it's not every other row. It would be any cell that is highightled that I would want to rank. I noticed that some of the rows actually skipped two or three, not just the one. I hope that makes sense...thanks again!
    Attached Files Attached Files
    Last edited by Ironman; 10-29-2010 at 11:14 AM. Reason: Complete - -

  2. #2
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Ranking with vba or ?

    Hi

    I've put this into column I for the moment so you can see the output separately. See how it goes.

    Sub aaa()
      holder = ""
      For i = 2 To Cells(Rows.Count, 2).End(xlUp).Row
        If Not (Cells(i, 2).Interior.ColorIndex = xlNone) Then
          holder = holder & Cells(i, 2).Address(ReferenceStyle:=xlR1C1) & ","
          Cells(i, "I").FormulaR1C1 = "=rank(rc2,salary)"
        End If
      Next i
      
      ActiveWorkbook.Names.Add Name:="salary", RefersTo:="=" & Left(holder, Len(holder) - 1)
    End Sub
    rylo

  3. #3
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Ranking with vba or ?

    Thanks Rylo - - Sorry for my late response. I tried this on the test file and it seemed to work great. Once I copied it into the actual data file the cell value was #name?. There is a bit of a change in the layout but I thought I had it all tweaked. The actual Rank Column would be A, the column I wish to rank is found in column K. Sorry for this, I thought I'd be able to reference the columns and be OK, but I guess not! Any thoughts/suggestions on how to further tweak your vba code?

    Thanks again, and I do appologize for my late response to your very good work!

  4. #4
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Ranking with vba or ?

    Hi

    Put up a new example file with the revised structure.

    rylo

  5. #5
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Ranking with vba or ?

    Hi Rylo - - I took the real file and changed the names of the companies and the Dollar values. The Rank Column is Column A. The column I wish to rank is Column K, only those in shaded.

    Also, as a side note ( I think I might be able to firgure this one out, but) you might notice also in Column K that is shaded is the subtotal and total values. Those I would not want to rank. From report to report (I'll be doing about 30 of these) and each one has different row numbers, so I was looking to add come code that would look down Column G for the words Sub Totals or Totals and if found to offset the 5th cell over and change it's background shade to none, then run though the ranking code, thus not ranking the subtotal and total value.

    I've attached the file, hope this helps and thanks again for your attention to this!
    Attached Files Attached Files

  6. #6
    Forum Guru
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    5,359

    Re: Ranking with vba or ?

    Hi

    How about
    Sub bbb()
      Dim holder As Range
      Dim first As Boolean
      first = True
      For i = 15 To Cells(Rows.Count, 9).End(xlUp).Row
        If Not (Cells(i, 9).Interior.ColorIndex = xlNone) And Left(Cells(i, 7), 3) <> "Sub" And Left(Cells(i, 7), 3) <> "TOT" Then
          If first Then
            Set holder = Cells(i, 9)
            first = False
          Else
            Set holder = Union(holder, Cells(i, 9))
          End If
        End If
      Next i
      
      For i = 15 To Cells(Rows.Count, 9).End(xlUp).Row
        If Not (Cells(i, 9).Interior.ColorIndex = xlNone) And Left(Cells(i, 7), 3) <> "Sub" And Left(Cells(i, 7), 3) <> "TOT" Then
          Cells(i, "A").FormulaR1C1 = WorksheetFunction.Rank(Cells(i, 9).Value, holder)
        End If
      Next i
      
    End Sub
    rylo

  7. #7
    Forum Contributor
    Join Date
    01-16-2009
    Location
    Ill.
    MS-Off Ver
    Excel 2003
    Posts
    129

    Re: Ranking with vba or ?

    Rylo - - Right on! Thanks...I've tested this by changing the order...ect and rerunning. Works GREAT! I also was able to get the other loop process in place to unshade those cells that are offset from the Subtotal, total rows. Then this code runs, thus not 'ranking' those that were changed to xlnone..between the two pieces of code it works great!

    I thought I'd post the two pieces of code and save this on as Solved...here's the first part that unshades the sub total and total cells (offset) from column G to K:

    With Worksheets("Cur").Range("g:g")
    Set c = Cells.Find(What:="TOTALS", after:=[a1], LookIn:=xlFormulas, LookAt:= _
    xlPart, SearchOrder:=xlByRows, SearchDirection:=xlAll, MatchCase:=False)
        If Not c Is Nothing Then
            firstaddress = c.Address
            Do
                c.Offset(0, 4).Interior.ColorIndex = xlNone
                    Set c = .FindNext(c)
                    Loop While Not c Is Nothing And c.Address <> firstaddress
                End If
            End With
    And here's the code you just sent me, again works really really well:

    Dim holder As Range
      Dim first As Boolean
      first = True
      For i = 15 To Cells(Rows.Count, 11).End(xlUp).Row
        If Not (Cells(i, 11).Interior.ColorIndex = xlNone) And Left(Cells(i, 7), 3) <> "Sub" And Left(Cells(i, 7), 3) <> "TOT" Then
          If first Then
            Set holder = Cells(i, 11)
            first = False
          Else
            Set holder = Union(holder, Cells(i, 11))
          End If
        End If
      Next i
      
      For i = 15 To Cells(Rows.Count, 11).End(xlUp).Row
        If Not (Cells(i, 11).Interior.ColorIndex = xlNone) And Left(Cells(i, 7), 3) <> "Sub" And Left(Cells(i, 7), 3) <> "TOT" Then
          Cells(i, "A").FormulaR1C1 = WorksheetFunction.Rank(Cells(i, 11).Value, holder)
        End If
      Next i
    Have a good day and enjoy the weekend!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.2.0