Jump to content
LoneWolf_2106

Error calculating the last row in excel

Recommended Posts

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.

Share this post


Link to post
Share on other sites
water

Can you please post the full code? Dot notation only works in a With block. 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
LoneWolf_2106
#include <MsgBoxConstants.au3>
#include <Array.au3>
#include <File.au3>
#include <Excel.au3>
#include <WinAPIShellEx.au3>
#include <Excel_Funktion_test.au3>

; Script Start 

Local Const $sMessage = "Select a folder"

    ; Display an open dialog to select a file.
Global $sFileSelectFolder = FileSelectFolder($sMessage, "")
    If @error Then
        ; Display the error message.
        MsgBox($MB_SYSTEMMODAL, "", "No folder was selected.")
    Else
        ; Display the selected folder.
        MsgBox($MB_SYSTEMMODAL, "", "You chose the following folder:" & @CRLF & $sFileSelectFolder)
    EndIf

Global $Cloud_Folder=""

FolderWrite()
Sleep(1000)

Global $aFileList = _FileListToArray($sFileSelectFolder, Default, Default, True)

$Output_Index = _ArraySearch($aFileList, "Output_Cloud",0,0,0,1)


_ArrayDelete($aFileList, $Output_Index)

FolderScan()

$Path_Len = StringLen($sFileSelectFolder)
_ArrayColInsert($aFileList, 1)



For $i=1 To UBound($aFileList) - 1
    $aFileList[$i][1] = StringTrimLeft($aFileList[$i][0], $Path_Len+1)
Next



_ArrayColInsert($aFileList, 2)


For $i = 1 To UBound($aFileList) - 1

    $split_array=StringSplit($aFileList[$i][1], "_", 2 )

    $aFileList[$i][2] = $split_array[0]
Next






For $i=1 To UBound($aFileList) - 1
    $Excel_File_1 = $aFileList[$i][0]
    Local $oExcel_1 = _Excel_Open()
    Local $vZeile_2 =""
    If @error Then Exit MsgBox(16, "Excel UDF: _Excel_BookOpen Example", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    Local $sWorkbook_1 = $Excel_File_1
    Local $oWorkbook_1 = _Excel_BookOpen($oExcel_1, $sWorkbook_1)
    Sleep(1000)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookOpen Example 1", "Error opening '" & $sWorkbook_1 & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
;   MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookOpen Example 1", "Workbook '" & $sWorkbook_1 & "' has been opened successfully." & @CRLF & @CRLF & "Creation Date: " & $oWorkbook_1.BuiltinDocumentProperties("Creation Date").Value)
    With $oWorkbook_1.ActiveSheet ; process active sheet

        $vZeile_2=_Excel_RangeRead($oWorkbook_1, Default, "A2")

        $vRange=$oWorkBook_1.Activesheet.Range("A2").EntireRow

        If $vZeile_2 = "" Then
            _Excel_RangeDelete($oWorkbook_1.ActiveSheet, $vRange, $xlShiftUp, 1)
        EndIf

        Sleep(1000)

        $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)
    EndWith
    ;_Excel_RangeWrite($oWorkbook_1, $oWorkbook_1.ActiveSheet,$aFileList[$i][2] , "AB3:AB"&$iRowCount)
    Sleep(1000)
    _Excel_BookSaveAs($oWorkbook_1, $Cloud_Folder&"\"&$aFileList[$i][1], $xlWorkbookDefault, true)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookSaveAs Example 1", "Error saving workbook to '" & $sWorkbook_1 & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
;MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookSaveAs Example 1", "Workbook successfully saved as '" & $sWorkbook_1 & "'.")
ShellExecute($sWorkbook_1)
Sleep(1000)
_Excel_Close($oExcel_1,False, True)
if @error then MsgBox($MB_OK, "error", @error)
ProcessClose("EXCEL.exe")
Sleep(1000)
Next

Sleep(1000)



Global $aFileList_Modified = _FileListToArray($Cloud_Folder, Default, Default, True)

;_ArrayDisplay($aFileList_Modified)


For $j=1 To UBound($aFileList_Modified) - 1

    ;MsgBox($MB_OK, "test", $aFileList_Modified[$j])

    SpalteAA($aFileList_Modified[$j])
    if @error then MsgBox($MB_OK, "error", @error)
    Sleep(1000)
    ProcessClose("EXCEL.exe")
    Sleep(1000)
Next

MsgBox($MB_OK, "Info", "Alle Dateien wurden bearbeitet")


Func FileSearch()

  ; Assign a Local variable the search handle of all files in the current directory.
    Local $hSearch = FileFindFirstFile("XYZ.xlsx")

    ; Check if the search was successful, if not display a message and return False.
    If $hSearch = -1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Error: No files/directories matched the search pattern.")
        Exit
    Else

        Return True
    EndIf

    ; Close the search handle.
    FileClose($hSearch)
EndFunc

Func FolderScan()
    ; List all the files and folders in the desktop directory using the default parameters and return the full path.
    If @error = 1 Then
        MsgBox($MB_SYSTEMMODAL, "", "Path was invalid.")
        Exit
    EndIf
    If @error = 4 Then
        MsgBox($MB_SYSTEMMODAL, "", "No file(s) were found.")
        Exit
    EndIf
    ; Display the results returned by _FileListToArray.
    ;_ArrayDisplay($aFileList, "$aFileList")
EndFunc

Func FolderWrite()

$Cloud_Folder=$sFileSelectFolder&"\Output_Cloud"

$Folder_exists=FileExists($Cloud_Folder)

If $Folder_exists=0 Then

    _WinAPI_ShellCreateDirectory ( $Cloud_Folder)

EndIf

EndFunc

I hope that this is enough :) If you need also the second part(which has no influence on the main one), let me know.

