Wolfiesaxah

Excel Sort Method Attempt

2 posts in this topic

Hi guys, I am using an old version of Excel.au3 which I am not ready to update yet so please help me. The old Excel.au3 does not have any method for SORT. I attempted to create one since i need one but I am getting an error. Here's my sample code:

$oAppl.Activesheet.UsedRange.sort($oAppl.Activesheet.cells(1,5),1,1)

I thought this would work but I get the  

: ==> The requested action with this object has failed.:
$oAppl.Activesheet.UsedRange.sort($oAppl.Activesheet.cells(1,$BidDayColNumber),1,1)
$oAppl.Activesheet.UsedRange.sort($oAppl.Activesheet.cells(1,$BidDayColNumber),1,1)^ ERROR

Any ideas or alternatives on sorting a range without calling from Excel.au3?

Share this post


Link to post
Share on other sites



The function that comes with the new Excel UDF lookcs like this. Docu can be found in the online help file.

Func _Excel_RangeSort($oWorkbook, $vWorksheet, $vRange, $vKey1, $iOrder1 = Default, $iSortText = Default, $iHeader = Default, _
        $bMatchcase = Default, $iOrientation = Default, $vKey2 = Default, $iOrder2 = Default, $vKey3 = Default, $iOrder3 = Default)
    If Not IsObj($oWorkbook) Or ObjName($oWorkbook, 1) <> "_Workbook" Then Return SetError(1, 0, 0)
    If Not IsObj($vWorksheet) Then
        If $vWorksheet = Default Then
            $vWorksheet = $oWorkbook.ActiveSheet
        Else
            $vWorksheet = $oWorkbook.WorkSheets.Item($vWorksheet)
        EndIf
        If @error Or Not IsObj($vWorksheet) Then Return SetError(2, @error, 0)
    ElseIf ObjName($vWorksheet, 1) <> "_Worksheet" Then
        Return SetError(2, @error, 0)
    EndIf
    If $vRange = Default Then
        $vRange = $vWorksheet.Usedrange
    ElseIf Not IsObj($vRange) Then
        $vRange = $vWorksheet.Range($vRange)
        If @error Or Not IsObj($vRange) Then Return SetError(3, @error, 0)
    EndIf
    $vKey1 = $vWorksheet.Range($vKey1)
    If @error Or Not IsObj($vKey1) Then Return SetError(4, @error, 0)
    If $vKey2 <> Default Then
        $vKey2 = $vWorksheet.Range($vKey2)
        If @error Or Not IsObj($vKey2) Then Return SetError(5, @error, 0)
    EndIf
    If $vKey3 <> Default Then
        $vKey3 = $vWorksheet.Range($vKey3)
        If @error Or Not IsObj($vKey3) Then Return SetError(6, @error, 0)
    EndIf
    If $iHeader = Default Then $iHeader = $xlNo
    If $bMatchcase = Default Then $bMatchcase = False
    If $iOrientation = Default Then $iOrientation = $xlSortColumns
    If $iOrder1 = Default Then $iOrder1 = $xlAscending
    If $iSortText = Default Then $iSortText = $xlSortNormal
    If $iOrder2 = Default Then $iOrder2 = $xlAscending
    If $iOrder3 = Default Then $iOrder3 = $xlAscending
    If Int($oWorkbook.Parent.Version) < 112 Then ; Use Sort method for Excel 2003 and older
        $vRange.Sort($vKey1, $iOrder1, $vKey2, Default, $iOrder2, $vKey3, $iOrder3, $iHeader, Default, $bMatchcase, $iOrientation, Default, $iSortText, $iSortText, $iSortText)
    Else
        ; http://www.autoitscript.com/forum/topic/136672-excel-multiple-column-sort/?hl=%2Bexcel+%2Bsort+%2Bcolumns#entry956163
        ; http://msdn.microsoft.com/en-us/library/ff839572(v=office.14).aspx
        $vWorksheet.Sort.SortFields.Clear
        $vWorksheet.Sort.SortFields.Add($vKey1, $xlSortOnValues, $iOrder1)
        If $vKey2 <> Default Then $vWorksheet.Sort.SortFields.Add($vKey2, $xlSortOnValues, $iOrder2)
        If $vKey3 <> Default Then $vWorksheet.Sort.SortFields.Add($vKey3, $xlSortOnValues, $iOrder3)
        $vWorksheet.Sort.SetRange($vRange)
        $vWorksheet.Sort.Header = $iHeader
        $vWorksheet.Sort.MatchCase = $bMatchcase
        $vWorksheet.Sort.Orientation = $iOrientation
        $vWorksheet.Sort.Apply
    EndIf
    If @error Then Return SetError(7, @error, 0)
    Return $vRange
EndFunc   ;==>_Excel_RangeSort

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i have an error:
      ==> The requested action with this object has failed.: $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count $iRowCount = .Range(^ ERROR  
      My code is:
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) Sleep(1000) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count Sleep(1000) _Excel_RangeWrite($oWorkbook_1, $oWorkbook_1.ActiveSheet,$aFileList[$i][2] , "AB3:AB"&$iRowCount) I have added some sleep because the application was crashing more often before, so i thought to slow down the code execution.
      But i didn't solve the issue.
      Has anyone an idea of what the problem might be?
      Thanks in advance.
    • water
      By water
      Extensive library to control and manipulate Microsoft Excel charts.
      Written by GreenCan and water.
      Theads: General Help & Support - Example Scripts
      BTW: If you like this UDF please click the "I like this" button. This tells me where to next put my development effort

      KNOWN BUGS (last changed: 2017-07-21)
      None. The COM error handling related bugs have been fixed.
       
    • LoneWolf_2106
      By LoneWolf_2106
      Hi,
      i have a problem with the deletion of an empty row in Excel.
      My code:
       
      If $vRow_2 = "" Then _Excel_RangeDelete($oWorkbook_1.ActiveSheet,"2", $xlShiftUp,1) EndIf I want to delete the second row. $vRow_2 is an empty cell, "A2".
      After running the code, the second row is not deleted.
      I have tried also:
       
      If $vRow_2 = Null Then _Excel_RangeDelete($oWorkbook_1.ActiveSheet,"2", $xlShiftUp,1) EndIf But it doesn't work.
      Any suggestion?
      Thanks in advance.
    • VeryGut
      By VeryGut
      I'm trying to insert the following formula in cell A2 using my script:
      =if(A1=""; "YES"; "NO")
      To my understanding, the line of code should be similar to this:
      _Excel_RangeWrite($MasterFile, Default, "=if(A1=""; "YES"; "NO")", "A2")
      However, it does not work, probably due to the multiple quotation marks that confuse the script :C
      How do I avoid this problem?
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a question about Excel, i have to create several charts one below the other dynamically.
      I have thought to use:
       
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count  
      And then to use it in this way:
      $Graph_position = "=Test1!A"&$iRowCount+2&":K"&$iRowCount+24 But it doesn't work with charts.
      Does anyone have a suggestion?