Jump to content
Sign in to follow this  
ngskicker

Excel macro converted script running so slow

Recommended Posts

ngskicker

Hi, I convert some macro functions to autoit, this script is return last cell with value supporting hidden and autofiltered not contiguous rows,

This is the original script

Option Explicit
Public Function objLast_Cell(Optional ByRef objWorksheet As Worksheet = Nothing) As Range

' --------------------------------------------------------------------------------------------------------------
' [ http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_28187684.html ]
'
' Question Channel: Experts Exchange > Software > Office / Productivity > Office Suites > MS Office > MS Excel
'
' ID:               28187684
' Question Title:   (Excel) reliable way to find the last row with a value,
'                   supporting hidden and autofiltered not contiguous rows
' Question Asker:   lucavilla                                 [ http://www.experts-exchange.com/M_3713400.html ]
' Question Dated:   2013-07-17 at 21:38:39
'
' Expert Comment:   fanpages                                   [ http://www.experts-exchange.com/M_258171.html ]
' Copyright:        (c) 2013 Clearlogic Concepts (UK) Limited                           [ http://NigelLee.info ]
' --------------------------------------------------------------------------------------------------------------

  Dim intColumn                                         As Integer
  Dim intLoop                                           As Integer
  Dim lngLoop                                           As Long
  Dim lngRow                                            As Long

  On Error GoTo Err_objLast_Cell

  Set objLast_Cell = Nothing

  If (objWorksheet Is Nothing) Then
     Set objWorksheet = ActiveSheet
  End If

  intColumn = objWorksheet.UsedRange.Column - 1 + objWorksheet.UsedRange.Columns.Count
  lngRow = objWorksheet.UsedRange.Row - 1& + objWorksheet.UsedRange.Rows.Count

  For lngLoop = lngRow To 1& Step -1&
      If Application.CountA(objWorksheet.Rows(lngLoop)) > 0& Then
         Exit For
      End If ' If Application.CountA(objWorksheet.Rows(lngLoop)) > 0& Then
  Next lngLoop

  lngRow = lngLoop

  For intLoop = intColumn To 1 Step -1
      If Application.CountA(objWorksheet.Columns(intLoop)) > 0 Then
         Exit For
      End If ' If Application.CountA(objWorksheet.Columns(intLoop)) > 0 Then
  Next intLoop

  intColumn = intLoop

Exit_objLast_Cell:

  On Error Resume Next

  If lngRow > 0& And _
     lngRow <= objWorksheet.Rows.Count And _
     intColumn > 0& And _
     intColumn <= objWorksheet.Columns.Count Then
     Set objLast_Cell = objWorksheet.Cells(lngRow, intColumn)
  Else
     Set objLast_Cell = Nothing
  End If ' If lngRow > 0& And lngRow <= Rows.Count And intColumn > 0& And intColumn <= Columns.Count Then

  Exit Function

Err_objLast_Cell:

  On Error Resume Next

  lngRow = 0&
  intColumn = 0

  Resume Exit_objLast_Cell

End Function

and this my converted script

#include <Excel.au3>

$fod = FileOpenDialog("Choose excel file", @ScriptDir & "", "All excel files(*.xls;*.xlsx;*.xlsm)")

Local $oX = _ExcelBookOpen($fod)
Local $oLastCell = FindLastCell($oX)

; FindLastCell return cell as object
MsgBox(0, 0, "Last cell address is: " & $oLastCell.Address)

Func FindLastCell(ByRef $oExcel)
    If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
    Local $intColumn, $intLoop, $lngLoop, $lngRow, $objLast_Cell = ""

    $objWorksheet = $oExcel.ActiveSheet
    $intColumn    = $objWorksheet.UsedRange.Column - 1 + $objWorksheet.UsedRange.Columns.Count
    $lngRow       = $objWorksheet.UsedRange.Row - 1 + $objWorksheet.UsedRange.Rows.Count

    For $lngLoop = $lngRow To 1 Step -1
;~      ConsoleWrite("$lngLoop = " & $lngLoop)
        If $oExcel.Application.WorksheetFunction.CountA($objWorksheet.Rows($lngLoop)) > 0 Then ExitLoop
    Next

    $lngRow = $lngLoop

    For $intLoop = $intColumn To 1 Step -1
        If $oExcel.Application.WorksheetFunction.CountA($objWorksheet.Rows($intLoop)) > 0 Then ExitLoop
    Next

    $intColumn = $intLoop

    If $lngRow > 0 And _
        $lngRow <= $objWorksheet.Rows.Count And _
        $intColumn > 0 And _
        $intColumn <= $objWorksheet.Columns.Count Then
        $objLast_Cell = $objWorksheet.Cells($lngRow, $intColumn)
    EndIf

    Return $objLast_Cell
EndFunc 

runing macro on attached file only take a few seconds, but autoit script take more than one minute on attached script, maybe because autoit must send COM request to Excel at every loop, can someone give a workaround for this.

test.rar

Edited by ngskicker

Share this post


Link to post
Share on other sites
water

Maybe this saves a few cycles:

#include <Excel.au3>

