Jump to content

Recommended Posts

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

opt("WinTitleMatchMode", 2)
$path = "C:\Users\User\Desktop\Nezavisimai\Papka2\"
$FileListShort = _FileListToArray($path, "*.xlsx", 1, True)
$ProgExcel = _Excel_Open()
$BookDestiny = _Excel_BookNew($ProgExcel)

Global $arr = []

$LineDestiny = 1
_Excel_RangeWrite($BookDestiny, $BookDestiny.Activesheet, "Weather", "A" & $LineDestiny)
_Excel_RangeWrite($BookDestiny, $BookDestiny.Activesheet, "Temp", "B" & $LineDestiny)

For $i = 1 to $FileListShort[0]

   $BookSource =  _Excel_BookOpen($ProgExcel, $FileListShort[$i], False, False)
   $aArray = _Excel_RangeRead($BookSource, $BookSource.Activesheet, "A2:B4")

   _ArrayAdd($arr, $aArray)

   _Excel_BookClose($BookSource, False)
Next


   Global $numRows = UBound($arr[0], 1)
   Global $numCols = UBound($arr[0], 2)

   Global $fArray = []
   ReDim $fArray [0 To ($numRows * (Ubound($arr) - 1))] [0 to $numCols]

   rT = 0
   For $p = 0 To UBound($arr) - 1
      For r = 0 To $numRows
         rT = rT + 1
         For c = 0 To $numCols
            $fArray[rT][c] = $p[r][c]
         Next c
      Next r
   Next $p

   _Excel_RangeWrite($BookDestiny, $BookDestiny.Activesheet, $fArray, "A2")

Hello, what is Autoit equivalent for VBA Collection object?

Dim CollArrays As New Collection 'VBA Collection

How can create collection of 2D arrays with Autoit?

This code write error.

Posted

What do you mean by "Write Error"? Can you please post the error message you get?

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

BTW: You try to use a lot of "variables" without the leading $; rt, r, c.
"Next $p" is not a valid statement. Should be "Next"

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 12/22/2021 at 3:26 PM, water said:

BTW: You try to use a lot of "variables" without the leading $; rt, r, c.
"Next $p" is not a valid statement. Should be "Next"

Expand  
$rT = 0
   For $p = 0 To (UBound($arr) - 1)
      For $r = 0 To $numRows
         $rT = $rT + 1
         For $c = 0 To $numCols
            $fArray[$rT][$c] = $p[$r][$c]
         Next
      Next
   Next

Error.

Line 4397 (File"C:\Users\Users\Desktop\Test2.exe"):

Error: Array variable has incorrect number of subscript or subscript dimension range exceeded.

Posted
  On 12/22/2021 at 12:48 PM, Spec86 said:

How can create collection of 2D arrays with Autoit?

Expand  

Here's a conceptual example of how to create a collection (array) of 2D arrays:

#include <Array.au3>

array_of_2d_arrays_example()

Func array_of_2d_arrays_example()
    Const $2D_ARRAY_1 = [["A1 Row 0/Col 0", "A1 Row 0/Col 1"], ["A1 Row 1/Col 0", "A1 Row 1/Col 1"]], _
          $2D_ARRAY_2 = [["A2 Row 0/Col 0", "A2 Row 0/Col 1"], ["A2 Row 1/Col 0", "A2 Row 1/Col 1"]]

    Local $aArrayOf2dArrays[0]

    ;Display 2d array 1 & 2
    _ArrayDisplay($2D_ARRAY_1, "$2D_ARRAY_1")
    _ArrayDisplay($2D_ARRAY_2, "$2D_ARRAY_2")

    ;Add 2d array 1 & 2 to the array of 2d arrays
    _ArrayAdd($aArrayOf2dArrays, $2D_ARRAY_1, 0, "", "", $ARRAYFILL_FORCE_SINGLEITEM)
    _ArrayAdd($aArrayOf2dArrays, $2D_ARRAY_2, 0, "", "", $ARRAYFILL_FORCE_SINGLEITEM)

    ;Display array of 2d arrays
    _ArrayDisplay($aArrayOf2dArrays, "$aArrayOf2dArrays")

    ;Display the rows of array of 2d arrays
    _ArrayDisplay($aArrayOf2dArrays[0], "Row 0 of $aArrayOf2dArrays")
    _ArrayDisplay($aArrayOf2dArrays[1], "Row 1 of $aArrayOf2dArrays")
EndFunc

 

Posted

Can you please tell us what you try to do with your script?
Do you need to create the array at all?
 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted (edited)
  On 12/22/2021 at 3:53 PM, Jos said:

$p is a number not an Array ...right?

Expand  

Yes, VBA has "For Each". Autoit has "For". I cannot use "For Each" in Autoit.

Edited by Spec86
Posted
  On 12/22/2021 at 4:00 PM, TheXman said:

