Borrar filtros
Borrar filtros

Using cell offset in Excel sheet from Matlab

13 visualizaciones (últimos 30 días)
Renish Ramegowda
Renish Ramegowda el 10 de Oct. de 2014
Editada: Andreas Martin el 22 de Mayo de 2017
ExcelFileName = 'Results.xls';
% COM Object for Excel application
h = actxserver('Excel.Application');
set(h, 'DisplayAlerts', 0);
set(h, 'visible', 0);
exlWkbk = h.Workbooks;
exlOpen = exlWkbk.Open([pwd,'\',ExcelFileName], 0, false);
exlSheets = h.ActiveWorkbook.Sheets;
exlsheetObj = exlSheets.get('Item', 'Header');
exlsheetObj.Activate;
Addr = exlsheetObj.Range('B:B').Find('Renish').Address;
ActiveRange = get(h.Activesheet, 'Range', Addr);
ActiveRange.Select;
ActiveRange.Offset(1,1).Value;
With this code, I get the value in the cell as 'Renish'. When I need to get the value of neighbouring cell, I used offset as in VB script
ActiveRange.Offset(1,2).Value;
But it throws error as "Index exceeds matrix dimensions."
If you specify "Offset(1,0)" or "Offset(1,-1)" in the same code, it throws error as "Subscript indices must either be real positive integers or logicals."
Can anybody help me to find the values of neighbouring cells.
Also how can "Subscript indices" be given "logicals"? Is it a incorrect information display from mathworks?
I even tried below steps and nothing is working
h.ActiveCell.Offset(1,2).Value;
h.Selection.Offset(1,2).Value;

Respuestas (1)

Andreas Martin
Andreas Martin el 22 de Mayo de 2017
Editada: Andreas Martin el 22 de Mayo de 2017
Hi,
maybe it's outdated, but for completeness: using the function 'get' does the trick.
ExcelFileName = 'Results.xls';
% COM Object for Excel application
h = actxserver('Excel.Application');
set(h, 'DisplayAlerts', 0);
set(h, 'visible', 0);
exlWkbk = h.Workbooks;
exlOpen = exlWkbk.Open([pwd,'\',ExcelFileName], 0, false);
exlSheets = h.ActiveWorkbook.Sheets;
exlsheetObj = exlSheets.get('Item', 'Header');
exlsheetObj.Activate;
Addr = exlsheetObj.Range('B:B').Find('Renish').Address;
ActiveRange = get(h.Activesheet, 'Range', Addr);
ActiveRange.Select;
ActiveRange. *get*( 'Offset', 1, 1 ).Value; % ActiveRange.Offset(1,1).Value;
I think some calls are superfluous (and time expensive) and may be omitted:
ExcelFileName = 'Results.xls';
% COM Object for Excel application
h = actxserver('Excel.Application');
set(h, 'DisplayAlerts', 0);
set(h, 'visible', 0);
exlWkbk = h.Workbooks;
exlOpen = exlWkbk.Open([pwd,'\',ExcelFileName], 0, false);
exlSheets = h.ActiveWorkbook.Sheets;
exlsheetObj = exlSheets.get('Item', 'Header');
Cell = exlsheetObj.Range('B:B').Find('Renish');
value = Cell.get( 'Offset', 1, 1 ).Value;
Cheers, Andreas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by