How do I select several rows in Excel?
Mostrar comentarios más antiguos
I am trying to find the quickest way to color thousands of rows in an Excel spreadsheet. My current method involves creating a cell array with strings, limited by the number of characters, of all the Excel cells that need to be colored, and then applying the appropriate colors at the end with several loops. If there is a way to use Union within Matlab, or to select thousands of non-adjacent rows within Excel, so that I can apply colors in one go without a loop, I'd greatly appreciate any suggestions!
Summary of current code:
xls=actxserver('Excel.Application');
xlsWB=xls.Workbooks.Open(FULL_XLS_FILE);
xlsWS=xlsWB.Sheets.Item(1);
rangeidx=cell(1000,10);
rangeidx(:)={''};
j=ones(1,10);
strlimit=240; %max for Range input is apparently 255 char, assume cells reach 6 digits
% This is one section of the loop for one coloring scheme, which is one
% string segmented into 255 char limited chunks.
for i=2:size(FINAL,1) %cycling through every row for color index
if coloridx{i} == 1 %for one coloring scheme
if coloridx(i-1) == 1 %for adjacent rows
rangeidx{j(1),1}= rangeidx{j(1),1}(1:end-(numel(num2str(i))+1)); % removing last row number and comma
rangeidx{j(1),1}=[rangeidx{j(1),1} sprintf('%g,',i)]; % adding second row number for previous column I
else %regular input
rangeidx{j(1),1}=[rangeidx{j(1),1} sprintf('A%g:I%g,',i,i)]; %storing syntax for rows to be colored
end
if size(rangeidx{j(1),1},2) >= strlimit %for max Range string input
j(1)=j(1)+1; %cycling index for next rows
end
end
end
% One out of many loops for coloring all the rows. The Range() part is what
% limits the chars for the strings listing the rows. This line is what
% takes the most time, and I'm trying to find a method that doesn't require
% it to be looped.
for i=1:j(1) %for one color
if size(rangeidx{i,1},2) >= 6 %minimum length for one input
rangeidx{i,1}=rangeidx{i,1}(1:end-1); %getting rid of comma
xlsWS.Range(rangeidx{i,1}).Interior.Color=rgbGreen;
end
end
Respuesta aceptada
Más respuestas (1)
dpb
el 5 de En. de 2022
I'll throw the last example code out as an Answer as well as Comment just for visibility -- and it may turn out useful for others as well.
Excel = actxserver('Excel.Application');
Workbook = Excel.Workbooks.Open(fullfile(FOLDER,'testunion.xlsx'))
Excel.Worksheets.Item("Sheet1").Activate
uRnge=Excel.Application.Union(Excel.Range("A1"),Excel.Range("A10"))
uRnge.Formula="=RAND()"
Excel.ActiveWorkbook.Save
Excel.ActiveWorkbook.Close(false)
delete(Excel),clear Excel
The example in the MS documentation worked as advertised -- the above begins to illustrate what you have to do to simulate the VBA compiler translation to low-level commands that the COM engine can understand (really, instantiating references to the COM object you created instead of the MATLAB interpreter trying to read all as MATLAB variables/functions).
The reference to the 'Item' array in the Worksheets collection instead of trying to just write
Excel.Worksheets("Sheet1").Activate
is a hard-won lesson of trial and error and finding other code smarter folks than I had figured out...
I think it is illustrated the concept works--now whether it will be any faster or not, only experience and trying it out will determine.
The key is to activate the sheet you're working on, build a range object as the output from the Union method and then do your thing on that compound range.
The Q? will be whether it's any faster to make a call to Union with a whole bunch of arguments with individual references to the Range method over just calling each iteratively. The MATLAB code to build a set of string references will be fast, of course, the possible bottleneck will be in turning those into Excel range references/objects that the Union method expects...it doesn't know anything about just a cell address, unfortunately.
I presume but didn't test that you can build range expressions to pass to .Range() above that are also compound references, whether the overhead then is reduced or not again only testing will tell.
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!