antmar904

How to display multiple Excel columns in array

6 posts in this topic

#1 ·  Posted (edited)

Hi all,

I am trying to disply columns B and F in an array but its not working.

 

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

$oExcel = _Excel_Open ("", "", "", "", True)
$Book = _Excel_BookOpen($oExcel, @ScriptDir & "\LNExport.xlsx")
$Range1 = $Book.Activesheet.Range("B2").EntireColumn
$Range2 = $Book.Activesheet.Range("F2").EntireColumn
$Result = _Excel_RangeRead($Book, "Sheet 1", $Range1:$Range2)
_ArrayDisplay($Result)
_Excel_Close($Book)
_Excel_Close($oExcel)

 

I can successfully display either column B or column F but I cannot display them both at the same time.

Edited by antmar904

Share this post


Link to post
Share on other sites



The UDF does not support unions. You would need to code something like this:

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

$oExcel = _Excel_Open(Default, Default, Default, Default, True)
$oBook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xlsx")
$aSource1 = _Excel_RangeRead($oBook, Default, $oBook.ActiveSheet.UsedRange.Columns("B"))
$aSource2 = _Excel_RangeRead($oBook, Default, $oBook.ActiveSheet.UsedRange.Columns("F"))
Global $aTarget[UBound($aSource1, 1)][2]
For $i = 0 To UBound($aSource1, 1) - 1
    $aTarget[$i][0] = $aSource1[$i]
    $aTarget[$i][1] = $aSource2[$i]
Next
_ArrayDisplay($aTarget)
_Excel_Close($oBook)
_Excel_Close($oExcel)

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - 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

#3 ·  Posted (edited)

@water,

I can get this to work like this...

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

$oExcel = _Excel_Open(Default, Default, Default, Default, True)
If @error Then Exit MsgBox(17, '', @error & @CRLF & @extended)

$oBook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xls")
If @error Then Exit MsgBox(17, '', @error & @CRLF & @extended)

$aSource1 = _Excel_RangeRead($oBook)
If @error Then Exit MsgBox(17, '', @error & @CRLF & @extended)

Global $aTarget[UBound($aSource1, 1)][2]
For $i = 0 To UBound($aSource1, 1) - 1
    $aTarget[$i][0] = $aSource1[$i][0]
    $aTarget[$i][1] = $aSource1[$i][4]
Next

_ArrayDisplay($aTarget)
_Excel_Close($oBook)
_Excel_Close($oExcel)

but when I use...

$aSource1 = _Excel_RangeRead($oBook, Default, $oBook.ActiveSheet.UsedRange.Columns("B"))

I get an array with no values and @ERROR = 0.

Environment:

  • Win 7 home x64
  • Excel 2002/SP3

Incidentally, your code only works if both columns are the same length.  My code solves that but will take longer if there is a lot of data.

kylomas

Edit: This is what I ran...

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

$oExcel = _Excel_Open(Default, Default, Default, Default, True)
If @error Then Exit MsgBox(17, '', @error & @CRLF & @extended)

$oBook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xls")
If @error Then Exit MsgBox(17, '', @error & @CRLF & @extended)

$aSource1 = _Excel_RangeRead($oBook, Default, $oBook.ActiveSheet.UsedRange.Columns('B'))
;$aSource1 = _Excel_RangeRead($oBook)
If @error Then Exit MsgBox(17, '', @error & @CRLF & @extended)
_ArrayDisplay($aSource1, '#1')

$aSource2 = _Excel_RangeRead($oBook, Default, $oBook.ActiveSheet.UsedRange.Columns("F"))
If @error Then Exit MsgBox(17, '', @error & @CRLF & @extended)
_ArrayDisplay($aSource1, '#2')

Global $aTarget[UBound($aSource1, 1)][2]
For $i = 0 To UBound($aSource1, 1) - 1
    $aTarget[$i][0] = $aSource1[$i]
    $aTarget[$i][1] = $aSource2[$i]
Next

_ArrayDisplay($aTarget)
_Excel_Close($oBook)
_Excel_Close($oExcel)