$fod = FileOpenDialog("Choose excel file", @ScriptDir & "", "All excel files(*.xls;*.xlsx;*.xlsm)")

Local $oX = _ExcelBookOpen($fod)
Local $oLastCell = FindLastCell($oX)

; FindLastCell return cell as object
MsgBox(0, 0, "Last cell address is: " & $oLastCell.Address)

Func FindLastCell(ByRef $oExcel)
    If Not IsObj($oExcel) Then Return SetError(1, 0, 0)
    Local $intColumn, $intLoop, $lngLoop, $lngRow, $objLast_Cell = ""

    $objWorksheet = $oExcel.ActiveSheet
    With $objWorksheet
        $intColumn    = .UsedRange.Column - 1 + .UsedRange.Columns.Count
        $lngRow       = .UsedRange.Row    - 1 + .UsedRange.Rows.Count

        For $lngLoop = $lngRow To 1 Step -1
;~          ConsoleWrite("$lngLoop = " & $lngLoop)
            If $oExcel.Application.WorksheetFunction.CountA(.Rows($lngLoop)) > 0 Then ExitLoop
        Next

        $lngRow = $lngLoop

        For $intLoop = $intColumn To 1 Step -1
            If $oExcel.Application.WorksheetFunction.CountA(.Rows($intLoop)) > 0 Then ExitLoop
        Next

        $intColumn = $intLoop

        If $lngRow > 0 And _
            $lngRow <= .Rows.Count And _
            $intColumn > 0 And _
            $intColumn <= .Columns.Count Then
            $objLast_Cell = .Cells($lngRow, $intColumn)
        EndIf
    EndWith

    Return $objLast_Cell
EndFunc

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
ngskicker

thanks, but its still need same amount of time

Share this post


Link to post
Share on other sites
water

The AutoIt equivalent of "On Error GoTo Err_objLast_Cell" is missing. So the VB function ends as soon as an (expected) error occurs. The AutoIt function ignores errors and hence might do unnecessary processing.

What's the goal you try to achieve?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
ngskicker

This script return last cell with value supporting hidden and autofiltered not contiguous rows,

I can use

$lngRow = $oExcel.Cells.Find("*", $oExcel.Cells(1, 1), Default, Default, 1, 2).Row

to speed up the script,

but Cells.Find does not support auto filtered cell, so i can not use this,

and i do not know how to convert "On Error GoTo Err_objLast_Cell" so i just remove them

Edited by ngskicker

Share this post


Link to post
Share on other sites
water

Which version of Excel do you run?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
ngskicker

2013, but this should run on 2010 too.

Share this post


Link to post
Share on other sites
water

Can't test at the moment but the SpecialCells method might return what you need. Give XlCellType xlCellTypeLastCell a try.
The enumerations can be found here.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (2018-01-27 - Version 1.3.3.1) - 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
ngskicker

I already try

$sLastCell = $oExcel.Application.Selection.SpecialCells($xlCellTypeLastCell).Address(True, True, $xlR1C1)

$sLastCell = StringRegExp($sLastCell, "\A[^0-9]*(\d+)[^0-9]*(\d+)\Z", 3)
$lngRow = $sLastCell[0]
$intColumn = $sLastCell[1]

but thats still give the same result

Edited by ngskicker

Share this post


Link to post
Share on other sites
Imbuter2000

