Jump to content

Excel macro converted script running so slow


ngskicker
 Share

Recommended Posts

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
Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

Which version of Excel do you run?

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 2 weeks later...

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
Link to comment
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
 Share

×
×
  • Create New...