using this spreadsheet...test.xls

Edited by kylomas

Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

Run the following script and you will see that the UsedRange starts in column B. When you retrieve the UsedRange and then access column "B" in this range you actually access column "C" of the whole worksheet - which indeed is empty:

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

$oExcel = _Excel_Open(Default, Default, Default, Default, True)
If @error Then Exit MsgBox(17, '', @error & @CRLF & @extended)

$oBook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test Kylomas.xls")
If @error Then Exit MsgBox(17, '', @error & @CRLF & @extended)
$oBook.ActiveSheet.UsedRange.Interior.ColorIndex = 3
MsgBox(0, "", "Usedrange")

_Excel_Close($oBook)
_Excel_Close($oExcel)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - 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

@water

Thank you that worked well.

Share this post


Link to post
Share on other sites

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - 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

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

    • VeryGut
      By VeryGut
      I'm trying to insert the following formula in cell A2 using my script:
      =if(A1=""; "YES"; "NO")
      To my understanding, the line of code should be similar to this:
      _Excel_RangeWrite($MasterFile, Default, "=if(A1=""; "YES"; "NO")", "A2")
      However, it does not work, probably due to the multiple quotation marks that confuse the script :C
      How do I avoid this problem?
    • LoneWolf_2106
      By LoneWolf_2106
      Hi everybody,
      i have a question about Excel, i have to create several charts one below the other dynamically.
      I have thought to use:
       
      $oRangeLast = .UsedRange.SpecialCells($xlCellTypeLastCell) $iRowCount = .Range(.Cells(1, 1), .Cells($oRangeLast.Row, $oRangeLast.Column)).Rows.Count  
      And then to use it in this way:
      $Graph_position = "=Test1!A"&$iRowCount+2&":K"&$iRowCount+24 But it doesn't work with charts.
      Does anyone have a suggestion?
       
    • LoneWolf_2106
      By LoneWolf_2106
      Hi all,
      i have an empty csv file, i have a non formatted text file.
      What do i want to do?
      I want to automate the process "get external data" in Excel, i want to import the data from the text file and basically create a csv file with a specific character encoding.
      Is it possible with AutoIT?
       
    • czardas
      By czardas
      Haven't had much time to code recently. However the following thread inspired me.
      The debate about linear, parallel and binary search methods was rather interesting and, in an attempt to be diplomatic, I decided to combine @jchd's suggestion with @LarsJ's binary search example. I decided that the binary search algorithm required modification to make it more linear. As usual, 'if you invent something, it probably already exists and if it already exists, it exists for a reason'. My first attempt was not all that good. The code worked but was really a mess. I blame peer pressure (to post an example of a parallel search method).  I will delete that old code in due course.
      With a little memory jogging and a glance at the help file, the solution turned out to be quite easy: I just needed a better understanding of Euler. Further modification will be needed to work with more complicated unicode strings. The output could be returned as an array or a delimitered string. I'm not so interested in those details. I'm just going to post the algorithm for now and anyone, who wants to, can modify it to suit their needs. Both arrays must contain at least 1 element.
      Local $aFoo = [0,1,2,3,4,5,6,7,9,10,11,12,13,14,15,16,19,20,23,24,26,30,35,39,40,41] Local $aBar = [0,1,5,6,7,8,9,10,11,12,13,14,17,18,19,21,24,25,26,27,34,35,38,40] ParallelExponetialSearch($aFoo, $aBar) ; Compares two lists - returning positive matches. Each input array must be unique (individually) and in alphabetical order. Func ParallelExponetialSearch($aFoo, $aBar) Local $sFind, _ $iMin_F = -1, $iMax_F = UBound($aFoo) -1, $Lo_F = $iMin_F, $Hi_F, _ $iMin_B = -1, $iMax_B = UBound($aBar) -1, $Lo_B = $iMin_B, $Hi_B While $iMin_F < $iMax_F And $iMin_B < $iMax_B ; Toggle Arrays - Which array has most untested elements? This is the one we want to search next, ; so we can bypass more comparisons because (in theory) mismatches have a greater chance of being skipped. If $iMax_F - $iMin_F >= $iMax_B - $iMin_B Then ; $aFoo has more (or an equal number of) untested elements $Hi_F = $iMax_F $iMin_B += 1 $sFind = $aBar[$iMin_B] While $Lo_F < $Hi_F ; search $aFoo For $i = 0 To Floor(Log($Hi_F - $Lo_F) / Log(2)) $Lo_F = $iMin_F + 2^$i If $aFoo[$Lo_F] = $sFind Then $iMin_F = $Lo_F ; each match should be added to the output [perhaps an array] ConsoleWrite($sFind & " found at $aFoo[" & $Lo_F & "] = $aBar[" & $iMin_B & "]" & @LF) ExitLoop 2 ElseIf $aFoo[$Lo_F] > $sFind Then $Hi_F = $Lo_F -1 $iMin_F += Floor(2^($i -1)) $Lo_F = $iMin_F ContinueLoop 2 EndIf Next $iMin_F = $Lo_F ; minimum increment is one WEnd Else ; $aBar has more untested elements $Hi_B = $iMax_B $iMin_F += 1 $sFind = $aFoo[$iMin_F] While $Lo_B < $Hi_B ; search $aBar For $i = 0 To Floor(Log($Hi_B - $Lo_B) / Log(2)) $Lo_B = $iMin_B + 2^$i If $aBar[$Lo_B] = $sFind Then $iMin_B = $Lo_B ; each match should be added to the output [perhaps an array] ConsoleWrite($sFind & " found at $aFoo[" & $iMin_F & "] = $aBar[" & $Lo_B & "]" & @LF) ExitLoop 2 ElseIf $aBar[$Lo_B] > $sFind Then $Hi_B = $Lo_B -1 $iMin_B += Floor(2^($i -1)) $Lo_B = $iMin_B ContinueLoop 2 EndIf Next $iMin_B = $Lo_B ; minimum increment is one WEnd EndIf WEnd EndFunc ;==> ParallelExponetialSearch I hope this will be useful to someone. I believe it deserved a thread of its own!
    • RC86
      By RC86
      Afternoon!
      I have a requirement to return both a string and an array from a function so as a result I put them both into an array and returned that.  I can access them in their entirety after returning them but then I can't seem to access the array elements after this.  Should I be able to or is there a prettier way?
      #include <Array.au3> ;Memory info returned as a string and an array $memoryInfo = _getMemoryInfo() msgbox(0,"Memory Info",$memoryInfo[0]) _ArrayDisplay($memoryInfo[1],"Memory as an Array") Local $newArray[7] $memoryInfo[1] = $newArray msgbox(0,"Test element",$newArray[0]) _ArrayDisplay($newArray) Func _getMemoryInfo() Local $newArray[7] Local $array = MemGetStats() $newArray[0] = $array[0] ;% of memory in use $newArray[1] = Round($array[1]/1024 * 0.001,2) ;Total physical RAM $newArray[2] = Round($array[2]/1024 * 0.001,2) ;Availaible physical RAM $newArray[3] = Round($array[3]/1024 * 0.001,2) ;Total pagefile $newArray[4] = Round($array[4]/1024 * 0.001,2) ;Available pagefile $newArray[5] = Round($array[5]/1024 * 0.001,2) ;Total virtual $newArray[6] = Round($array[6]/1024 * 0.001,2) ;Available virtual $memoryUsage = $newArray[1] - $newarray[2] $pagefileUsage = $newArray[3] - $newarray[4] ;Output/Return Local $returnArray[2] $returnArray[0] = "Memory: " & $memoryUsage & " GB/" & $newArray[1] & " GB " & @CRLF & "Pagefile: " & $pagefileUsage & " GB/" & $newArray[3] & " GB " $returnArray[1] = $newArray return $returnArray EndFunc A bit messy but hopefully it's understandable what I'm trying to achieve.
      Thanks!