Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Reading an Excel cell using a variable



  • Please log in to reply
4 replies to this topic
Jstjacques
  • Members
  • 112 posts
  • Last active: May 12 2014 09:43 PM
  • Joined: 10 Jan 2013

I've given up on search and am now asking for your help!  I am very new to AHK_L (first script), I've been using AHK basic for a while now. 

 

The code below works as it should:

XL := ComObjCreate("Excel.Application") 
Workbook := XL.Workbooks.Open(File) 
CellA1Value := Workbook.activeSheet.Range("A2").Value
msgbox, % cellA1Value

But, when I attempt to turn the range into a variable (2 different ways), I get an error:

#1

count = 2
XL := ComObjCreate("Excel.Application") 
Workbook := XL.Workbooks.Open(File) 
CellA1Value := Workbook.activeSheet.Range("A%count%").Value
msgbox, % cellA1Value

#2

count = 2
string = Workbook.activeSheet.Range("A%count%").Value
XL := ComObjCreate("Excel.Application") 
Workbook := XL.Workbooks.Open(File) 
CellA1Value := %string%
msgbox, % cellA1Value

Please help!  I can't figure out how I am supposed to read a cell based on a variable.


You can get a good look at a steak but shoving your head up a cow's rear end, but wouldn't you rather take the butchers word for it?


sinkfaze
  • Moderators
  • 6367 posts
  • Last active: Nov 30 2018 08:50 PM
  • Joined: 18 Mar 2008
✓  Best Answer

Expression mode must be respected:
 

count=2
xl :=	ComObjCreate("Excel.Application") 
workBook :=	xl.Workbooks.Open(File) 
cellValue :=	Workbook.activeSheet.Range("A" count).Value
msgbox, %	cellValue

And just FYI, you can shortcut the process of opening the workbook:

count=2
workBook :=	ComObjGet(File) 
cellValue :=	Workbook.activeSheet.Range("A" count).Value
msgbox, %	cellValue


Jstjacques
  • Members
  • 112 posts
  • Last active: May 12 2014 09:43 PM
  • Joined: 10 Jan 2013

That was exactly what I need, and in record time!  Thanks a lot for the help.


You can get a good look at a steak but shoving your head up a cow's rear end, but wouldn't you rather take the butchers word for it?


guyerown
  • Members
  • 1 posts
  • Last active: May 07 2015 05:39 AM
  • Joined: 06 May 2015

Am getting 

 "Error: 80010001 - Call was rejected by callee.

 

  Specifically: ActiveCell"

 

Please help!



Jackie Sztuk _Blackholyman
  • Spam Officer
  • 3757 posts
  • Last active: Apr 03 2016 08:47 PM
  • Joined: 28 Feb 2012

Active workbook can NOT be in EditMode other then that we need more info to help you


Helping%20you%20learn%20autohotkey.jpg?d

[AHK] Version. 1.1+ [CLOUD] DropBox ; Copy [WEBSITE] Blog ; About