Reading multiple cells after merged cell from excel sheet
21 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ghenji
el 14 de Mzo. de 2018
Comentada: Guillaume
el 20 de Mzo. de 2018
Cells A1 and B1 are merged. Now when I read Cells A1 and B1 with Excel COM Application, they read 'Waste Accumulation'and 'NaN' and The immediate cell going down after merged cell is just A2 . Is there a method with which I can read both the cells A2 and B2 after read merged cell? I would like to read column after cells A2 and B2 separately.
Respuesta aceptada
Guillaume
el 14 de Mzo. de 2018
I'm still not exactly clear what your question is. If you're trying to find the cells that are part of the merge I've already shown how to do that in my comment:
%...
sheet = workbook.Worksheets.Item(sheetname{1});
start_range = sheet.Cells.Find(string_item{1}); %no need to get the address
end_range = start_range.End('xlDown');
numcolumns = start_range.MergeArea.Columns.Count; %if you want to know how many columns are merged
After that I'm not sure what you want to do.
5 comentarios
Guillaume
el 20 de Mzo. de 2018
Editada: Guillaume
el 20 de Mzo. de 2018
Not having your spreadsheet, I obviously cannot try your code.
I forgot to subtract one from numcolumns. Hence why you get an extra column. Fixed now.
If you just want the one row after the merged cells:
sheet = workbook.Worksheets.Item(sheetname{1});
start_range = sheet.Cells.Find(string_item{1}); %no need to get the address
numcolumns = start_range.MergeArea.Columns.Count;
full_range = get(sheet, 'Range', get(start_range, 'Offset', 1), get(start_range, 'Offset', 1, numcolumns-1));
header = full_range.value;
Guillaume
el 20 de Mzo. de 2018
Ok. Something I hadn't noticed before: when you offset a merged range by 0 columns, the returned range is offset relative to the start column, when you offset the same range by 1 or more columns, the returned range is offset relative to the end column. That greatly messes things up. Wouldn't be the first thing that makes no sense with Excel!
Never mind, we can obtain the range of the next row without using Offset:
start_range = sheet.Cells.Find(data_items{1}); %no need to get the address
numcolumns = start_range.MergeArea.Columns.Count;
full_range = get(sheet, 'Range', get(sheet, 'Cells', start_range.Row + 1, start_range.Column), get(sheet, 'Cells', start_range.Row + 1, start_range.Column + numcolumns - 1));
header = full_range.Value
Más respuestas (0)
Ver también
Categorías
Más información sobre Use COM Objects in MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!