Jump to content
YouriKamperman

Problem with Excel_RangeWrite

Recommended Posts

YouriKamperman

I am working on a script that will turn all file names in a directory into an array, and then writes this Array to an Excel file.

This in itself is working, but the RangeWrite function always puts the Array count in the first cell.

How can i make sure this does not happen? I can of course just filter it out in Excel, but i am trying to keep all logic of filtering text in my script.

This is what my script looks like:

Local $Yesterday = _DateAdd('d', -1, _NowCalcDate())
Local $cDate = StringReplace($Yesterday, "/", "-")

Local $aFileList = _FileListToArray(@WorkingDir & "/" & $cDate, "*")

Local $oExcel = _Excel_Open()
Local $oWorkbook = _Excel_BookNew($oExcel)

_Excel_RangeWrite($oWorkbook, $oWorkbook.Activesheet, $aFileList)

Share this post


Link to post
Share on other sites
Subz

Delete the count in the Array

_ArrayDelete($aFileList, 0)

 

  • Thanks 1

Share this post


Link to post
Share on other sites
YouriKamperman

Works like a charm!

Thanks a lot for the quick reply!

Share this post


Link to post
Share on other sites
stick3r
Posted (edited)

Not to start a new topic, I will ask here as my question is related with RangeWrite.

What I have for now:

Func _OpenDialog()

    $sFileOpenDialog = FileOpenDialog($sMessage, @WindowsDir & "\", "All (*.*)", $FD_FILEMUSTEXIST + $FD_MULTISELECT)

EndFunc   ;==>_OpenDialog

Func _ExcelOpen()

    $oExel = _Excel_Open()
    $oWorkbook = _Excel_BookOpen($oExel, $sFileOpenDialog)

EndFunc   ;==>_ExcelOpen


;================Get ID's from Excel=============================
Func _getID()

    Local $arrResult = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("A:A"), 2)
    For $i = 1 To UBound($arrResult) - 1
=======Irrelevant code running=======
        _makingActions()
    Next

EndFunc   ;==>_getID


;===================Changing Orders=================
Func _makingActions()
    For $i = 7 To 17
        Local $strK = _Extra_StringGet($oExtra, $i, 10, 1)

        If $strK = "K" Then
=======Irrelevant code running=======
            EndIf
        Else
            Return
        EndIf
    Next

EndFunc   ;==>_makingActions

I get ID's from Excel A column, save them to array and then do some things with them.

What I need to do is after completing with every ID, write something in L column.

If ID was from A2, I need to write something  to L2, then finish with ID from A3 and write something to L3.

How do I do that?

Edited by stick3r

Share this post


Link to post
Share on other sites
Subz

Do you mean you want to just write the modified array to L Column?

_Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet, $arrResult, "L1")

 

Share this post


Link to post
Share on other sites
stick3r

Not exactly. I have ID's, i.e. 5372615376, in $arrResult, then _makingActions() withing other program with these ID's.

When finished _makingActions(), I want to write Completed or Incompleted in L column. First ID is in A2 and there are a lot of them. So when finished _makingActions() with first ID, I want to write "Completed" or "Incompleted" in L2, depending on the result.

Share this post


Link to post
Share on other sites
Subz

So you mean you copy A2-A100 for example from the spreadsheet, you then want to loop through the array and do stuff and send a response back?  If so you can do it two ways you can use the index of the loop to write back to the spreadsheet, one at a time (using an offset e.g. if A2 = 1 in your array, then you would then write back to $index + 1) or expand your array to two columns and write the response back to the second column, once completed you can write the entire second column back to the Excel spreadsheet.  It really depends on your preference.  If you can provide an example spreadsheet and what results you expect then might be able to put something together.

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
      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!
×