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

    • AzgarD
      By AzgarD
      Hi guys. I know this is a newbie topic, very newbie, but i've read a lot of stuff and still don't get it. I just need to copy something from Excel cell, paste this in other program, copy something in this program and paste in other Excel cell. Something like...
      Copy A2 Use some WindowActivate and MouseMove stuff and CTRL+C (not a problem) Go back to the Excel sheet Paste that content in C2 Then Copy A3 Use some WindowActivate and MouseMove stuff and CTRL+C (not a problem) Go back to the Excel sheet Paste that content in C3 ... And it goes on The problem is, how can i "communicate" with Excel and do this row change? Like A2 to C2 and A3 to C3 ... In a efficient way that can be done like hundreds of times.
      Very newbie question but still not understanding this.
       
      Ty guys.
    • Gowrisankar
      By Gowrisankar
      Dear members of the forum,
      I need to open excel files that may or may not need a password and finally move the files that needs password to manual queue.
      Is there a fastest way to do this?
       
      PS: I have a huge respect for the rules of this forum. I am not asking assistance to override any security measure. I just need to segregate the files that needs passwords.
    • MrCheese
      By MrCheese
      Hi guys,
      without including everything (unless you want it)
      I am copying data from a table in chrome and wanting to paste it into excel.
      Copying in Chrome works.
      I can paste it into the field i want by emulating goto -> ctrl V:
      WinActivate($dataload) WinWaitActive($dataload) Sleep(500) $oWorkbook1.Sheets("ItemReturn").Activate Sleep(500) $msg = "Measuring Sheet" conwrite() ttips2() Local Const $xlUp = -4162 With $oWorkbook1.ActiveSheet ; process active sheet $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) ; get a Range that contains the last used cells $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, "B")).Rows.Count ; get the the row count for the range starting in row/column 1 and ending at the last used row/column $iLastCell = .Cells($iRowCount + 1, "B").End($xlUp).Row ; start in the row following the last used row and move up to the first used cell in column "B" and grab this row number EndWith $NewStartCell = $iLastCell + 2 $msg = "moving to location" conwrite() ttips2() Sleep(250) Send("^g") WinWait("Go To") Sleep(100) Send("B" & $NewStartCell) Sleep(100) Send("{ENTER}") Sleep(500) Send("^v")  
      But, I want to use _excel_rangecopypaste, pasting from the clipboard
      _Excel_RangeCopyPaste($oWorkbook1.ActiveSheet, default, "B" & $NewStartCell,default,$xlPasteValuesAndNumberFormats) If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_RangeCopy Example 2", "Error pasting cells." & @CRLF & "@error = " & @error & ", @extended = " & @extended) however, this gives me error 4 , extended@:  -2147352567
      How can i fix this or find out how to debug this error?
       
      Thanks
    • Simpel
      By Simpel
      Hi.
      I try to figure out who is using a excel workbook which I can only open "read only". I use this code:
      #include <Array.au3> #include <Excel.au3> Local $sFile = ; excel file with path on a network drive Local $oExcel = _Excel_Open(True, True) Local $oTabelle = _Excel_BookOpen($oExcel, $sFile) Local $aUsers If IsObj($oTabelle) Then $aUsers = $oTabelle.UserStatus _ArrayDisplay($aUsers) EndIf If I am the one allowed to write to the excel file (I'm the first one who opened it) then I will get an array with myself:

      If my collegue opened the excel file first and I run the code I get the following error message:
      "H:\_Conrad lokal\Downloads\AutoIt3\_COX\Tests\test.au3" (9) : ==> The requested action with this object has failed.: $aUsers = $oTabelle.UserStatus $aUsers = $oTabelle^ ERROR The excel file is on a network drive. Is that's the problem?
      Regards, Conrad
    • robertocm
      By robertocm
      change linked image paths in excel 2007 Open XML Files with AutoIt and 7-zip:
      #include <File.au3> ;Change this Local $sFind = "C:\Users\MyUserName\Documents\MyImageFolder\My%20Image1.png" Local $sReplace = "C:\Users\ANOTHERUSERNAME\Documents\AnotherImageFolder\My%20Image1.png" Local Const $sMessage = "Directory to change excel image paths" Local $sFileSelectFolder = FileSelectFolder($sMessage, "") Local $sTempDir = @ScriptDir & "\testdir" ;Required 7-zip Local $PathZipProgram = @ProgramFilesDir & "\7-Zip\" If Not(FileExists($PathZipProgram & "\7z.exe")) Then MsgBox(16, "", "7z.exe not found in path " & $PathZipProgram) Exit EndIf ;look for excel files in selected directory and all subdirectories Local $SFileList = _FileListToArrayRec($sFileSelectFolder, "*.xls.;*.xlsm", $FLTAR_FILES, $FLTAR_RECUR, $FLTAR_NOSORT, $FLTAR_FULLPATH) If Not @error Then For $i = 1 To $SFileList[0] DirRemove($sTempDir, 1) ;use x command to keep the folder stucture, -aoa Overwrite All existing files without prompt, use -r to unzip the subfolders from the zip file RunWait('"' & $PathZipProgram & '7z.exe" x -aoa -r "' & $SFileList[$i] & '" -o"' & $sTempDir & '" -y', $PathZipProgram, @SW_HIDE) __ReplaceImagePaths($sTempDir, $sFind, $sReplace) RunWait('"' & $PathZipProgram & '7z.exe" a -r "' & $SFileList[$i] & '" "' & $sTempDir & '\*" -tzip -y', $PathZipProgram, @SW_HIDE) Next Else MsgBox(16, "Error", "No files were found in the folder specified.") EndIf DirRemove($sTempDir, 1) Func __ReplaceImagePaths($sTempDir, $sFind, $sReplace) ;List all files with .xml.rels extension in the directory \xl\drawings\_rels Local $aFileList = _FileListToArray($sTempDir & "\xl\drawings\_rels", "*.xml.rels", 1, True) If @error = 1 Then ;MsgBox (0, "", "Path was invalid") SplashTextOn("Title", "Path was invalid", -1, -1, -1, -1, 1, "", 24) Sleep(2000) SplashOff() Exit EndIf If @error = 4 Then ;MsgBox (0, "No files", "No files were found") SplashTextOn("Title", "No files were found", -1, -1, -1, -1, 1, "", 24) Sleep(2000) SplashOff() Exit EndIf Local $iRetval ;Loop through the array For $i = 1 To $aFileList[0] $iRetval = _ReplaceStringInFile($aFileList[$i], $sFind, $sReplace) Next EndFunc  
      Some references:
      https://stackoverflow.com/questions/37145369/change-path-to-picture-links-in-excel http://www.jkp-ads.com/Articles/Excel2007FileFormat.asp EDITED:
      Note: it seems that if User Account Control (UAC) is enabled then 7zip is unable to overwrite the destination file (using the same name).
      In this case, a possible solution would be to rename the original excel file before (see _PathSplit in help file).
      In my case i prefer just to disable UAC
×