Using cell offset in Excel sheet from Matlab
조회 수: 13 (최근 30일)
이전 댓글 표시
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;
댓글 수: 0
답변 (1개)
Andreas Martin
2017년 5월 22일
편집: Andreas Martin
2017년 5월 22일
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
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Spreadsheets에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!