Results 1 to 3 of 3

Dynamic sort with dynamic sort criteria

Threaded View

  1. #1
    Registered User
    Join Date
    12-02-2012
    Location
    El Paso, Texas
    MS-Off Ver
    Excel 2010
    Posts
    6

    Dynamic sort with dynamic sort criteria

    Hello,

    My problem is this. I need to sort a range, apply a formula to the sorted information and then sort again by the results of the new formula. After that I need to keep the top row of information as is, recalculate all the remaining rows with the same formula. The second row uses values from the first row. After the recalculation is done I need to sort the new values again and then sort them. This process repeats until all the rows are done. I am new at coding and I have pieced together some code which is close but I am having trouble updating my new sort ranges on each iteration. Here is what I have so far.

       'this loop dynamicly calculates and inserts the slack time
        
        Cells(1, 8).Value = "slack time"
        Cells(2, 8).Value = Cells(2, 5) - Cells(2, 3) - 0
        
        Dim a As Integer, b As Integer
    
     'this loop calculates the slack time
    
    Application.ScreenUpdating = False
     a = 3
    
     For b = 1 To Sheets("sheet1").Cells(61, 2)
        
         Cells(a, 8).Value = Cells(a, 5) - Cells(a, 3) - (Cells(a - 1, 3) + Cells(a - 1, 4))
       ''
       '
       'this portion sorts the jobs after each slack time had been calculated
       
        Range("A1:h51").Select
        Application.CutCopyMode = False
        ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Clear
       ActiveWorkbook.Worksheets("Sheet2").Sort.SortFields.Add Key:=Range("ga+1:g51")
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
    
        With ActiveWorkbook.Worksheets("Sheet2").Sort
            .SetRange Range("A1:h51")
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
         End With
         
         
         
         
     
            a = a + 1
        Next b

    I highlighted the section which is not correct. It seems like that would be an easy thing to do, but like i said I'm new to this language and my vocabulary is limited.

    By the way most of the code I have is copied from recorded macros or from this forum so if the code looks familiar its probably yours and thank you.

    David
    Last edited by david.herrera1; 12-07-2012 at 02:00 AM. Reason: change color on font

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