Jump to content

RangeRead from active Excel cell


Recommended Posts

Hey guys :)
I'm new to autoit, so this could be a simple question. I'm trying to read the value from the currently selected cell in Excel. I read on the forum and tired to find videos, but I couldn't quite get to it. 

This is what I have got so far:

 

Local $oExcel_1 = _Excel_Open()

Local $var = "C:\Users\Acer\xy"

Local $oWorkbook = _Excel_BookOpen($oExcel_1,$var)

 

Local $_read1 = _Excel_RangeRead($oWorkbook, Default.Application.ActiveCell.Address)

 

Whatever I try, I either get an error or it only reads "0".

 

Thank you very much for any helpful thoughts!

Link to comment
Share on other sites

You need to use _Excel_BookAttach to read ActiveCell, if the workbook isn't opened you need to define the cell you want to read, for example:

#include <Excel.au3>
Local $bOpenWorkBook = False, $oExcel = _Excel_Open()
Local $sFilePath = @ScriptDir & "\Test.xlsx"

Local $oWorkbook
$oWorkbook = _Excel_BookAttach($sFilePath)
If @error Then
    $oWorkbook = _Excel_BookOpen($oExcel, $sFilePath)
    $bOpenWorkBook = True
 EndIf

If $bOpenWorkBook Then
    $sCell = _Excel_RangeRead($oWorkbook, Default, "A20") ;~ Workbook was opened so read cell A2
Else
    $sCell = _Excel_RangeRead($oWorkbook, Default, $oExcel.ActiveCell.Address) ;~ Workbook was already open, read the active cell.
EndIf
MsgBox(4096, "Excel Cell Value", "Value = " & $sCell)

 

Link to comment
Share on other sites

23 hours ago, Subz said:

You need to use _Excel_BookAttach to read ActiveCell, if the workbook isn't opened you need to define the cell you want to read, for example:

#include <Excel.au3>
Local $bOpenWorkBook = False, $oExcel = _Excel_Open()
Local $sFilePath = @ScriptDir & "\Test.xlsx"

Local $oWorkbook
$oWorkbook = _Excel_BookAttach($sFilePath)
If @error Then
    $oWorkbook = _Excel_BookOpen($oExcel, $sFilePath)
    $bOpenWorkBook = True
 EndIf

If $bOpenWorkBook Then
    $sCell = _Excel_RangeRead($oWorkbook, Default, "A20") ;~ Workbook was opened so read cell A2
Else
    $sCell = _Excel_RangeRead($oWorkbook, Default, $oExcel.ActiveCell.Address) ;~ Workbook was already open, read the active cell.
EndIf
MsgBox(4096, "Excel Cell Value", "Value = " & $sCell)

 

Thank you very much for the example! Yeah, the BookAttach was missing.

As I corrected my script, I came up with the question, if it was possible to read from a cell near the active cell. With an offset function for example:

Local $_read2 = _Excel_RangeRead($oWorkbook, Default, $oExcel.ActiveCell.Address.offset(2,2)) ?

Is this possible with Autoit?

 

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...