These methods fail:
- Usedrange fails because it includes unfilled cells with only changed borders
- Find... xlFormulas method fails because it doesn't include autofiltered rows
- xlUp method is not good because it acts only on a single specified column at a time
- xlDown method is not good because it doesn't pass through intermediate unfilled cells
- xlCellTypeLastCell has some of the above limitations (eg it doesn't include autofiltered rows)
- xlCellTypeVisible count doesn't support non-contiguous visible range of rows

The CountA method works well...   however, what about optimizing those two "step -1" loops changing them to dichotomic steps? (CountA works with range of rows too, not only with single rows)

Share this post


Link to post
Share on other sites
ngskicker

These methods fail:

- Usedrange fails because it includes unfilled cells with only changed borders

- Find... xlFormulas method fails because it doesn't include autofiltered rows

- xlUp method is not good because it acts only on a single specified column at a time

- xlDown method is not good because it doesn't pass through intermediate unfilled cells

- xlCellTypeLastCell has some of the above limitations (eg it doesn't include autofiltered rows)

- xlCellTypeVisible count doesn't support non-contiguous visible range of rows

The CountA method works well...   however, what about optimizing those two "step -1" loops changing them to dichotomic steps? (CountA works with range of rows too, not only with single rows)

Can you giva a sample how to do that

Share this post


Link to post
Share on other sites
soonyee91

and i do not know how to convert "On Error GoTo Err_objLast_Cell" so i just remove them

I think this line can be converted to autoit code. 

You may need to include this line before your objcreate code:

Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

Func _ErrFunc()
;put in the code you want to convert here
EndFunc   ;==>_ErrFunc

 Between, I tested both water and your code. Both scripts are running quite fast (within 1-2sec)

Edited by soonyee91

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
Sign in to follow this  

  • Similar Content

    • nooneclose
      By nooneclose
      My program has to first search for names in Column D that do not match up with column C. I got that search to work using arrays. It was slow and I could not figure out how to delete them so I just manually put coded the names that do not belong. I found their cell location but I do not know how to store that location and delete it.
      This is what I have so far.
      Local $NameToDelete1[6]  = _Excel_RangeFind($OpenWorkbook, "Smith, Bill") _ArrayDisplay($NameToDelete1, "Excel UDF: _Excel_RangeFind Example 1", "", 0, "|", "Sheet|Name|Cell|Value|Formula|Comment") _Excel_RangeDelete($OpenWorkbook.ActiveSheet, $NameToDelete1[2], $xlShiftUp)  
      Please help, I wanted to have this program done yesterday but I did not see this problem until yesterday. 
    • nooneclose
      By nooneclose
      I need to perform a subtotal in excel and I would like to automate this process using Autoit if possible like always any and all help will be greatly appreciated. 
      I can not find a good example but the two from Microsoft. Here is one of the two from msdn.microsoft.com/en-us/vba/excel-vba/articles/range-subtotal-method-excel
      I do not really understand how to translate this into AutoIt, but I gave it a try and here is what I have.
      $OpenRange      = "A1:E200" $xlSum          = -4157 $Added_Array[2] = [2, 3] $OpenRange.Subtotal("B1", $xlSum, $Added_Array, True, False, True) I just need to perform a subtotal on a range based on a header called department, and then perform a sum on the results.
    • nooneclose
      By nooneclose
      How to use _Excel_RangeSort to sort my excel file by three different headers Column A1, B1, and C1 have headers on which I want to sort by. The headers on which I want to sort are department, employee type, and name.
      I still really new to AutoIt so I do not actually know how to properly start this line or lines of code, to be honest. The example code is the best I can do.
      _Excel_RangeSort($OpenWorkbook, Default, "A1:C1", "1:1", $xlDescending, Default, $xlYes, Default, $xlSortRows) I just need to sort by those three headers in that order of department, employee type, and name, plus in descending order.
       
      any and all help would be greatly appreciated.  Thank you!
    • Daniza
      By Daniza
      It works fine on my Excel 2007, but after I Emailed My Area Manager he told me after he Enable Macro Security nothing happen's, can someone test this to run on Excel 2016? My AM can't provide me more information 

      Thank You in Advance,
      Please see attachment.
      <snip>
    • tuffgong
      By tuffgong
      Good morning. I have a system I am trying to automate that works like this: user fills a column in an Excel spreadsheet with values they would like printed and saves it to a folder on their desktop, they start the script and it formats their data into a text file (adding a prefix) and sends the text file as a .bch file where it needs to go. This is working:
      #include <Array.au3> #include <Excel.au3> #include <File.au3> #include <MsgBoxConstants.au3> Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) barcodePrint() Func barcodePrint() $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) FileOpen($sFilename, $FO_OVERWRITE) ;Global $oWorkbook = _Excel_BookAttach($oExcel) Global $sSTCArray = _Excel_RangeRead($Read) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, "!StaticShelving1x3_ZPL," & $sSTCArray[$i]) Next _Excel_Close($oExcel) FileMove($sFilename, "***file path***\print.bch") EndFunc However, it only works if the user first formats the spreadsheet to text. I want to automate that. From what I have read it appears AutoIt does not like formatting cells that already have values. True? Can I pull the values from an un-formatted (default GENERAL format) spreadsheet and go straight to my text file? I have also considered opening a second spreadsheet, formatting it, and copying the values over. Like this:
        
      Global $sPrefix Global $oPath Global $sSTCArray Global $sFilename = @DesktopDir & "\Labels\print.txt" Global $sWorkbook = @DesktopDir & "\Labels\Labels.xlsx" Global $oExcel = _Excel_Open(False,False,False,False,True) Global $aArray Global $bExcel _Excel_BookNew($bExcel) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 $aArray = IniReadSection("***File path***\barcode.ini", "stc/rvt/mgm") $sPrefix = $aArray[1][1] $oPath = $aArray[2][1] barcodePrint() EndSwitch WEnd Func barcodePrint() $oWorkbook = _Excel_BookOpen($bExcel, @DesktopDir & "\Labels\print.xlsx") $oWorkbook.ActiveSheet.Columns("A").NumberFormat = "@" Local $Read = _Excel_BookOpen($oExcel, $sWorkbook, True, False, Default, Default, Default) $oCopy = _Excel_RangeRead($sWorkbook) _Excel_RangeWrite($oWorkbook, Default, $oCopy) FileOpen($sFilename, $FO_OVERWRITE) $sSTCArray = _Excel_RangeRead($Read,"Default","Default",3) ;_ArrayDisplay($sSTCArray) For $i = 0 to UBound($sSTCArray, 1) - 1 FileWriteLine($sFilename, $sPrefix & $sSTCArray[$i]) Next _Excel_Close($oExcel) ;FileMove($sFilename, $oPath) Exit EndFunc This does not like the formatting of $oWorkbook: "Variable must be of type 'Object'".  Do I need this second sheet? If so, how can I format it? Is there a better way to get the Excel values into a .txt file? Any ideas would be appreciated. Thanks!
×