+ Reply to Thread
Results 1 to 8 of 8

Arrange numbers in the row according to cells with value only

Hybrid View

  1. #1
    Registered User
    Join Date
    12-14-2013
    Location
    Ukraine
    MS-Off Ver
    Excel 2013
    Posts
    25

    Arrange numbers in the row according to cells with value only

    Hi, there!!
    I have the macro which takes the values from the table and write them into given cells in the row:

    Sub SortingGupta()
    '
    ' SortingGupta Macro
    ' Sorting jobs according to given K
    
    Dim i As Integer, k As Integer
    
     i = 0
     For k = 0 To 29
     Worksheets("Gupta").Cells(45, Chr(66 + i)) = Worksheets("Gupta").Cells(15 + k, "C")
     i = i + 2
     k = k + 2
     Next k
    
    End Sub
    Row looks like this:

    1.JPG

    My goal now is rearrange the numbers in the same row from min to max. I've tried to use excel build in "Sort" function but it's destroy the row after values sorting:

    2.JPG

    I need to keep the order (value-arrow, value-arrow...)
    it is possible to make it work somehow using vba (add the code into existing macro)?
    Thank you in advance,
    Igor
    Last edited by igor7; 05-06-2014 at 09:28 AM.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,022

    Re: Arrange numbers in the row according to cells with value only

    You should describe the actual setup of your sheet, or post a workbook, rather than a trimmed picture that shows nothing useful. Anyway, the better way is to not sort, but assign the values to the cells, like so, which will arrange the values from lowest to highest, in columns B, D, F, .... Z of row 45.

    Sub SortingEveryOtherColumnInARow()
    Dim v As Variant
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    With Worksheets("Gupta")
    For i = 45 To 45 'change if you need different or more rows
    v = Intersect(.Range("B:Z"), .Rows(i)).Value 'get values from columns B to Z
    k = 1
    For j = 2 To 26 Step 2
    .Cells(i, j).Value = Application.WorksheetFunction.Small(v, k)
    k = k + 1
    Next j
    Next i
    End With
    End Sub
    Last edited by Bernie Deitrick; 05-06-2014 at 10:02 AM.
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    12-14-2013
    Location
    Ukraine
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: Arrange numbers in the row according to cells with value only

    Thank you for the answer and sorry for inconvenience...
    I've used your code in the file but it's didn't work... it return following error:
    Attachment 316377
    I've attached whole excel file. I need arrange colored values from min to max in row 45 (after "k=1") and keep existing format (value-arrow):
    Gupta File
    Appreciate your help!!

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,022

    Re: Arrange numbers in the row according to cells with value only

    You need to run your macro first, to put values in, and change the range from B:Z to B:T, and the upper on the loop. The macros can be combined like so - Note that the Chr(...) to return the column letter breaks down after Z, so better to use a number, and it is always a bad idea to increment your loop control variable within the loop.

    Sub SortingGuptaV2()
        Dim v As Variant
        Dim lngRow As Long
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        
        With Worksheets("Gupta")
            i = 2
            For j = 1 To 10
                .Cells(45, i).Value = .Cells(15 + k, "C").Value
                i = i + 2
                k = k + 3
            Next j
            
            lngRow = 45 'sort row 45
            
            v = Intersect(.Range("B:T"), .Rows(lngRow)).Value 'get values from columns B to Z
        
            For j = 2 To 20 Step 2
                .Cells(lngRow, j).Value = Application.WorksheetFunction.Small(v, j / 2)
            Next j
    
        End With
    End Sub
    Last edited by Bernie Deitrick; 05-06-2014 at 12:29 PM.

  5. #5
    Registered User
    Join Date
    12-14-2013
    Location
    Ukraine
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: Arrange numbers in the row according to cells with value only

    WOW!!
    Thanks for quick replay!! Now it's working!!
    I'm just learning to use vba so you help is much appreciated!
    May be you can help me with additional question...
    Within the same file under A45 (k = 1) row I have "Job" row.
    So in this row I should arrange job numbers (1,2,3,4,5,6,7,8,9,10) according to the colored values it the table.
    For example if value "-0.17" belong to job "1" in the "Job" row I should write "1" under "-0.17" value.
    In other words after values are arranged from min to max in the row "job" should be job numbers arranged accordingly.
    Than you in advance,
    igor
    Last edited by igor7; 05-06-2014 at 12:52 PM.

  6. #6
    Registered User
    Join Date
    12-14-2013
    Location
    Ukraine
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: Arrange numbers in the row according to cells with value only

    So far I've find the way to do what I need with simple cell command (repeated in each cell accordingly):

    =IF(H45=C15,A14,IF(H45=C18,A17,IF(H45=C21,A20,IF(H45=C24,A23,IF(H45=C27,A26,IF(H45=C30,A29,IF(H45=C33,A32,IF(H45=C36,A35,IF(H45=C39,A38,IF(H45=C42, A41))))))))))
    But problem with this method that if there same number appeared in different cells (for example C15 and C18) it always pick up first in the order.

    I've also tried to add to the code above line:
    .Cells(46, i).Value = .Cells(14 + k, "A").Value
    Like this:

    Sub SortingGuptaV2()
        Dim v As Variant
        Dim lngRow As Long
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        
        With Worksheets("Gupta")
            i = 2
            For j = 1 To 10
                .Cells(45, i).Value = .Cells(15 + k, "C").Value
                .Cells(46, i).Value = .Cells(14 + k, "A").Value
                i = i + 2
                k = k + 3
            Next j
            
            lngRow = 45 'sort row 45
            
            v = Intersect(.Range("B:T"), .Rows(lngRow)).Value 'get values from columns B to Z
        
            For j = 2 To 20 Step 2
                .Cells(lngRow, j).Value = Application.WorksheetFunction.Small(v, j / 2)
            Next j
    
        End With
    End Sub
    But it's writing numbers in the order (1, 2, 3 and so on) without relation that I need.
    Your help will be much appreciated!

  7. #7
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,022

    Re: Arrange numbers in the row according to cells with value only

    When you need to sort values keeping things together, it is better to create a range to sort elsewhere. Try it this way, which assumes that rows 145 and 146 are blank.

    Sub SortingGuptaV3()
        Dim v As Variant
        Dim lngRow As Long
        Dim i As Integer
        Dim j As Integer
        Dim k As Integer
        
        With Worksheets("Gupta")
            
            For j = 1 To 10
                .Cells(145, j).Value = .Cells(15 + k, "C").Value
                .Cells(146, j).Value = .Cells(14 + k, "A").Value
                k = k + 3
             Next j
               
            .Sort.SortFields.Clear
            .Sort.SortFields.Add Key:=.Cells(145, 1), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
            .Sort.SetRange .Cells(145, 1).Resize(2, 10)
            .Sort.Header = xlNo
            .Sort.Orientation = xlLeftToRight
            .Sort.Apply
        
            For j = 1 To 10
                .Cells(45, j * 2).Value = .Cells(145, j).Value
                .Cells(46, j * 2).Value = .Cells(146, j).Value
            Next j
            
            .Cells(145, 1).Resize(2, 10).Clear
        End With
    End Sub

  8. #8
    Registered User
    Join Date
    12-14-2013
    Location
    Ukraine
    MS-Off Ver
    Excel 2013
    Posts
    25

    Re: Arrange numbers in the row according to cells with value only

    Thanks!
    I'm even didn't think it this way!!

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 2
    Last Post: 03-29-2014, 10:04 AM
  2. macro to arrange rows in numerical order using numbers in column
    By blairw in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 03-19-2013, 10:58 PM
  3. [SOLVED] Arrange numbers in a sequence
    By Joe in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 04-13-2006, 12:50 AM
  4. Replies: 2
    Last Post: 08-15-2005, 11:05 AM

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