Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: Sort Range Using VBA And Ignoring Or Discarding Blank Rows

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 one. adjust the ranges accordingly.

    Code:
    Option Explicit
    Sub Sort1()
    
        Dim NonBlankData(), n As Long, r As Long, c As Long
        Dim WholeData
        
        WholeData = Range("R3:AC17").Value2
        
        ReDim NonBlankData(1 To UBound(WholeData, 1), 1 To UBound(WholeData, 2))
        
        For r = 1 To UBound(WholeData, 1)
            '//check whether the first column has data. if so, proceed. Replace the 1 with appropriate column to check
            If Len(WholeData(r, 1)) Then
                n = n + 1
                For c = 1 To UBound(WholeData, 2)
                    NonBlankData(n, c) = WholeData(r, c)
                Next
            End If
        Next
        
        If n Then
            With Range("AE3")
                .Resize(n, UBound(NonBlankData, 2)).Value2 = NonBlankData
                With .Resize(n, UBound(NonBlankData, 2))
                    .sort Key1:=.Cells(1, 1), Order1:=xlAscending, Key2:=.Cells(1, 8), _
                    Order2:=xlDescending, Key3:=.Cells(1, 9), Order3:=xlAscending, _
                    Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
                    xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
                    DataOption3:=xlSortNormal
                End With
            End With
        End If
        
    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
    Member
    Join Date
    Aug 2013
    Posts
    59
    Rep Power
    13


    That worked like a Charm, Thanks a ton.

    For other ranges in the same worksheet, I guess, I only need to change

    Code:
    WholeData = Range("R3:AC17").Value2
    and rest of the code should be repeated for each range, no need to change the variable, also, I guess. I'll surely try for other ranges.

  3. #3
    Member
    Join Date
    Aug 2013
    Posts
    59
    Rep Power
    13
    Code given in #11 works perfectly right, over the given range.

    But, Now I want to repeate this process over other row range with same columns, so, I repeated the code as under (without variable, as that have already been declared)

    Code:
     WholeData = Range("R102:AC116").Value2
        
        ReDim NonBlankData(1 To UBound(WholeData, 1), 1 To UBound(WholeData, 2))
        
        For r = 1 To UBound(WholeData, 1)
            '//check whether the first column has data. if so, proceed. Replace the 1 with appropriate column to check
            If Len(WholeData(r, 1)) Then
                n = n + 1
                For c = 1 To UBound(WholeData, 2)
                    NonBlankData(n, c) = WholeData(r, c)  '  Error - Run time error 9, Subscript out of Range
                Next
            End If
        Next
        
        If n Then
            With Range("AE102")
                .Resize(n, UBound(NonBlankData, 2)).Value2 = NonBlankData
                With .Resize(n, UBound(NonBlankData, 2))
                    .sort Key1:=.Cells(1, 1), Order1:=xlAscending, Key2:=.Cells(1, 8), _
                    Order2:=xlDescending, Key3:=.Cells(1, 9), Order3:=xlAscending, _
                    Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
                    xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
                    DataOption3:=xlSortNormal
                End With
            End With
        End If
    I need to perform similar sorting over row
    3 to 17
    102 to 116
    202 to 216
    302 to 316 and so on till 1000+ rows

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

    I guess the output you want in continuous range. If so, try this

    Code:
    Option Explicit
    
    Sub SortData(ByRef Destination As Range, ParamArray SourceData())
    
        Dim NonBlankData(), n As Long, r As Long, c As Long
        Dim WholeData, i As Long
        
        Const MaxOutputRows     As Long = 1000
        Const OutputColumns     As Long = 12
        
        ReDim NonBlankData(1 To MaxOutputRows, 1 To 12)
        
        For i = LBound(SourceData) To UBound(SourceData)
            WholeData = SourceData(i)
            For r = 1 To UBound(WholeData, 1)
                If Len(WholeData(r, 1)) Then
                    n = n + 1
                    For c = 1 To UBound(WholeData, 2)
                        NonBlankData(n, c) = WholeData(r, c)
                    Next
                End If
            Next
        Next
        If n Then
            With Destination
                .Resize(n, UBound(NonBlankData, 2)).Value2 = NonBlankData
                With .Resize(n, UBound(NonBlankData, 2)) 'adjust the sorting columns. here it's 1,8 and 9th column of the output range
                    .Sort Key1:=.Cells(1, 1), Order1:=xlAscending, Key2:=.Cells(1, 8), _
                    Order2:=xlDescending, Key3:=.Cells(1, 9), Order3:=xlAscending, _
                    Header:=xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:= _
                    xlTopToBottom, DataOption1:=xlSortNormal, DataOption2:=xlSortNormal, _
                    DataOption3:=xlSortNormal
                End With
            End With
        End If
        
    End Sub
    
    '***** and call the procedure like this...
    
    Sub kTest()
        
        Dim rngDest As Range
        
        Set rngDest = Worksheets("OSum").Range("AE102") '<< adjust the sheet name and the range
        
        With Worksheets("OSum")
            SortData rngDest, .Range("ae3:ap17").Value2, .Range("ae102:ap116").Value2, .Range("ae202:ap216").Value2 '<< add more ranges here
        End With
        
    End Sub
    Last edited by Admin; 02-07-2014 at 11:03 AM.
    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)

  5. #5
    Member
    Join Date
    Aug 2013
    Posts
    59
    Rep Power
    13
    The revised sheet is attached, with more data in subsequent block every 100th row has 10 rows+column matrix which needs to be copied and sorted.

    Somehow, unable to do.

    Data position should not be changed, as in the adjacent area many rows columns have data from which some processing is being done, and final output is obtained, which is required to be re-arranged (sorted) from Column AE. Please have a look.
    Attached Files Attached Files
    Last edited by analyst; 02-06-2014 at 02:15 PM.

  6. #6
    Member
    Join Date
    Aug 2013
    Posts
    59
    Rep Power
    13
    I've 15 such blocks having 10 rows each at interval of 100 rows. Should i create 15 variables, and re-write or how to do, it? Kindly explain.

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

    As I told you add those 15 ranges in this procedure as an argument and run the macro.

    Code:
    Sub kTest()
        
        Dim rngDest As Range
        
        Set rngDest = Worksheets("OSum").Range("AE102") '<< adjust the sheet name and the range
        
        With Worksheets("OSum")
            'I have added 3 ranges here
            SortData rngDest, .Range("r3:ac17").Value2, .Range("r102:ac116").Value2, .Range("r202:ac216").Value2 '<< add more ranges here separated by comma
        End With
        
    End Sub
    Note: I have amended the SortData procedure in my earlier post.
    Last edited by Admin; 02-07-2014 at 11:04 AM.
    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)

  8. #8
    Member
    Join Date
    Aug 2013
    Posts
    59
    Rep Power
    13
    My poor understanding, am not able to do the requisite.

  9. #9
    Member
    Join Date
    Aug 2013
    Posts
    59
    Rep Power
    13
    I am trying, instead of putting all range in single line, am making different block, and it is working, thank u. Thank you Admin.

Similar Threads

  1. Delete blank rows
    By dhivya.enjoy in forum Excel Help
    Replies: 5
    Last Post: 10-23-2013, 10:04 PM
  2. Insert blank rows based on cell value
    By muhammad susanto in forum Excel Help
    Replies: 13
    Last Post: 09-11-2013, 06:18 AM
  3. Replies: 6
    Last Post: 08-14-2013, 04:25 PM
  4. Replies: 2
    Last Post: 05-06-2011, 02:59 AM
  5. Deleting blank rows
    By Rasm in forum Excel and VBA Tips and Tricks
    Replies: 0
    Last Post: 04-14-2011, 03:14 AM

Tags for this Thread

Posting Permissions

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