Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 852
Default Columns I to Q range variable definition

What's a better way to capture the range of Columns I:Q down to last row the with an entry.

I don't know which column will have the longest list of data.

I use select here just to verify the range on the sheet, I will set the range to a range object when I find the better way.

I understand xlCellTypeLastCell is only reduced by deleting the cells. The range may be 1400 rows or only 11 as an example.

When it is only 11, I don't want my Find code to have to search all the wasted unused range.

Thanks,
Howard


Sub aMethod()
Dim WS As Worksheet
Dim LastCell As Range
Dim lcCel As Long

Set LastCell = Range("I:Q").SpecialCells(xlCellTypeLastCell)
lcCel = LastCell.Row
Range(Cells(3, 9), Cells(lcCel, 17)).Select
End Sub
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Columns I to Q range variable definition

Hi Howard,

Am Fri, 26 Feb 2016 03:41:50 -0800 (PST) schrieb L. Howard:

What's a better way to capture the range of Columns I:Q down to last row the with an entry.

I don't know which column will have the longest list of data.

I use select here just to verify the range on the sheet, I will set the range to a range object when I find the better way.

I understand xlCellTypeLastCell is only reduced by deleting the cells. The range may be 1400 rows or only 11 as an example.


xlCellTypeLastCell is not reliable. If you have formats downwards the
existing data you will get a wrong range.
I would check the last cell of I and J and use the maximum:

LRow1 = Cells(Rows.Count, "I").End(xlUp).Row
LRow2 = Cells(Rows.Count, "J").End(xlUp).Row

Set myRng = Range("I1:J" & Application.Max(LRow1, LRow2))


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 852
Default Columns I to Q range variable definition


xlCellTypeLastCell is not reliable. If you have formats downwards the
existing data you will get a wrong range.
I would check the last cell of I and J and use the maximum:

LRow1 = Cells(Rows.Count, "I").End(xlUp).Row
LRow2 = Cells(Rows.Count, "J").End(xlUp).Row

Set myRng = Range("I1:J" & Application.Max(LRow1, LRow2))


Regards
Claus B.
--


Hi Claus,

I assume "J" is a typo, I need

myRng = Range("I1:Q" & Application.Max(LRow1, LRow2))

However, if any column between "I" and "Q" has more rows than "I" or "Q" then there will be some data not included in myRng.

I need something like this,

RNofLongCol = (Longest Column from I to Q).Row

Set myRng = Range("I1:Q" & RNofLongCol))

Howard




  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Columns I to Q range variable definition

Hi Howard,

Am Fri, 26 Feb 2016 08:17:58 -0800 (PST) schrieb L. Howard:

I assume "J" is a typo, I need

myRng = Range("I1:Q" & Application.Max(LRow1, LRow2))


sorry, my bad. I didn't wear my glasses.
Try:

Sub Test()
Dim varRows(8) As Variant
Dim i As Long
Dim myRng As Range

For i = Columns("I").Column To Columns("Q").Column
varRows(i - 9) = Cells(Rows.Count, i).End(xlUp).Row
Next
Set myRng = Range("I1:Q" & Application.Max(varRows))
'myRng.Select
End Sub


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,872
Default Columns I to Q range variable definition

Hi Howard,

Am Fri, 26 Feb 2016 17:25:57 +0100 schrieb Claus Busch:

Sub Test()
Dim varRows(8) As Variant


or if you don't want to count the columns first:

Sub Test()
Dim varRows() As Variant
Dim i As Long, myFirst As Long, myLast As Long
Dim myRng As Range

myFirst = Columns("I").Column
myLast = Columns("Q").Column
ReDim Preserve varRows(myLast - myFirst)

For i = myFirst To myLast
varRows(i - myFirst) = Cells(Rows.Count, i).End(xlUp).Row
Next
Set myRng = Range("I1:Q" & Application.Max(varRows))
'myRng.Select
End Sub


Regards
Claus B.
--
Vista Ultimate / Windows7
Office 2007 Ultimate / 2010 Professional


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 852
Default Columns I to Q range variable definition

Hi Claus,

Both test codes work well.

Just exactly what I was looking for.

Thanks much.

Howard

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
VBA variable definition help needed. jenn k New Users to Excel 4 September 5th 08 08:55 PM
Boolean Variable Definition James Excel Programming 1 August 14th 08 06:42 PM
Excel question with variable definition Yong Kim Excel Programming 2 March 4th 05 05:47 PM
[variable] definition Veena Excel Programming 3 August 19th 04 01:23 PM
variable range definition jmp Excel Programming 2 May 6th 04 11:06 PM


All times are GMT +1. The time now is 09:49 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"