Here's a conceptual example of how to create a collection (array) of 2D arrays:

#include <Array.au3>

array_of_2d_arrays_example()

Func array_of_2d_arrays_example()
    Const $2D_ARRAY_1 = [["A1 Row 0/Col 0", "A1 Row 0/Col 1"], ["A1 Row 1/Col 0", "A1 Row 1/Col 1"]], _
          $2D_ARRAY_2 = [["A2 Row 0/Col 0", "A2 Row 0/Col 1"], ["A2 Row 1/Col 0", "A2 Row 1/Col 1"]]

    Local $aArrayOf2dArrays[0]

    ;Display 2d array 1 & 2
    _ArrayDisplay($2D_ARRAY_1, "$2D_ARRAY_1")
    _ArrayDisplay($2D_ARRAY_2, "$2D_ARRAY_2")

    ;Add 2d array 1 & 2 to the array of 2d arrays
    _ArrayAdd($aArrayOf2dArrays, $2D_ARRAY_1, 0, "", "", $ARRAYFILL_FORCE_SINGLEITEM)
    _ArrayAdd($aArrayOf2dArrays, $2D_ARRAY_2, 0, "", "", $ARRAYFILL_FORCE_SINGLEITEM)

    ;Display array of 2d arrays
    _ArrayDisplay($aArrayOf2dArrays, "$aArrayOf2dArrays")

    ;Display the rows of array of 2d arrays
    _ArrayDisplay($aArrayOf2dArrays[0], "Row 0 of $aArrayOf2dArrays")
    _ArrayDisplay($aArrayOf2dArrays[1], "Row 1 of $aArrayOf2dArrays")
EndFunc

 

Expand  

I'm first time in coding )) I cannot use Functions )))

Posted (edited)
  On 12/22/2021 at 4:02 PM, water said:

Can you please tell us what you try to do with your script?
Do you need to create the array at all?
 

Expand  

Target: combine 2D arrays. And create final 2D array. insert final 2D array into sheet.  This script combine data from folder. I'm first time in coding. I understand this code. Loop files is work.  Combine 2D arrays is not work.

Edited by Spec86
Posted

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted (edited)

There is no need to create an array.
I suggest something like this:

#include <File.au3>
#include <Excel.au3>

Global $LineDestiny = 1, $oBookDestiny, $aArraySource, $oBookSource
Global $sPath = "H:\Local\TEST" ; "C:\Users\User\Desktop\Nezavisimai\Papka2\"
Global $aFileListShort = _FileListToArray($sPath, "*.xlsx", $FLTA_FILES, True)
Global $oExcel = _Excel_Open()
Global $oBookDestiny = _Excel_BookNew($oExcel)
_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, "Weather", "A" & $LineDestiny)
_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, "Temp", "B" & $LineDestiny)

For $i = 1 to $aFileListShort[0]
   $oBookSource = _Excel_BookOpen($oExcel, $aFileListShort[$i], False, False)
   $aArraySource = _Excel_RangeRead($oBookSource, $oBookSource.Activesheet, "A2:B4")
   ; Calculate the last used row 
   $iLastUsedRow = $oBookDestiny.Activesheet.UsedRange.SpecialCells($xlCellTypeLastCell).Row
   _Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, $aArraySource, "A" & $iLastUsedRow+1)
   _Excel_BookClose($oBookSource, False)
Next

 

Edited by water

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 12/22/2021 at 5:00 PM, water said:

There is no need to create an array.
I suggest something like this:

#include <File.au3>
#include <Excel.au3>

Global $LineDestiny = 1, $oBookDestiny, $aArraySource, $oBookSource
Global $sPath = "H:\Local\TEST" ; "C:\Users\User\Desktop\Nezavisimai\Papka2\"
Global $aFileListShort = _FileListToArray($sPath, "*.xlsx", $FLTA_FILES, True)
Global $oExcel = _Excel_Open()
Global $oBookDestiny = _Excel_BookNew($oExcel)
_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, "Weather", "A" & $LineDestiny)
_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, "Temp", "B" & $LineDestiny)

For $i = 1 to $aFileListShort[0]
   $oBookSource = _Excel_BookOpen($oExcel, $aFileListShort[$i], False, False)
   $aArraySource = _Excel_RangeRead($oBookSource, $oBookSource.Activesheet, "A2:B4")
   ; Calculate the last used row 
   $iLastUsedRow = $oBookDestiny.Activesheet.UsedRange.SpecialCells($xlCellTypeLastCell).Row
   _Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, $aArraySource, "A" & $iLastUsedRow+1)
   _Excel_BookClose($oBookSource, False)
Next

 

Expand  

Thank you. I still have an interest )))  I would like to have code version with final array ))) How is create final array? How do my first code working?  

Posted
#include <File.au3>
#include <Excel.au3>

