JKnight_xbt33 wrote: ↑05 Oct 2020, 05:31
Dear all,
I have an excel table with 5 columns (A to E) and 5 rows (1 TO 5)
I want to store the output of each individual cell as a variable.
I know how to output the array of columns but not each individual cell (see example below)
Does anyone know how to do this. for example if I want to store the value of A2 into a variable?
Code: Select all
file2open := "\\private\Users\JK\Documents\8-IT\AHK scripts\Preventing mistakes pdf check\test excel data.xlsx"
arr := {}
XL := ComObjCreate( "Excel.Application" )
XL.Workbooks.Open(file2open)
For c In xl.ActiveSheet.UsedRange.cells
arr[c.address(0,0)] := c.Value
loop, % xl.Activesheet.UsedRange.rows.count
comp := arr["a" a_index
conv := arr["c" a_index]
serial := arr["d" a_index]
msgbox % arr["a" a_index] arr["c" a_index] arr["d" a_index]
appreciated
J
You can also use Excel's native SafeArray:
Code: Select all
File2Open := A_Desktop "\Test\specificExcelFile.xlsx"
xlApp := ComObjCreate( "Excel.Application" )
xlWB := xlApp.Workbooks.Open(File2Open)
xlApp.Visible := true
SafeArray := xlWB.ActiveSheet.Range("B2:C4").Value
MsgBox % SafeArray[1,2] ; First Row, Second Column of Range
If you really just want A2 then:
Code: Select all
File2Open := A_Desktop "\Test\specificExcelFile.xlsx"
xlApp := ComObjCreate( "Excel.Application" )
xlWB := xlApp.Workbooks.Open(File2Open)
xlApp.Visible := true
xlCell_A2 := xlWB.ActiveSheet.Range("A2").Value
MsgBox % xlCell_A2
Then you have to think if you really need the values in a variable or array. I mean unless you are doing something very repetitive with the value it is hardly worth storing all the information, you can just access it as you need it.
Code: Select all
File2Open := A_Desktop "\Test\specificExcelFile.xlsx"
xlApp := ComObjCreate( "Excel.Application" )
xlWB := xlApp.Workbooks.Open(File2Open)
xlApp.Visible := true
for Cell in xlWB.ActiveSheet.Range("B2:C4")
MsgBox % Cell.Value
Loop through all cells in a range.
FG