Thanks in advance for your support.

Share this post


Link to post
Share on other sites
water

Will check on Friday as I‘m on vacation right now. 

  • Like 1

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
LoneWolf_2106

Enjoy your vacation :-)

Edited by LoneWolf_2106

Share this post


Link to post
Share on other sites
LoneWolf_2106

I have noticed that the issue occurs only with the 32 bit Version. The issue is not reproducible in the 64 bit Version.

I have 64 bit OS and Excel is 32 bit.

Share this post


Link to post
Share on other sites
water

I noticed you use your own Excel include file. 
Do you still get the error when using the default Excel UDF that comes with AutoIt?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
Faalamva

Hi,

This code will give you the last row of column 1, and the last column on row 1 of your Excel file :

$MaxRow = $oWorkbook.Sheets($SheetName).Range("A99999").End(-4162).Row
$MaxCol = $oWorkbook.Sheets($SheetName).Range("ZZ1").End(-4159).Column

-4162 is the internal Excel code for "XlUp" and -4159 the one for "XlToLeft".

Make sure to adapt the 99999 and ZZ if necessary (i.e. if not high enough).

Share this post


Link to post
Share on other sites
JLogan3o13

@LoneWolf_2106 when you say last row I am assuming you mean last used row, correct? If so, there are a couple of ways you can do it.

   Something like this is you want all of the data in an array, for later manipulation:

#include <Array.au3>
#include <Excel.au3>

Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, @DesktopDir & "\defects.xlsx")
Local $aRange = _Excel_RangeRead($oWorkbook, Default, $oExcel.ActiveSheet.UsedRange)

_Excel_BookClose($oWorkbook)
_Excel_Close($oExcel)

    $iLastRow = (UBound($aRange))
    ConsoleWrite($iLastRow & @CRLF)

   Or something like this is you purely want to know the row count:

#include <Array.au3>
#include <Excel.au3>

Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookOpen($oExcel, @DesktopDir & "\defects.xlsx")
Local $iLastRow = $oExcel.ActiveSheet.UsedRange.Rows.Count

_Excel_BookClose($oWorkbook)
_Excel_Close($oExcel)

    ConsoleWrite($iLastRow & @CRLF)

 

  • Like 1

√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites
Juvigy
On 11/16/2017 at 1:56 PM, LoneWolf_2106 said:

I have noticed that the issue occurs only with the 32 bit Version. The issue is not reproducible in the 64 bit Version.

I have 64 bit OS and Excel is 32 bit.

I think the issue comes from here. Why dont you compile a 32bit script and then a 64bit and test it?

Share this post


Link to post
Share on other sites
LoneWolf_2106

@Juvigy I have compiled 2 Versions, i have tested both for 3 hours. The 32 bit one crashes sometimes.

I would like just to find out the reason, in order to avoid the same issue in the future.

Share this post


Link to post
Share on other sites
water

