Jump to content

How to display multiple Excel columns in array


 Share

Recommended Posts

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
Link to comment
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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

@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

Link to comment
Share on other sites

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 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

:)

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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
 Share

×
×
  • Create New...