Global $sPath = "H:\Local\TEST" ; "C:\Users\User\Desktop\Nezavisimai\Papka2\"
Global $aFileListShort = _FileListToArray($sPath, "*.xlsx", $FLTA_FILES, True)
Global $oExcel = _Excel_Open()
Global $oBookDestiny = _Excel_BookNew($oExcel)
Global $LineDestiny = 1, $oBookDestiny, $aArraySource, $oBookSource, $aArrayOutput[1][2] = [["Weather", "Temp"]]
_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, "Weather", "A" & $LineDestiny)
_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, "Temp", "B" & $LineDestiny)

For $i = 1 to $aFileListShort[0]
   $oBookSource = _Excel_BookOpen($oExcel, $aFileListShort[$i], False, False)
   $aArraySource = _Excel_RangeRead($oBookSource, $oBookSource.Activesheet, "A2:B4")
   _ArrayConcatenate($aArrayOutput, $aArraySource)
   $iLastUsedRow = $oBookDestiny.Activesheet.UsedRange.SpecialCells($xlCellTypeLastCell).Row
   _Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, $aArraySource, "A" & $iLastUsedRow+1)
   _Excel_BookClose($oBookSource, False)
Next
_ArrayDisplay($aArrayOutput)

 

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted
  On 12/22/2021 at 5:37 PM, water said:
#include <File.au3>
#include <Excel.au3>

Global $sPath = "H:\Local\TEST" ; "C:\Users\User\Desktop\Nezavisimai\Papka2\"
Global $aFileListShort = _FileListToArray($sPath, "*.xlsx", $FLTA_FILES, True)
Global $oExcel = _Excel_Open()
Global $oBookDestiny = _Excel_BookNew($oExcel)
Global $LineDestiny = 1, $oBookDestiny, $aArraySource, $oBookSource, $aArrayOutput[1][2] = [["Weather", "Temp"]]
_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, "Weather", "A" & $LineDestiny)
_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, "Temp", "B" & $LineDestiny)

For $i = 1 to $aFileListShort[0]
   $oBookSource = _Excel_BookOpen($oExcel, $aFileListShort[$i], False, False)
   $aArraySource = _Excel_RangeRead($oBookSource, $oBookSource.Activesheet, "A2:B4")
   _ArrayConcatenate($aArrayOutput, $aArraySource)
   $iLastUsedRow = $oBookDestiny.Activesheet.UsedRange.SpecialCells($xlCellTypeLastCell).Row
   _Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, $aArraySource, "A" & $iLastUsedRow+1)
   _Excel_BookClose($oBookSource, False)
Next
_ArrayDisplay($aArrayOutput)

 

Expand  
#include <File.au3>
#include <Excel.au3>

Global $sPath = "C:\Users\User\Desktop\Nezavisimai\Papka2\"
Global $aFileListShort = _FileListToArray($sPath, "*.xlsx", $FLTA_FILES, True)
Global $oExcel = _Excel_Open()
Global $oBookDestiny = _Excel_BookNew($oExcel)
Global $LineDestiny = 1, $oBookDestiny, $aArraySource, $oBookSource, $aArrayOutput[1][2] = [["Weather", "Temp"]]
_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, "Weather", "A" & $LineDestiny)
_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, "Temp", "B" & $LineDestiny)

For $i = 1 to $aFileListShort[0]
   $oBookSource = _Excel_BookOpen($oExcel, $aFileListShort[$i], False, False)
   $aArraySource = _Excel_RangeRead($oBookSource, $oBookSource.Activesheet, "A2:B4")
   _ArrayConcatenate($aArrayOutput, $aArraySource)
   ;$iLastUsedRow = $oBookDestiny.Activesheet.UsedRange.SpecialCells($xlCellTypeLastCell).Row
   ;_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, $aArraySource, "A" & $iLastUsedRow+1)
   _Excel_BookClose($oBookSource, False)
Next
_Excel_RangeWrite($BookDestiny, $BookDestiny.Activesheet, $aArrayOutput, "A2")
_ArrayDisplay($aArrayOutput)

Your code is work. But when insert $aArrayOutput in one iteration all array, write error. I am normal ))) But i would like to insert final array = 1 object.   _Excel_RangeWrite($BookDestiny, $BookDestiny.Activesheet, $aArrayOutput, "A2")

Posted

I have modified the variable names to adhere to the hungary notation.
So

_Excel_RangeWrite($BookDestiny, $BookDestiny.Activesheet, $aArrayOutput, "A2")

should be

_Excel_RangeWrite($oBookDestiny, $oBookDestiny.Activesheet, $aArrayOutput, "A2")

My UDFs and Tutorials:

  Reveal hidden contents

 

Posted

Once again: If you get an error and ask for help, please post the full error message!

My UDFs and Tutorials:

  Reveal hidden contents

 

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...