Jump to content

Get Excel selection address [Solved]


ahha
 Share

Recommended Posts

I'm using the excellent _Excel UDF to read a whole spreadsheet into an array I want to operate on.  I can read a specified range without a problem.  What I'd like to do is allow the user in Excel to select a range (contiguous is fine) and be able to read that range so that I know what range to operate on without having the user input the range via a keyboard.  I'm stuck and any hints greatly appreciated.  The code below uses the Helpfile .xls to illustrate where I crash.

;
#AutoIt3Wrapper_run_debug_mode=Y    ; use this to debug in console window <--- LOOK

#include <Excel.au3>
#include <MsgBoxConstants.au3>
#include <Debug.au3>    ;for _DebugArrayDisplay

$sExcelFullFileName = "C:\Program Files (x86)\AutoIt3\Examples\Helpfile\Extras\_Excel1.xls"     ;use Autoit test for example

$oExcel = _Excel_Open() ;Create application object and open an Excel workbook
$oWorkbook = _Excel_BookOpen($oExcel, $sExcelFullFileName)
$aExcelArray = _Excel_RangeRead($oWorkbook, Default)
_DebugArrayDisplay($aExcelArray, $aExcelArray)  ;let's look at it
$aI5K6 = _Excel_RangeRead($oWorkbook, Default,"I5:K6")  ;read a rangeI5:K6 and return value
_DebugArrayDisplay($aI5K6, $aI5K6)  ;let's look at range it

;rather than defining the range explicity I'd like to read the selection in Excel and use it
;need a function like _Excel_SelectionRead
;try hacking Excel.au3 _Excel_RangeRead to see if can create _Excel_SelectionRead  looks like .Selection .Value .Text and .Address possible
MsgBox($MB_SYSTEMMODAL, "Info", "Select cells in Excel then click OK")

;ACTUALLY get range of the selection and this will suffice for us to use on the $aExcelArray
$vRange = $oWorkbook.ActiveSheet.Selection.Address($vRange)
;tried all sorts of permutations and clearly I need help
_DebugArrayDisplay($vRange, $vRange)    ;let's look at range it

;$vResult = $oExcel.Transpose($vRange.Value)
;_DebugArrayDisplay($vResult, $vResult) ;let's look at range it

_Excel_Close($oExcel, False, True)
Exit

 

Edited by ahha
solved
Link to comment
Share on other sites

$oExcel.Selection returns the range selected by the user.
See: https://docs.microsoft.com/de-de/office/vba/api/excel.application.selection

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.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 (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...