Adrees to the column/row end

%3CLINGO-SUB%20id%3D%22lingo-sub-2016502%22%20slang%3D%22en-US%22%3EAdrees%20to%20the%20column%2Frow%20end%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2016502%22%20slang%3D%22en-US%22%3E%3CP%3EI%20would%20like%20to%20form%20an%20address%20from%20a%20cell%20to%20the%20end%20of%20its%20column%20or%20row%2C%20for%20instance%20in%20a%20total%20on%20the%20top%20of%20a%20column%20whose%20extension%20covers%20the%20column%20below%20in%20full.%20This%20is%20different%20from%20a%20full%20column%2Frow%20address%20(for%20instance%20A%3AA)%2C%20as%20not%20all%20cells%20are%20included%2C%20only%20those%20starting%20from%20a%20predefined%20one.%3C%2FP%3E%3CP%3EA%20handy%20way%20is%20an%20addres%20from%20that%20cell%20to%20the%20last%20one%20in%20the%20column%20or%20row%20(for%20instance%20A5%3AA1048576).%20However%2C%20the%20last%20cell%20appears%20no%20special%20in%20the%20address%2C%20it%20just%20happens%20to%20be%20last.%20I%20tried%20to%20add%2Fdelete%20a%20row%2Fcolumn%20(to%20simulate%20future%20maintenance%20or%20improvement)%2C%20and%20the%20address%20reimains%20all%20right%2C%20that%20is%20to%20the%20last%20cell.%20Maybe%20Excel%20recognises%20that%20the%20address%20is%20to%20the%20last%20cell%20and%20then%20preserves%20this.%20In%20all%20cases%2C%20this%20way%20is%20not%20elegant%20and%20covers%20the%20programmer's%20intention.%3C%2FP%3E%3CP%3EIs%20there%20a%20special%20way%20to%20address%20the%20last%2Ffirst%20cell%20of%20a%20row%2Fcolumn%3F%20If%20such%20name%20existed%20and%20were%2C%20e.g.%2C%20%24Last%2C%20then%20addresses%20such%20as%20A5%3AA%24Last%20would%20be%20possible.%3C%2FP%3E%3CP%3EThank%20you.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2016502%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2016555%22%20slang%3D%22en-US%22%3ERe%3A%20Adrees%20to%20the%20column%2Frow%20end%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2016555%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F912128%22%20target%3D%22_blank%22%3E%40INRIM%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIf%20there%20are%20no%20blank%20cells%20between%20A5%20and%20the%20last%20used%20cell%20in%20column%20A%2C%20you%20can%20use%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3E%3DOFFSET(%24A%245%2C%200%2C%200%2C%20COUNTA(%24A%245%3A%24A%241048576)%2C%201)%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3Eto%20refer%20to%20the%20range%20from%20A5%20to%20the%20last%20used%20cell%20in%20column%20A.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EIn%20VBA%2C%20you%20can%20use%20code%20like%20this%3A%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual%22%3E%3CCODE%3E%20%20%20%20Dim%20LastRow%20As%20Long%0A%20%20%20%20Dim%20RangeA%20As%20Range%0A%20%20%20%20LastRow%20%3D%20Range(%22A%22%20%26amp%3B%20Rows.Count).End(xlUp).Row%0A%20%20%20%20If%20LastRow%20%26lt%3B%205%20Then%20LastRow%20%3D%205%0A%20%20%20%20Set%20RangeA%20%3D%20Range(%22A5%3AA%22%20%26amp%3B%20LastRow)%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2016630%22%20slang%3D%22en-US%22%3ERe%3A%20Adrees%20to%20the%20column%2Frow%20end%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2016630%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F912128%22%20target%3D%22_blank%22%3E%40INRIM%3C%2FA%3E%26nbsp%3BAs%20a%20variant%2C%20and%20since%20you%20give%20an%20example%20of%20adding%20a%20%3CEM%3Etotal%3C%2FEM%3E%20in%20the%20top%20of%20the%20column%2C%20I%20assume%20you%20are%20working%20with%20numbers.%20The%20following%20formula%20will%20sum%20the%20range%20from%20A5%20to%20the%20last%20cell%20%3CU%3Ewith%20a%20number%3C%2FU%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3E%3DSUM(%24A%245%3AINDEX(A%3AA%2CMATCH(99%5E99%2CA%3AA)))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BSimilarly%2C%20if%20you%20want%20to%20sum%20e.g.%20from%20D5%20to%20the%20last%20number%20of%20row%205%2C%20use%20this%20one%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel%22%3E%3CCODE%3E%3DSUM(%24D%245%3AINDEX(5%3A5%2CMATCH(99%5E99%2C5%3A5)))%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3BBoth%20will%20allow%20the%20range%20to%20include%20blanks.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

I would like to form an address from a cell to the end of its column or row, for instance in a total on the top of a column whose extension covers the column below in full. This is different from a full column/row address (for instance A:A), as not all cells are included, only those starting from a predefined one.

A handy way is an addres from that cell to the last one in the column or row (for instance A5:A1048576). However, the last cell appears no special in the address, it just happens to be last. I tried to add/delete a row/column (to simulate future maintenance or improvement), and the address reimains all right, that is to the last cell. Maybe Excel recognises that the address is to the last cell and then preserves this. In all cases, this way is not elegant and covers the programmer's intention.

Is there a special way to address the last/first cell of a row/column? If such name existed and were, e.g., $Last, then addresses such as A5:A$Last would be possible.

Thank you.

3 Replies

@INRIM 

If there are no blank cells between A5 and the last used cell in column A, you can use

 

=OFFSET($A$5, 0, 0, COUNTA($A$5:$A$1048576), 1)

 

to refer to the range from A5 to the last used cell in column A.

 

In VBA, you can use code like this:

 

    Dim LastRow As Long
    Dim RangeA As Range
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    If LastRow < 5 Then LastRow = 5
    Set RangeA = Range("A5:A" & LastRow)

@INRIM As a variant, and since you give an example of adding a total in the top of the column, I assume you are working with numbers. The following formula will sum the range from A5 to the last cell with a number.

 

=SUM($A$5:INDEX(A:A,MATCH(99^99,A:A)))

 

 Similarly, if you want to sum e.g. from D5 to the last number of row 5, use this one:

 

=SUM($D$5:INDEX(5:5,MATCH(99^99,5:5)))

 

 Both will allow the range to include blanks.

@Riny_van_Eekelen 

Alternatively for the last

=XMATCH(,A:A)-1

could be any values in the cells, assuming there are no blanks in between