You have to compile the AutoIt script for the bitness of Excel.
Means:
If Excel is 32 bit then you need to compile your AutoIt script for 32 bit.
If Excel is 64 bit then you need to compile your AutoIt script for 64 bit.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

You open the Excel application over and over again in your Loop. I have cleaned up your loop a bit:

Local $vZeile_2, $sWorkbook_1, $oWorkbook_1
Local $oExcel_1 = _Excel_Open()
If @error Then Exit MsgBox(16, "My Script", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
For $i = 1 To UBound($aFileList) - 1
    $Excel_File_1 = $aFileList[$i][0]
    $vZeile_2 = ""
    $sWorkbook_1 = $Excel_File_1
    $oWorkbook_1 = _Excel_BookOpen($oExcel_1, $sWorkbook_1)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Excel UDF: _Excel_BookOpen Example 1", "Error opening '" & $sWorkbook_1 & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    With $oWorkbook_1.ActiveSheet ; process active sheet
        $vZeile_2 = _Excel_RangeRead($oWorkbook_1, Default, "A2")
        $vRange = $oWorkbook_1.Activesheet.Range("A2").EntireRow
        If $vZeile_2 = "" Then _Excel_RangeDelete($oWorkbook_1.ActiveSheet, $vRange, $xlShiftUp, 1)
        $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell)
        $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count
        _Excel_RangeWrite($oWorkbook_1, $oWorkbook_1.ActiveSheet, $aFileList[$i][2], "AB3:AB" & $iRowCount)
    EndWith
    _Excel_BookSaveAs($oWorkbook_1, $Cloud_Folder & "\" & $aFileList[$i][1], $xlWorkbookDefault, True)
    If @error Then Exit MsgBox($MB_SYSTEMMODAL, "My Script", "Error saving workbook to '" & $Cloud_Folder & "\" & $aFileList[$i][1] & "'." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    ; ShellExecute($sWorkbook_1)
Next
_Excel_Close($oExcel_1, False, True)
If @error Then MsgBox($MB_OK, "error", @error)

 

Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
water

Another question:
What is

ShellExecute($sWorkbook_1)

for?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (2018-06-01 - Version 1.4.9.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2018-09-01 - Version 1.3.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
PowerPoint (2017-06-06 - Version 0.0.5.0) - Download - General Help & Support
Excel - Example Scripts - Wiki
Word - Wiki
 
Tutorials:

ADO - Wiki

 

Share this post


Link to post
Share on other sites
LoneWolf_2106

@water, thanks for your support. Please ignore that line, i have removed it from the code, i don't even remember what test i have made in August :-)

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

    • nooneclose
      By nooneclose
      Hey. I'm working on a new project and was wondering if there is a better way to "update" my Column E array. 
      Here is my code: 
      Local $nI  = 0                                                            ;Creates a name index of 0: nI = Name index Local $nII = 1                                                            ;Creates a name index of 1 for second loop: nII = Name Index 2 For    $iN = 0 To $IndexRows Step 1                                       ;Checks the roster for any names that appear twice      For $iN2 = 0 To $IndexRows Step 1          if $d_Names[$nI] == $d_Names[$nII] And $d_Names[$nII] <> "" Then              Local $timeSheetName = _ArraySearch($e_Names, $d_Names[$nI], 0, 0, 0, 0, 1)              ;MsgBox($MB_SYSTEMMODAL, "Found it", $d_Names[$nI] & " In column E on Row " & $timeSheetName)              Local $eI  = $timeSheetName + 1              ;ConsoleWrite($timeSheetName & @CRLF)              ;ConsoleWrite($eI & @CRLF)              ;ConsoleWrite(@CRLF)              _Excel_RangeInsert($OpenWorkbook.ActiveSheet, "E" & $eI & ":F" & $eI, $xlShiftDown)                                                                          ;Inserts a empty cell in columns E and F.              _Excel_RangeWrite($OpenWorkbook, $OpenWorkbook.ActiveSheet, $d_Names[$nII], "E" & $eI)                                                                         ;Fills the empty cell in columns E with the doubled name              $aArray_Index = 2                                           ;Array element counter              For $Index = 2 To $IndexRows Step 1                        ;Loops through every row in the Excel file unto no rows are found or a null row is found                  $Array_Value_E = _Excel_RangeRead($OpenWorkbook, Default, "E"&$Index)                  $e_names[$aArray_Index] = $Array_Value_E                ;While the code loops every value in column E is stored in the E array (updating the array)                  $aArray_Index += 1              Next              ExitLoop          EndIf      Next      $nI  += 1      $nII += 1 Next Basically, It checks a roster for people whose name appears twice then inserts a new "row" for that person because they work in two different departments.
      I have to find that name however in Column E if two appear in column D. My code works but I think it is not as efficient as it could be. 
      Any ideas on how to improve the "update" for my array?
      (once it finds the double names in Column D it then searches for that name by going name by name in the Column E array and once it finds it inserts a new row. However, the E array doesn't have that new row stored in it so I have to "update" the array to properly find the next name)
      Any and all tips would be greatly appreciated. 
       
      NOTE: Just assume I'm opening the excel file properly please do not add that code in, it only complicates your answer. 
    • smud
      By smud
      Currently, I'm working on a program that will display Dialog boxes with either Yes or No.
      For each dialog, I reward the user with X amount of Credits.
      I'm hoping to output the amount of credits to a cell in a column (there will be 20 different columns).
      It will only post to a row that is equal to today's date (first column). If no row exists yet with the current date, it will start a new row.
      Any suggestions?
      Thank you
    • nooneclose
      By nooneclose
      How do I properly convert this to Autoit? This is a VBA macro that I recorded in Excel.
       ActiveSheet.Outline.ShowLevels RowLevels:=2 I need this to close my subtotal once it is finished. 
      any help will be greatly appreciated. 
    • Skeletor
      By Skeletor
      Hi All,
      While creating a few excel spreadsheets using AutoIt, I came across something which to my limiting time to research the forums I don't anyone has mentioned. 
      The color pallettes are reversed. 
      Huge shock to me.
       
      I wanted to produce a red row but kept on getting blue. 
      Seems like 0xFF0000 was red on the charts but when running the script, I got blue. 
      I then played around with the colors, and after a few tries, I finally got Red. 
      Reversed the FF0000 and the result is 0000FF.
       
      So for Excel compared to Html
      0000FF (Red) - Excel
      0000FF (Blue) - Html

      FFFF00 (Cyan) - Excel
      FFFF00(Yellow) - Html
       
    • Jemboy
      By Jemboy
      Yesterday I had to make some little changes to an old Autoit program we use at my work.
      The program reads some data and convert it to Excel.
      Before writing the cell, it is changed to text and later on I slap the column with an autofitwidth.
      Furthermore weI execute a conditional format on the sheet, to make the data more readable.
      I quickly found out that because of the breaking changes Excel.udf had starting from AutoIt 3.3.12.0,
      a lot of things had to been changed.
      The changes I had to do, only took 10 minutes.
      After trying to adjust the script for over 5 hours, to get it working with the new Excel.udf, I gave up.
      I stopped changing the script, uninstalled the my Autoit and went looking for an older version.
      Luckilly I was able to find Autoit v3.3.8.1 (with corresponding Scite) in my software repository.
      Installing Autoit V3.3.8.1 and compiling the file, now took me  10 minutes .
       
      So why did I not get the old script working with the new Excel.udf?
      There are several reasons I failed getting the old script working with the new Excel UDF.
       I had some pressure from management to fix it ASAP (and got a little anxious)  Most all resources on the internet point to the working of the old EXcel.UDF And offcourse there were thosing "breaking changes",
      with new functions using diffrent parameters or using parameters in different order. One of the column's on the sheet is used to store EAN13 (barcode) and was formatted like 1,23E12.
      I couldn't change the cell to text, also autofitwidt was not working and using conditional formatting was also a no-no.
      So in the end I could use the new Excel UDF, but not desapointed management.
       
      What would I like to ask?
      I understand that sometimes you want to rewrite a program to make it better. I even understand that one has to make breaking changes sometimes.
      But in this case because of lacking examples/resources my day went completely down the drain.
      I would like to ask the Excel.udf developpers to:
      Make more functions available to do things like changing cell properties easily, changing cell color, do an autofit columnwidth, format data conditionally. 
        Or write an Example using the (new) Excel UDF, making examples how to format a cell, do conditional format, changing cell colors etc.
        I probably am more of an example guy.
      Having a good Excel.UDF Example showing a lot of common things normally makes, programming things easy for me.
      Because I can keep tweaking snippets until I get it working the way I want it.
       
      So dear developpers, could you help me and other future user out?
       
       
       
       
       
       
       
       
       
       
       
       
       
       
×