+ Reply to Thread
Results 1 to 12 of 12

Sort function does not correctly reference the answer from the input box.

Hybrid View

herbie226 Sort function does not... 10-10-2012, 02:22 PM
Norie Re: Sort function does not... 10-10-2012, 02:26 PM
herbie226 Re: Sort function does not... 10-10-2012, 02:31 PM
Norie Re: Sort function does not... 10-10-2012, 04:25 PM
LeapingLizard Re: Sort function does not... 10-10-2012, 04:40 PM
herbie226 Re: Sort function does not... 10-10-2012, 06:43 PM
herbie226 Re: Sort function does not... 10-13-2012, 05:56 PM
jeffreybrown Re: Sort function does not... 10-13-2012, 06:25 PM
herbie226 Re: Sort function does not... 10-13-2012, 07:57 PM
jeffreybrown Re: Sort function does not... 10-14-2012, 04:52 PM
JosephP Re: Sort function does not... 10-15-2012, 03:15 AM
herbie226 Re: Sort function does not... 10-16-2012, 10:30 AM
  1. #1
    Registered User
    Join Date
    04-25-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    91

    Sort function does not correctly reference the answer from the input box.

    Hello,

    The code below does not reference the answer from the input box. I am unsure how to make the sort "Custom order" to reference the answer given in the input box.

    Any help would be appreciated.

    Thanks!


    Sub A_Test()
    
    Answer = Application.InputBox("Please enter sort criteria", "Sort")
    
    Range("A1").Select
                    
    'This is the section that does not correctly reference the answer from the input box.
      
      ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("A2:XFD2"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
            Answer, DataOption:=xlSortNormal
        With ActiveSheet.Sort
            .SetRange ActiveSheet.UsedRange
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
    
    'Everything below has no issues working.
      ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("A4:XFD4") _
            , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
        With ActiveSheet.Sort
            .SetRange ActiveSheet.UsedRange
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
        
        Cells.Find(What:="#", after:=ActiveCell, LookIn:=xlValues, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False).Activate
        ActiveCell.Columns("A:A").EntireColumn.Select
        Selection.Cut
        Columns("A:A").Select
        Selection.Insert Shift:=xlToRight
        Range("A2").FormulaR1C1 = "Time"
        Range("A3").FormulaR1C1 = "sec"
        Range("A4").ClearContents
        Rows("4:4").Select
        Selection.Copy
        Rows("5:5").Select
        Selection.Insert Shift:=xlDown
        Range("A1").Select
    
    End Sub

  2. #2
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Sort function does not correctly reference the answer from the input box.

    What is being entered via the input box?

  3. #3
    Registered User
    Join Date
    04-25-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    91

    Re: Sort function does not correctly reference the answer from the input box.

    Hi Norie,

    It will be a column title.

    Thanks.

  4. #4
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,645

    Re: Sort function does not correctly reference the answer from the input box.

    I don't think you can just supply a column name.

    I think it needs to be a list which each item separated by commas.

  5. #5
    Forum Contributor
    Join Date
    03-28-2012
    Location
    Pennsylvania, USA
    MS-Off Ver
    Excel 2010
    Posts
    166

    Re: Sort function does not correctly reference the answer from the input box.

    Use Application.Inputbox, with Type:=8

    On Error Resume Next
    Set rng = Application.Inputbox("Select range",Type:=8)
    If Not rng Is Nothing Then
    rng.Sort
    'etc.
    TRy setting it up this way.
    Last edited by Cutter; 10-15-2012 at 12:00 PM. Reason: Added code tags

  6. #6
    Registered User
    Join Date
    04-25-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    91

    Re: Sort function does not correctly reference the answer from the input box.

    LeapingLizard I am not understanding your advice, sorry. I am a novice with VBA.

    To clarify, I am attempting to sort data from left-to-right based upon row2 using a dynamic value which is entered by the user through the input box. The issue is linking the answer from the input box to the custom sort.

    I can do this sorting manually but I need to incorporate this into a larger macro which can handle many operations.

    The issue is designating the "Answer" in the sort criteria. I am unsure of the format. See below.

    Answer = Application.InputBox("Please enter sort criteria", "Sort")
    
    Range("A1").Select
                    
      
      ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("A2:XFD2"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
            Answer, DataOption:=xlSortNormal
        With ActiveSheet.Sort
            .SetRange ActiveSheet.UsedRange
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
    If for example I use the following, the macro runs with no issues.

      ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("A2:XFD2"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
            "GGG", DataOption:=xlSortNormal
        With ActiveSheet.Sort
            .SetRange ActiveSheet.UsedRange
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
    The problem is the turning the format "GGG" into the 'Answer'.

    Thanks!
    Last edited by herbie226; 10-11-2012 at 01:43 PM.

  7. #7
    Registered User
    Join Date
    04-25-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    91

    Re: Sort function does not correctly reference the answer from the input box.

    Bumping Thread!!!

  8. #8
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Sort function does not correctly reference the answer from the input box.

    Can you provide a sample workbook with data and your sort macro (the one that works and the one you are trying)?

    Please also explain what you are trying to achieve with the CustomOrder
    HTH
    Regards, Jeff

  9. #9
    Registered User
    Join Date
    04-25-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    91

    Re: Sort function does not correctly reference the answer from the input box.

    Hello Jeff,

    Thank you for your assistance.


    This is the code that works.
    Sub A_Test()
    
      ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("A2:XFD2"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
            "GGG", DataOption:=xlSortNormal
        With ActiveSheet.Sort
            .SetRange ActiveSheet.UsedRange
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub

    This is the code that does not work.
    Sub A_Test()
    
    Answer = Application.InputBox("Please enter sort criteria", "Sort")
      ActiveSheet.Sort.SortFields.Clear
        ActiveSheet.Sort.SortFields.Add Key:=Range("A2:XFD2"), _
            SortOn:=xlSortOnValues, Order:=xlAscending, CustomOrder:= _
            Answer, DataOption:=xlSortNormal
        With ActiveSheet.Sort
            .SetRange ActiveSheet.UsedRange
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
    End Sub
    I want to sort all the columns based upon Row2 using a custom sort of "GGG". I know I can do this manually but this is one part of a much larger macro which I am putting together.
    Attached Files Attached Files
    Last edited by herbie226; 10-13-2012 at 08:11 PM.

  10. #10
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,327

    Re: Sort function does not correctly reference the answer from the input box.

    After several several attempts to find a solution I just could not. I placed a message with the other forum members, but not luck so far.

    Once you see this, if there still is not a hit you can bump again.

  11. #11
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: Sort function does not correctly reference the answer from the input box.

    perhaps
    Sub A_Test2()
        Dim Answer
        Answer = Application.InputBox("Please enter sort criteria", "Sort")
        Application.AddCustomList Array(Answer)
        With ActiveSheet.Sort
            With .SortFields
                .Clear
                .Add Key:=Range("A2:XFD2"), SortOn:=xlSortOnValues, Order:=xlAscending, _
                     CustomOrder:=Application.CustomListCount, DataOption:=xlSortNormal
            End With
            .SetRange ActiveSheet.UsedRange
            .Header = xlYes
            .MatchCase = False
            .Orientation = xlLeftToRight
            .SortMethod = xlPinYin
            .Apply
        End With
        Application.DeleteCustomList Application.CustomListCount
    End Sub
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  12. #12
    Registered User
    Join Date
    04-25-2012
    Location
    US
    MS-Off Ver
    Excel 2010
    Posts
    91

    Re: Sort function does not correctly reference the answer from the input box.

    Hello JosephP,

    Thank you for your assistance. Your suggestion works great. I appreciate all the help.

    Herbie226

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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