+ Reply to Thread
Results 1 to 7 of 7

Keep SpecialCell(XlCellTypeComments) on top while sorting data

Hybrid View

ImranBhatti Keep... 03-28-2018, 06:58 PM
MarvinP Re: Keep... 03-28-2018, 07:10 PM
ImranBhatti Re: Keep... 03-29-2018, 02:47 PM
vichopalacios Re: Keep... 04-05-2018, 01:29 PM
ImranBhatti Re: Keep... 04-10-2018, 03:34 PM
vichopalacios Re: Keep... 04-11-2018, 08:20 AM
ImranBhatti Re: Keep... 04-11-2018, 01:33 PM
  1. #1
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Keep SpecialCell(XlCellTypeComments) on top while sorting data

    Hi

    I have a worksheet with comments in A column (some cells contains comments and some don't)
    their are 2 adjacent columns filled with data (B and C)

    I want to sort my dataset on the basis of A column but the cells containing the comments should remain on top and the rest of the cells should be sorted beneath them (the comments cells should remain on top but should be sorted at their own place)

    Help would be greatly appreciated

    Best Regards
    Imran Bhatti
    Teach me Excel VBA

  2. #2
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,451

    Re: Keep SpecialCell(XlCellTypeComments) on top while sorting data

    Hi Imran,

    I think you are looking for the Review Tab and click on "Show All Comments". Then when you sort column A the comment boxes stay where they are and only the lines to the cells move.

    https://www.howtogeek.com/218633/how...ines-in-excel/
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  3. #3
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: Keep SpecialCell(XlCellTypeComments) on top while sorting data

    Hi sir MarvinP
    Thanks for the review

    I have attached a sample showing "Before sorting" and "After sorting"
    hope that helps to understand the requirement easily.
    Attached Files Attached Files

  4. #4
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Keep SpecialCell(XlCellTypeComments) on top while sorting data

    hi @ImranBhatti
    Here you can find a simple piece of code to do your requirement.
    It's using column D as auxiliar
    I hope it's helpfull for you

    Sub SortComments()
    Dim myWs As Worksheet
    Dim myRange As Range
    Dim LastRow As Long
    Dim i As Long
    
    Set myWs = Worksheets("Sheet2")
    With myWs
        LastRow = .Cells(Rows.Count, "A").End(xlUp).Row
        Set myRange = .Range("A1:D" & LastRow)
        For i = 1 To LastRow
            If .Cells(i, 1).Comment Is Nothing Then
                .Cells(i, 4).Value = 1
            Else
                .Cells(i, 4).Value = 0
            End If
        Next i
    End With
    myRange.Select
    With myWs.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("A1:A" & LastRow), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange myRange
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    With myWs.Sort
        .SortFields.Clear
        .SortFields.Add Key:=Range("D1:D" & LastRow), _
        SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        .SetRange myRange
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With
    End Sub
    Barriers are there for those who don't want to dream

  5. #5
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: Keep SpecialCell(XlCellTypeComments) on top while sorting data

    Doing exactly what I want.Any chance we can avoid the use of helper column OR can we assign a column that is not adjacent to the datagrid I mean can I use XFD column as helper?

  6. #6
    Valued Forum Contributor
    Join Date
    08-07-2014
    Location
    Quito, Ecuador
    MS-Off Ver
    Excel 2016 & 365, Windows 10
    Posts
    511

    Re: Keep SpecialCell(XlCellTypeComments) on top while sorting data

    As far as I know, sorting is not working on discontinuos ranges.
    I think the simplest way would be inserting a column before the procedure ( just after the Dim instructions), and deleting it after.

  7. #7
    Valued Forum Contributor ImranBhatti's Avatar
    Join Date
    03-27-2014
    Location
    Rawalpindi,Pakistan
    MS-Off Ver
    Office 365
    Posts
    1,785

    Re: Keep SpecialCell(XlCellTypeComments) on top while sorting data

    Quote Originally Posted by vichopalacios View Post
    I think the simplest way would be inserting a column before the procedure ( just after the Dim instructions), and deleting it after.
    Clever move.Thanks

+ 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: 1
    Last Post: 12-16-2015, 01:04 PM
  2. Replies: 0
    Last Post: 02-24-2014, 11:27 AM
  3. Sorting all data based on data in single column (DATES)
    By subbby in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-16-2013, 08:19 AM
  4. [SOLVED] Data Analysis: Comparing 3 columns, sorting, removing unique values, display data
    By kmills2626 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 10-14-2013, 07:27 AM
  5. Sorting 2 data ranges by comparing one column in each and sorting to match
    By MDKsmiffy in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 06-17-2013, 03:30 PM
  6. Replies: 3
    Last Post: 12-20-2012, 06:16 AM
  7. Sorting, finding dulicates, moving one data element up, deleting original data
    By rickwtx in forum Excel Programming / VBA / Macros
    Replies: 22
    Last Post: 01-13-2011, 07:32 PM

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