With activeX server running Excel, access the cells syntax on range property
Mostrar comentarios más antiguos
Hello there,
I am trying to fill an excel sheet by running an activeX server, and I would like to access the right range like i would do in VBA, for instance :
WS.Range(Cells(1,1),Cells(10,2))
I used to use this syntax, but with letters for the column index:
e = actxserver('Excel.Application');
eW = e.Workbooks;
eF = eW.Open(result);
e.Visible = 0;
e.DisplayAlerts = false;
eS = e.ActiveWorkbook.Sheets.get('Item',1);
eS.Activate;
eActivesheetRange = get(e.Activesheet,'Range','A1:B10');
eActivesheetRange.Value = Data;
I would like to run a loop to write the right data in the right column.
(I know I can use xlswrite and so but that's not the point ;) )
Thanks !
Thomas
1 comentario
Guillaume
el 13 de Ag. de 2019
When automating Excel/Word/etc. you should never rely on Activexxx as whatever is active may change without you knowing (e.g. a macro or the user doing something else wiith Excel/Word/etc.)
In your example code, it's also completely pointless since e.Activesheet will basically return the eS you already have unless the user or a macro actually change the active sheet.
Also, note that most times, you don't need to use get:
excel = actxserver('Excel.Application'); %invisible by default
excel.DisplayAlerts = false;
workbook = excel.Workbooks.Open(result);
worksheet = workbook.Sheets.Item(1); %no need for get
range = worksheet.Range.Item('A1:B10'); %no need to go through activesheet, or get
range.Value = data;
Respuesta aceptada
Más respuestas (1)
Bob Thompson
el 7 de Ag. de 2019
0 votos
I used to do this by creating a variable ('range' for simplicity) that was defined with string concatenation. Because you want the range to vary I would suggest using sprintf.
for i = 1:26
range = sprintf('%c%i',i,i);
end
The results of the loop should be single cells A1, B2, C3, ..., Z26. You can do this for both starting and ending, and you can define integers in the string, rather then as variables. Keep in mind though that %c for 27 is not AA, so you will need to create a different string condition if you plan on having the range cover columns greater than Z.
3 comentarios
Thomas Hermelin
el 13 de Ag. de 2019
Guillaume
el 13 de Ag. de 2019
The characters are not blank or nothing, they're actually control characters.
Bob is missing an offset for the character conversion, it should be:
range = sprintf('%c%i', col+'A'-1, row); %only works for row up to 26
Ginny
el 4 de Dic. de 2023
If you want to include letters past Z:
if col > 26 && col < 52
col2 = col - 26;
range = sprintf('%c%c%i','A',col2+'A'-1,row);
end
Categorías
Más información sobre Spreadsheets en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!