image thumbnail

Excel auto alphabet cell range

by

 

This code generates the alphabetical index of any cell in excel doc.,

[indx]=letter_excel(N)
function [indx]=letter_excel(N)
% N=input('inter the alphabet Number:   ');
alpha=0;init=0;
N_tmp=N;
if N>26
    N_tmp=N-26;
    if N_tmp>676
        N_tmp=N_tmp-676;
        if N_tmp>17576
            disp('ERROR OUT OF EXCEL RANGE')
        else
            N_loop=N_tmp;
            while N_loop>676
                N_loop=N_loop-676;
                init=init+1;
            end
            if N_tmp/676<=1 && init==1; init=init-1; end 
            first=char('A'+init);
            if rem(N_loop,26)==0; alpha=1;end 
            second=char('A'+floor((N_tmp-init*676)/26)-alpha);
            while N_loop>26
             N_loop=N_loop-26;
            end
            third=char('A'+N_loop-1);
            if rem(N_tmp,26)==0; third=char('A'+26-1); end
            indx=strcat(first,second,third)
        end
    else
            N_loop=N_tmp;
           while N_loop>26
                 N_loop=N_loop-26;
           end
           if rem(N_loop,26)==0; alpha=1;end 
           first=char('A'+floor((N_tmp-init*26)/26)-alpha);
           second=char('A'+N_loop-1);
           indx=strcat(first,second)
    end
else
    indx=char('A'+N-1)
end

Contact us