+ Reply to Thread
Results 1 to 18 of 18

Sort by column in another sheet

  1. #1
    Forum Contributor
    Join Date
    11-24-2016
    Location
    Libya
    MS-Off Ver
    Office 365
    Posts
    526

    Sort by column in another sheet

    Hi vba experts
    in my file, there are 2 sheets Sheet1 & Sheet2
    Sheet1 has data in column a (the original names with the data already sorted in certain way)
    Sheet2 has the same names but shuffled with some other data in multiple columns (A1:P200)

    I wanted to sort the data in Sheet2 by the data in column a in sheet1

    e.g
    sheet1
    ^^^^
    aa
    bb
    cc
    dd
    11
    22
    33
    44
    ee
    ff

    Sheet2
    ^^^^
    11 ...... any .......... any .........any
    cc ...... any .......... any .........any
    ee ...... any .......... any .........any
    44 ...... any .......... any .........any
    aa ...... any .......... any .........any
    33 ...... any .......... any .........any
    dd ...... any .......... any .........any
    22 ...... any .......... any .........any
    ff ...... any .......... any .........any
    bb ...... any .......... any .........any

  2. #2
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Post Re: Sort by column in another sheet

    ( removed )
    Last edited by Marc L; 04-13-2022 at 08:36 AM.

  3. #3
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Sort by column in another sheet


    As a reminder without any attachment you will have to amend yourself the VBA procedure for what we won't guess …
    Last edited by Marc L; 04-13-2022 at 08:14 AM.

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Sort by column in another sheet


    As you can achieve it yourself just using a helper column with a MATCH formula
    in order to sort the range according to this helper column (the same under VBA) …

  5. #5
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Try this …


    A VBA demonstration well working with smart worksheet design for a starting point :

    PHP Code: 
    Sub Demo1()
            
    Dim V
        With Sheet2
    .UsedRange
            V 
    Application.Match(Sheet1.UsedRange.Columns(1), .Columns(1), 0)
            If 
    Application.Count(V) < UBound(VThen Beep: Exit Sub
           
    .Value2 Application.Index(.Value2VEvaluate("COLUMN(" & .Rows(1).Address ")"))
        
    End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  6. #6
    Forum Contributor
    Join Date
    11-24-2016
    Location
    Libya
    MS-Off Ver
    Office 365
    Posts
    526

    Re: Sort by column in another sheet

    Thank you. it works well for letter data but not for numbers
    Attached Files Attached Files

  7. #7
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: Sort by column in another sheet


    As it works like a charm on my side with smart worksheets so the bad is on yours ‼

    First this is not my original procedure and you badly modified it !
    Your issue is Sheet1 has numbers but Sheet2 has numbers as texts ! Both sheets must have the same data type …

  8. #8
    Forum Contributor
    Join Date
    11-24-2016
    Location
    Libya
    MS-Off Ver
    Office 365
    Posts
    526

    Re: Sort by column in another sheet

    When used Application.Count(V) I got exit sub and the following lines are not executed
    The data type is General in both sheets. I couldn't figure it out

  9. #9
    Forum Contributor
    Join Date
    11-24-2016
    Location
    Libya
    MS-Off Ver
    Office 365
    Posts
    526

    Re: Sort by column in another sheet

    I didn't modify anything except count to counta

  10. #10
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Sort by column in another sheet


    Yes it's a safety for non smart worksheets when the data do not match !
    'Cause you bypassed it by modifying the procedure you got the error …

    Replace each number as text (marked with a green triangle in upper left corner) in Sheet2 with real number then that works …
    Last edited by Marc L; 04-13-2022 at 10:32 AM.

  11. #11
    Forum Contributor
    Join Date
    11-24-2016
    Location
    Libya
    MS-Off Ver
    Office 365
    Posts
    526

    Re: Sort by column in another sheet

    You are right. So sorry
    Now it worked well
    Thank you so so much sir

  12. #12
    Forum Contributor
    Join Date
    11-24-2016
    Location
    Libya
    MS-Off Ver
    Office 365
    Posts
    526

    Re: Sort by column in another sheet

    The original file worked well but I got NA at the last row of the result sheet (that I sorted)

  13. #13
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: Sort by column in another sheet


    No such issue on my side …

    Last row # : 11 ?

  14. #14
    Forum Contributor
    Join Date
    11-24-2016
    Location
    Libya
    MS-Off Ver
    Office 365
    Posts
    526

    Re: Sort by column in another sheet

    I don't know what's wrong. I wanted to sort sheet2 as the names in sheet1
    Attached Files Attached Files

  15. #15
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: Sort by column in another sheet


    Well, well, well … You first forgot to update the column index as in Sheet2 the names are not anymore in column #1 like in your first attachment !

    And as I warned it works only with smart worksheets which is not the case in your last attachment
    so you must use the CurrentRegion of a cell reference rather than the worksheet UsedRange

  16. #16
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Lightbulb Try this !


    According to post #4 for your post #14 attachment :

    PHP Code: 
    Sub Demo2()
       Const 
    2
         Dim L
    &
        
    With Sheet2.[A1].CurrentRegion.Columns
            
    .Item(.Count 1).Formula "=MATCH(" & .Cells(C).Address(FalseFalse) & "," _
                                        Sheet1
    .[A1].CurrentRegion.Columns(1).Address(, , , True) & ",0)"
            
    .Resize(, .Count 1).Sort .Item(.Count 1), 1Header:=1
             L 
    Application.Count(.Item(.Count 1).Offset(1))
            .
    Item(.Count 1).ClearContents
             
    If < .Rows.Count 1 Then Range(.Cells(2C), .Cells(.Rows.CountC)).Interior.Color vbRed
        End With
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !

  17. #17
    Forum Contributor
    Join Date
    11-24-2016
    Location
    Libya
    MS-Off Ver
    Office 365
    Posts
    526

    Re: Sort by column in another sheet

    The last code worked well. Thank you so much sir MarcL

  18. #18
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Re: Sort by column in another sheet


    Try it with your post #6 attachment as it is with the constant C set to 1 …

+ 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. [SOLVED] Sort by column in one sheet and maintain row formula references in another sheet
    By ChronicMixTapes in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 09-24-2021, 11:08 AM
  2. Replies: 1
    Last Post: 03-29-2018, 07:58 AM
  3. [SOLVED] sort values in column C per sheet
    By k1dr0ck in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-30-2017, 07:34 AM
  4. [SOLVED] Sort data to other sheet based on column value
    By phbryan in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-10-2016, 11:37 AM
  5. Replies: 1
    Last Post: 07-06-2014, 10:25 PM
  6. [SOLVED] Find column heading then sort the sheet off that column
    By daillest319 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-11-2012, 10:00 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