Results 1 to 7 of 7

Thread: Sort Data By Multiple Columns And Delete Some Rows Based On Criteria

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi

    Try this

    Code:
    Option Explicit
    
    Sub kTest()
        
        Dim i   As Long
        Dim r   As Long
        Dim j   As Long
        Dim v   As Variant
        Dim k   As Variant
        
        Const ResizeCol As Long = 8 '<< adjust this value with actual column count
        Const ReplaceV  As String = "####"
        
        v = Array("% Agree", "Mean score")
        k = Array(999999999, 999999998)
        
        Application.ScreenUpdating = 0
        With ThisWorkbook
            For i = 1 To .Worksheets.Count
                With .Worksheets(i)
                    r = .Range("a" & .Rows.Count).End(xlUp).Row
                    With .Range("a1").Resize(r, ResizeCol)
                        For j = LBound(v) To UBound(v)
                            .Columns(6).Replace v(j), k(j), 1
                        Next
                        .Sort .Cells(2, 2), 1, , .Cells(2, 3), 1, .Cells(2, 6), 2, Header:=1
                        On Error Resume Next
                        .Columns(4).SpecialCells(4) = ReplaceV
                        .Columns(4).Replace "sector-wide", "Sector Wide", 1
                        .Columns(4).Replace "Sector Wide", vbNullString, 1
                        .Columns(4).SpecialCells(4).EntireRow.Delete
                        On Error GoTo 0
                        For j = LBound(v) To UBound(v)
                            .Columns(6).Replace k(j), v(j), 1
                        Next
                        .Columns(4).Replace ReplaceV, vbNullString, 1
                    End With
                End With
            Next
        End With
        Application.ScreenUpdating = 1
                        
    End Sub
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  2. #2
    Junior Member
    Join Date
    Aug 2013
    Posts
    6
    Rep Power
    0
    Great! Thank you. It seems to be working on my real workbook, except for the order of column F, as "% agree" and "mean scores" come at the end rather than on top of the range. It might be my fault as the sample workbook I included had "% agree" and "mean scores" values only for those rows I wanted to be deleted (with sector wide in column D). I attach an amended sample workbook for you to have a look. Thank you again.
    Attached Files Attached Files

Similar Threads

  1. Replies: 3
    Last Post: 05-23-2013, 11:17 PM
  2. Replies: 4
    Last Post: 05-01-2013, 09:49 PM
  3. Delete Remove Rows By Criteria VBA Excel
    By marreco in forum Excel Help
    Replies: 5
    Last Post: 12-20-2012, 05:56 PM
  4. Replies: 2
    Last Post: 06-14-2012, 04:10 AM
  5. Replies: 7
    Last Post: 03-06-2012, 07:49 AM

Posting Permissions

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