+ Reply to Thread
Results 1 to 17 of 17

Selection Process finding "x"s from within 7 columns

Hybrid View

curbster Selection Process finding... 09-10-2009, 09:28 PM
Paul Re: Selection Process finding... 09-11-2009, 12:15 AM
curbster Re: Selection Process finding... 09-11-2009, 11:36 AM
curbster Re: Selection Process finding... 09-11-2009, 05:05 PM
ravishankar Re: Selection Process finding... 09-12-2009, 12:48 PM
curbster Re: Selection Process finding... 09-12-2009, 08:22 PM
curbster Re: Selection Process finding... 09-13-2009, 09:56 AM
curbster Re: Selection Process finding... 09-14-2009, 07:49 PM
curbster Re: Selection Process finding... 09-16-2009, 12:21 PM
StephenR Re: Selection Process finding... 09-16-2009, 12:46 PM
curbster Re: Selection Process finding... 09-16-2009, 02:11 PM
StephenR Re: Selection Process finding... 09-16-2009, 03:22 PM
curbster Re: Selection Process finding... 09-16-2009, 03:32 PM
StephenR Re: Selection Process finding... 09-16-2009, 03:36 PM
  1. #1
    Registered User
    Join Date
    08-11-2009
    Location
    Gilbert, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    82

    Selection Process finding "x"s from within 7 columns

    Hi I have a process in which I have to create a selection without bias. There are 9 categories in this selection process. The 1st and 2nd category already have code and they work differently from the rest. For the 3rd through the 8th categories I have to choose one from the 3rd category, then one from the 4th, etc. After the 8th category I start back at the 3rd and go to the 4th, ect. It ends when cell A1 reaches 30%.

    Its a little difficult to explain but the process is simple enough to understand once you see it. I have attached an example of the process. I have also started the code and left explanations within the code.

    Sub selection_process()
    
    Dim x As Range, lastRow As Long
    lastRow = ActiveSheet.UsedRange.Rows.Count
    On Error Resume Next
    
    'Sort Column L, Descending, and N, Ascending with a header row:
    Range("A2:AZ" & lastRow).Sort Key1:=Range("L2"), Order1:=xlDescending, key2:=Range("N2"), _
    order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    
    'Place a "1" in SEL column for each row which containS an "x" in column C.
    For Each x In Range("C2:C" & lastRow)
        If x.Value = "x" Then
           x.Offset(0, -1) = "1"
        End If
    Next x
    
    ''''''''''''START LOOP'''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    'start at cell E2 and go down to the first "x" in the column.
    'If column B of same row contains a number, go to next "x" below in column E
    'if column B of same row equals nothing, place a "3" in column B
    
    'start at active cell in E and go right one column and then down to the first "x" in the column.
    'If column B of same row contains a number go to next "x" below in column F
    'if column B of same row equals nothing, place a "4" in column B
    
    'start at active cell in F and go right one column and then down to the first "x" in the column.
    'If column B of same row contains a number, go to next "x" below in column G
    'if column B of same row equals nothing, place a "5" in column B
    
    'start at active cell in G and go right one column and then down to the first "x" in the column.
    'If column B of same row contains a number go to next "x" below in column H
    'if column B of same row equals nothing, place a "6" in column B
    
    'start at active cell in H and go right one column and then down to the first "x" in the column.
    'If column B of same row contains a number, go to next "x" below in column I
    'if column B of same row equals nothing, place a "7" in column B
     
    'start at active cell in I and go right one column and then down to the first "x" in the column.
    'If column B of same row contains a number, go to next "x" below in column J
    'if column B of same row equals nothing, place an "8" in column B
    
    'Then start the process over starting with '''''''start loop''''' above.
    
    'Continue the process until the formula in A1 reaches 30% (it will probably not be right at 30%, give or take a few percent)
    
    '''''''''''''END LOOP''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    
    End Sub
    I'm hoping there are just a few lines of code that I can loop until the cell A1 reaches 30%.

    Please help!
    Thank you!
    Curbster
    Attached Files Attached Files
    Last edited by curbster; 09-16-2009 at 06:54 PM. Reason: Solved

  2. #2
    Forum Expert Paul's Avatar
    Join Date
    02-05-2007
    Location
    Wisconsin
    MS-Off Ver
    2016/365
    Posts
    6,887

    Re: Selection Process finding "x"s from within 7 columns

    Hi Curbster,

    A few questions...

    1. In your pseudo-code, you say for each part of the loop "Start at cell D2", "Start at cell E2", etc. but your example spreadsheet shows gray boxes for the first round of selection don't follow that pattern. It looks like you start at D2 and find the first "x" below that, then you go over to column E in that row and find the first "x" from there downward, then move to column F from that row and find the first "x". If you truly wanted to start at the top of each column and find the first "x" from the top, I would expect to see D32, E5, F2, G2, H2, I2 and J2 selected in the first round. Am I correct in my observation?

    2. What happens when you get to the "last row" in a column and 30% has not yet been reached? Do you start back at the top of that column? (This assumes my observation in question 1 was correct, and that you're not simply starting at D2, E2, F2, etc. every step through the loop.

    Thanks.

  3. #3
    Registered User
    Join Date
    08-11-2009
    Location
    Gilbert, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    82

    Re: Selection Process finding "x"s from within 7 columns

    Paul,
    you are exactly right! My mistake. So once a selection is made, go to the next column (same row) and head downwards. Lets say I just selected a 6, I would go over to column 7, and then down to the first x. If I hit the bottom and have not found an x in that column, I start back at the top (in the same column) until I find the next 7. If there are no 7's left, I go on to the next column, column 8.

    Thanks for helping me clear this up. I'll edit it above as well.

    is it even possible to write code to do this?
    Thanks!
    Last edited by curbster; 09-11-2009 at 04:02 PM.

  4. #4
    Registered User
    Join Date
    08-11-2009
    Location
    Gilbert, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    82

    Re: Selection Process finding "x"s from within 7 columns

    any help on this?

    thanks!

  5. #5
    Forum Contributor
    Join Date
    02-27-2008
    Posts
    764

    Re: Selection Process finding "x"s from within 7 columns

    Hi
    try the macro, it gave 29.58%. may be I am missing something.
    Ravi
    Attached Files Attached Files

  6. #6
    Registered User
    Join Date
    08-11-2009
    Location
    Gilbert, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    82

    Re: Selection Process finding "x"s from within 7 columns

    I appreciate your help! Its close. Is there any way to get it to start back at the top after every round? For example column 3 starts the next X down from the top instead of the next x down from the last x in column 8?

    Also, I tried deleting the selection numbers and running the code, it only got me to 22%. I think it is because it is not starting over at the top. If I reach the bottom before I finish an 8 selection, I start back at the top and grab the first x in column 8 (unless it has already been selected by another number). I do this for all columns. Another example, if after the selection has started and I run out of 3's, I go on to the 4's. So I would start at 4, then go right to 5, 6, 7, 8, and back to 4, 5, 6... etc.

    I tried to figure out the code but I'm no good at creating the code yet. Thank you so much for your help!
    Last edited by curbster; 09-16-2009 at 03:53 PM.

  7. #7
    Registered User
    Join Date
    08-11-2009
    Location
    Gilbert, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    82

    Re: Selection Process finding "x"s from within 7 columns

    Ravi, here is the sheet with the expected results. I apologize if I was unclear on my original thread. Thanks for taking the time to help out!
    Curbster.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    08-11-2009
    Location
    Gilbert, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    82

    Re: Selection Process finding "x"s from within 7 columns

    Anyone avalible to help with this? Thanks!

  9. #9
    Registered User
    Join Date
    08-11-2009
    Location
    Gilbert, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    82

    Re: Selection Process finding "x"s from within 7 columns

    I'm not sure if my post is showing up on the page since the original post was from last week, but I'm still in need of some help if anyone knows how to do this. Thanks!

  10. #10
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Selection Process finding "x"s from within 7 columns

    Looks interesting, but in your example A1 is already over 30%. Can you post another example?

  11. #11
    Registered User
    Join Date
    08-11-2009
    Location
    Gilbert, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    82

    Re: Selection Process finding "x"s from within 7 columns

    Ok, I got most of it working for how I need, now it just needs to keep looping back to the top until cell A1 equals around 32% (currently it only reaches 13.52%).

    once you have run the macro, see the comment in cell J141 to explain.

    Sub selection_process()
    
    Dim lastRow As Long
    lastRow = ActiveSheet.UsedRange.Rows.Count
    On Error Resume Next
    
    'Sort Column L, Descending, and N, Ascending with a header row:
    Range("A2:AZ" & lastRow).Sort Key1:=Range("L2"), Order1:=xlDescending, key2:=Range("N2"), _
    order2:=xlAscending, Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _
    DataOption1:=xlSortNormal
    
    Dim onex As Range
    'Place a "1" in SEL column for each row which containS an "x" in column C.
    For Each onex In Range("C2:C" & lastRow)
        If onex.Value = "x" Then
           onex.Offset(0, -1) = "1"
        End If
    Next onex
    
    Dim twox As Range
    
    'Place a "2" in SEL column for each row which containS an "x" in column D.
    'skip any cell within column B that already contains a value of 1.
    For Each twox In Range("D2:D" & lastRow)
        If twox.Value = "x" And twox.Offset(0, -2) = "" Then
           twox.Offset(0, -2) = "2"
        End If
    Next twox
    
    'selects x's in column 3 through column 8 in a certain order.
            'this is not looping right yet.
    Dim a As Long, b As Long, c As Long, y As Long
    y = Cells(Rows.Count, 1).End(xlUp).Row
    b = 5
    c = 4
        For a = 2 To y
        If Cells(1, 1).Value < 0.32 Then
            If Cells(a, b) = "x" And Cells(a, 2) = "" Then
                  Cells(a, 2) = b - 2
                    Cells(a, b).Interior.ColorIndex = c
                    b = b + 1
                   
                    If b = 11 Then
                        b = 5
                        c = c + 1
                        End If
            End If
            Else
            Exit For
            End If
        Next a
        MsgBox "complete"
        End Sub
    thanks for your help!
    Attached Files Attached Files
    Last edited by curbster; 09-16-2009 at 02:18 PM.

  12. #12
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Selection Process finding "x"s from within 7 columns

    I'm a little confused about that code works at all because originally you said:
    'start at cell D2 and go down to the first "x" in the column.
    'If column B of same row contains a number go to next "x" below in column D
    'if column B of same row equals nothing, place a "2" in column B
    But in the code just posted you have this section
    For Each twox In Range("D2:D" & lastRow)
        If twox.Value = "x" And twox.Offset(0, -2) = "" Then
           twox.Offset(0, -2) = "2"
        End If
    Next twox
    which means that there will always be a value in B whenever D is 'x'. Am I missing something?

  13. #13
    Registered User
    Join Date
    08-11-2009
    Location
    Gilbert, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    82

    Re: Selection Process finding "x"s from within 7 columns

    you are correct, I realized that I preselect rows 1 and 2 (or, columns C and D) and those are done first and remain untouched. I had to change the code a little to reflect the selection process to only choose columns 3-8.

    If the cell in column B (SEL column) contains a number, then go to next row and continue searching down. So as far as the code is concerned for onex and twox it works perfectly for what I need.

    the code I can't figure out is below that. I have come up with a loop but it is not working the way I need it to. It overwrites the 1 and 2 columns and doesn't seem to stop where it should, at 32%.

    'selects x's in column 3 through column 8 in a certain order.
            'this is not working right yet.
    Dim a As Long, b As Long, c As Long, y As Long
    y = Cells(Rows.Count, 1).End(xlUp).Row
    b = 5
    c = 4
     
        Do While Cells(1, 1).Value <= 0.3
          For a = 2 To y
            If Cells(a, b) = "x" And Cells(a, 2) = "" Then
                  Cells(a, 2) = b - 2
                    Cells(a, b).Interior.ColorIndex = c
                    b = b + 1
                   
                    If b = 11 Then
                        b = 5
                        c = c + 1
                    End If
            End If
                    
        Next a
        
        Loop
        MsgBox "complete"
        
        End Sub

  14. #14
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: Selection Process finding "x"s from within 7 columns

    I have written some code and got it up to c19%, but it appears that for every x in col F, col B has a number. Should it then jump to the next column? Are you sure that this does have a solution!?

  15. #15
    Registered User
    Join Date
    08-11-2009
    Location
    Gilbert, Arizona, USA
    MS-Off Ver
    Excel 2003
    Posts
    82

    Re: Selection Process finding "x"s from within 7 columns

    Right, if there are no more 3's for example, the selection would continue to 4-8, if it randomly runs out of 7 to select, then it should keep selecting from the remaining columns.

    Since this is dummy data and i have changed the numbers in column W (to see what the macro would do when it ran out of x's) it may not reach 32%. The formula in A1 should be divided by the sum of column W.

    ex. =SUM(A2:A150)/1242557
    where 1,242,557 equals sum of W

    Like I said I did change the numbers around to see what errors I would get with the macro. Feel free to change the formula to reflect the "/W:W"

+ 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.6.0 RC 1