i need a ruby code to read the column a and find where does the last filled cell in the column ends.In the image uploaded the last filled data is i in cell "A21". i need to know this cell address through ruby code.
Asked
Active
Viewed 1,285 times
2

Arup Rakshit
- 116,827
- 30
- 260
- 317

anurag
- 145
- 1
- 1
- 8
-
Are you using `win32ole` ? One more clarification I need - what do you mean by *i need to know this **cell address** through ruby code* ? – Arup Rakshit Mar 05 '14 at 09:00
-
@ArupRakshit: Yes i am using Win32ole...please help – anurag Mar 05 '14 at 09:02
-
means i need to reach to the last cell with data and fetch the data from that cell ...as in the image my last data is i i need to get the cell address and the value of that cell. – anurag Mar 05 '14 at 09:03
-
**cell address** means - `(21,1)` for the last data `i`.. right ? – Arup Rakshit Mar 05 '14 at 09:05
-
1yes rakshit u r correct in ur understanding – anurag Mar 05 '14 at 09:09
2 Answers
4
I would do using the Ruby stdlib WIN32OLE
.
require 'win32ole'
# create an instance of the Excel application object
excel = WIN32OLE.new('Excel.Application')
# make Excel visible
excel.visible = true
# open the excel from the desired path
wb=excel.workbooks.open("C:\\Users\\test.xlsx")
# get the first Worksheet
wbs= wb.Worksheets(1)
# value of the constants I picked up from
# http://techsupt.winbatch.com/ts/T000001033005F9.html
rng = wbs.range("1:1").SpecialCells(11) # value of 'xlCellTypeLastCell' is 11
rng.value # => "i"
rng.address # => "$A$21"
# to get the row and column number
row,col = rng.row,rng.column
[row,col] # => [21,1]
Look at the MSDN documentation of SpecialCells
.
You can get the value of xlCellTypeLastCell
from the version of MSExcel
installed in your pc. Just do ALT+F11
-> F2
-> search the constant there :

Arup Rakshit
- 116,827
- 30
- 260
- 317
-
@anurag I learned a lot from your question to.. and Glad to help you out. – Arup Rakshit Mar 05 '14 at 10:11
-
-
-
actually i dnt know how to vote for a question so didn't do it...sorry:) but serious thanks for ur superb and quick help – anurag Mar 07 '14 at 14:47
-
`rng = wbs.range("1:1").SpecialCells(11)`, does work, but if there are borders around empty cells, it will include the borders, is there a way to determine the last cell with a value in it, using ruby? – Davesexcel Jan 09 '17 at 13:35
0
try using 'spreadsheet' gem
book = Spreadsheet.open 'myexcel.xls';
sheet1 = book.worksheet 0
last_value = nil
sheet1.each do |row|
last_value = row[0].present? && row[0]
end
last_value
=>
"i"

Sachin Singh
- 7,107
- 6
- 40
- 80