+ Reply to Thread
Results 1 to 9 of 9

Sort Descending

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    112

    Sort Descending

    Hello Excel Gurus:

    I need help for the revision of the VBA code below to sort DESCENDING on the “TR DATE “ (starting from cell Y7).

    Any help would be appreciated.

    Thank you.
    Option Explicit
    Option Compare Text
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Application.EnableEvents = False
    Application.ScreenUpdating = False
    
    Dim Rng         As Range
    Dim Dn          As Range
    Dim Dic         As Object
    Dim Temp        As String
    Dim oCols       As String
    Dim R           As Range
    Dim C           As Long
    
    Dim ws As Worksheet
    
    With Sheets("Q-1")
      .EnableCalculation = False
    End With
    
    Set ws = Sheets("PubQ1")
    
       If Not Application.Intersect(Target, Range("C3")) Is Nothing Then
           Target(1).Value = UCase(Target(1).Value)
       End If
        
     With Sheets("dBase")
        Set Rng = .Range(.Range("J8"), .Range("J" & Rows.Count).End(xlUp))
     End With
    
    Set Dic = CreateObject("scripting.dictionary")
        Dic.CompareMode = vbTextCompare
        
    C = 7
    
    For Each Dn In Rng
        If Not Dic.exists(Dn.Value) Then
            Dic.Add Dn.Value, Dn
        Else
            Set Dic.Item(Dn.Value) = Union(Dic.Item(Dn.Value), Dn)
        End If
    Next
    
    C = 7
        With Sheets("PubQ1")
           .Unprotect Password:="."
           .AutoFilterMode = False
           .Range(.Range("N6"), .Range("N" & Rows.Count).End(xlUp)).Resize(, 20).ClearContents
           .Range("N7").Resize(, 20).Value = Array("KEY DATE", "KEYED BY", "PUB/LOC/COMMENT", "PUB/LOC", "REC", "ISS", "ADJ QTY", "PICK QTY", "TR DATE", "PUB NO.", "DESCRIPTION", "TR DATE", "TR TYPE", "TR QTY", "PALLET", "ROW", "POS", "LEVEL", "UNI LOC", "COMMENT")
            If Dic.exists(.Range("C3").Value) Then
                For Each R In Dic.Item(.Range("C3").Value)
                  C = C + 1
                    .Range("N" & C).Resize(, 20).Value = R.Offset(, -9).Resize(, 20).Value  ‘I think revision is on this line will sort the result
                Next R
          End If
    End With
    
     If Not Sheets("PubQ1").AutoFilterMode Then
      ws.Unprotect Password:="."
      ws.Range("A7:G7").AutoFilter
      ws.Protect _
            contents:=True, _
            AllowFiltering:=True, _
            UserInterfaceOnly:=True, _
            Password:="."
     End If
     
    Application.EnableEvents = True
    Application.ScreenUpdating = True
    
    End Sub

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258

    Re: Sort Descending

    Hello GEMINI52,

    There is a lot going on here with references. It would be best if you upload a copy of the workbook for review.

    If your workbook contains any personal, private, or confidential information then please redact it before you post.
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Contributor
    Join Date
    12-01-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    112

    Sort Descending

    Hello Leith Ross,

    Here is the file that was requested.

    The attached file has 2 sheets, dBase and PubQ1 sheet and has a working code. Type the pub number on cell “B3” and it will display KEY DATE, KEYED BY, TR DATE, TR TYPE, TR QTY, UNIFIED LOC and COMMENT coming from dBase sheet.

    The revision I want is result to be sorted (descending) on the TR DATE and display only the 6 column (KEY DATE, KEYED BY, TR DATE, TR TYPE, etc.). As of now the column that I don’t want to show is just hidden.

    Also, please check the code to set the print area is correct.

    If the code has to be changed, it is most welcome. I have 25,000 rows and it runs slow.

    Thank you and hope to receive your revision ASAP.
    Attached Files Attached Files

  4. #4
    Forum Contributor
    Join Date
    12-01-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Sort Descending

    Thank you for your quick response and will let you know once i tested it. Happy thanksgiving.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Sort Descending

    separate threads merged.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,670

    Re: Sort Descending

    Try replace sheet module code with (Deleted Sheet_Activate code)
    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address(0, 0) <> "B3" Then Exit Sub
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        [a7].CurrentRegion.Offset(1).ClearContents
        If [b3] <> "" Then
            With Sheets("dbase")
                With .Range("a6", .Cells.SpecialCells(11))
                    .Parent.AutoFilterMode = False
                    .AutoFilter 10, [b3]
                    With .Offset(1)
                        Union(.Columns("a:b"), .Columns("i"), .Columns("m:n"), _
                                .Columns("s:t")).Copy [a8]
                    End With
                    .AutoFilter
                End With
            End With
        End If
        [a7].CurrentRegion.Sort [c7], 2, , , , , , True
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub
    Attached Files Attached Files
    Last edited by jindon; 11-21-2015 at 05:49 PM. Reason: File attached

  7. #7
    Forum Contributor
    Join Date
    12-01-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    112

    Re: Sort Descending

    Tested the code and it works but a little bit slow on 25K rows. I few more twerks i need on sheet "PubQ1" (if i may?)

    The revisions i need:
    1. When you type on cell "B3", i want it in all CAPS even if you type in small caps.
    2. Keep formatting on destination cells (A8:G)
    3. Always set filter from cell (A8:G) and
    4. The sheet should be protected.

    I am a newbie on macro code, so forgive me for my ignorance.

    I hope you can help me again. Thank you...

  8. #8
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,670

    Re: Sort Descending

    Not sure about 2).

    Sheet protected under "password"
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address(0, 0) <> "B3" Then Exit Sub
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        Me.Unprotect "password"
        [a7].CurrentRegion.Offset(1).ClearContents
        If Target <> "" Then
            Target.Value = UCase$(Target.Value)
            With Sheets("dbase")
                With .Range("a6", .Cells.SpecialCells(11))
                    .Parent.AutoFilterMode = False
                    .AutoFilter 10, Target
                    With .Offset(1)
                        Union(.Columns("a:b"), .Columns("i"), .Columns("m:n"), _
                                .Columns("s:t")).Copy
                        [a8].PasteSpecial xlPasteValues
                        If Not Me.AutoFilterMode Then [a7].CurrentRegion.AutoFilter
                        Target(2).Select
                    End With
                    .AutoFilter
                End With
            End With
        End If
        [a7].CurrentRegion.Sort [c7], 2, , , , , , True
        Me.Protect "password", , , , , , , , , , , , , , True
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub

  9. #9
    Forum Contributor
    Join Date
    12-01-2012
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    112
    Hello again,
    It works perfectly for my needs, two more request.
    1. Can you add a second sort? UNIFIED LOC (ascending)
    2. Cell B3 is merge with C3 and D4 and it does not clear A8:G when cell B3 is blank.

    Just for curiosity:
    CurrentRegion has 6 commas between 2 and True, what does it represent.
    Likewise, me.Protect... has 14 commas,
    Please educate me.

    Thank you...



    QUOTE=jindon;4247948]Not sure about 2).

    Sheet protected under "password"
    Private Sub Worksheet_Change(ByVal Target As Range)
        If Target.Address(0, 0) <> "B3" Then Exit Sub
        With Application
            .EnableEvents = False
            .ScreenUpdating = False
        End With
        Me.Unprotect "password"
        [a7].CurrentRegion.Offset(1).ClearContents
        If Target <> "" Then
            Target.Value = UCase$(Target.Value)
            With Sheets("dbase")
                With .Range("a6", .Cells.SpecialCells(11))
                    .Parent.AutoFilterMode = False
                    .AutoFilter 10, Target
                    With .Offset(1)
                        Union(.Columns("a:b"), .Columns("i"), .Columns("m:n"), _
                                .Columns("s:t")).Copy
                        [a8].PasteSpecial xlPasteValues
                        If Not Me.AutoFilterMode Then [a7].CurrentRegion.AutoFilter
                        Target(2).Select
                    End With
                    .AutoFilter
                End With
            End With
        End If
        [a7].CurrentRegion.Sort [c7], 2, , , , , , True
        Me.Protect "password", , , , , , , , , , , , , , True
        With Application
            .EnableEvents = True
            .ScreenUpdating = True
        End With
    End Sub
    [/QUOTE]

+ 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. Data sort then sort of same value in descending days of the week
    By jimbob121 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-05-2014, 02:32 PM
  2. [SOLVED] Sort Table Descending
    By Jonathan78 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-24-2013, 03:05 AM
  3. [SOLVED] Sort descending not bound to worksheet.
    By Rob* in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-28-2012, 05:21 AM
  4. [SOLVED] Sort a column descending: VBA
    By niko79542 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-23-2012, 04:16 PM
  5. VBA to sort column descending - can't get it right!
    By wonderdunder in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-18-2011, 04:29 AM
  6. Using sort descending in VBA
    By eonizuka in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-05-2010, 06:05 PM
  7. Sort in descending order
    By shahcu in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2008, 03:01 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