ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   selecting a range according to the longest column (https://www.excelbanter.com/excel-programming/369456-selecting-range-according-longest-column.html)

John Smith

selecting a range according to the longest column
 
Say, I have data in A1-A3, B1-B7, C1-C5. How do I programmatically
select A1-C7 with one command?

"range([A1],C[1].end(xldown)).select" doesn't do it.

Tom Ogilvy

selecting a range according to the longest column
 
depends on what you know about your data

Range("A1").CurrentRegion.Resize(,3).Select

if there are no completely blank rows before you get to the bottom.

Dim maxrow as long, i as long, rw as Long
maxrow = 0
for i = 1 to 3
rw = cells(rows.count,i).End(xlup).Row
if rw maxrow then maxrow = rw
Next i
Range("A1").Resize(rw,3).Select


Many might suggest

ActiveSheet.UsedRange.Select

That will often work, but there is no guarantee.


This is pretty resiliant:

Sub GetRealLastCell()
Dim RealLastRow As Long
Dim RealLastColumn As Long
On Error Resume Next
RealLastRow = _
Range("A:C").Find("*", [A1], , , xlByRows, xlPrevious).Row
RealLastColumn = _
Range("A:C").Find("*", [A1], , , xlByColumns, xlPrevious).Column
Range("A1",Cells(RealLastRow, RealLastColumn)).Select
End Sub


--
Regards,
Tom Ogilvy


"John Smith" wrote in message
...
Say, I have data in A1-A3, B1-B7, C1-C5. How do I programmatically
select A1-C7 with one command?

"range([A1],C[1].end(xldown)).select" doesn't do it.




Dana DeLouis

selecting a range according to the longest column
 
If there is no surrounding data, then one of a few ways...

[A1].CurrentRegion.Select

HTH
Dana DeLouis


John Smith wrote:
Say, I have data in A1-A3, B1-B7, C1-C5. How do I programmatically
select A1-C7 with one command?

"range([A1],C[1].end(xldown)).select" doesn't do it.



All times are GMT +1. The time now is 07:34 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com