Office

     Go!
Prospero Blocks


 

Chat Center

Board Folders

MS Office - General: 4351 msgs in 879 dscns, Latest: Nov-13 MS Office - Ge...
4351 msgs in 879 dscns
Latest: Nov-13
Outlook: 10259 msgs in 1962 dscns, Latest: 8:53 PMOutlook
10259 msgs in 1962 dscns
Latest: 8:53 PM
Word & Word VBA: 9368 msgs in 1767 dscns, Latest: Nov-18 Word & Word VBA
9368 msgs in 1767 dscns
Latest: Nov-18
Excel & Excel VBA: 12847 msgs in 2265 dscns, Latest: Nov-17 Excel & Excel VBA
12847 msgs in 2265 dscns
Latest: Nov-17
Powerpoint: 750 msgs in 196 dscns, Latest: Sep-18 Powerpoint
750 msgs in 196 dscns
Latest: Sep-18
Access & Access VBA: 5436 msgs in 1214 dscns, Latest: Nov-6 Access & Acces...
5436 msgs in 1214 dscns
Latest: Nov-6
Graphic Tools: 130 msgs in 34 dscns, Latest: Aug-25 Graphic Tools
130 msgs in 34 dscns
Latest: Aug-25
Publisher: 254 msgs in 47 dscns, Latest: Oct-18 Publisher
254 msgs in 47 dscns
Latest: Oct-18
Works Suite: 360 msgs in 73 dscns, Latest: May-26 Works Suite
360 msgs in 73 dscns
Latest: May-26
Money & MSTaxSaver: 97 msgs in 27 dscns, Latest: 12/25/08 Money & MSTaxS...
97 msgs in 27 dscns
Latest: 12/25/08
MS Maps & Trips: 78 msgs in 15 dscns, Latest: Oct-23 MS Maps & Trips
78 msgs in 15 dscns
Latest: Oct-23
FrontPage: 1259 msgs in 224 dscns, Latest: Oct-15 FrontPage
1259 msgs in 224 dscns
Latest: Oct-15
Other Office Products: 325 msgs in 82 dscns, Latest: Oct-29 Other Office P...
325 msgs in 82 dscns
Latest: Oct-29
OneNote: 144 msgs in 19 dscns, Latest: Nov-11 OneNote
144 msgs in 19 dscns
Latest: Nov-11
Message Area
Excel & Excel VBA

Find last ROW / Column

 Subscribe SubscribeGet a printer-friendly version of this discussion Print Discussion 

#1 of 5

     Posted Nov-5 5:28 PM   
A1pilot
 
From  A1pilot  Posts 195  Last Nov-11
To  All      [Msg # 15896.1 ]    

Is it possible to find the number of rows that have data in them and then use that number to create a DO WHILE loop using that number?

 

This statement will find the range of the cells, but can I used the last part of that formula to TELL me how many actual rows there are and then convert that into a number (ACTROWNUM)I can use?

 

For lRow = 1 To Sheets("P_Days").Cells(Rows.Count, 1).End(xlUp).Row

 

My goal is to use a loop that says something like this:

 

Do while iNumRows <= ActRowNum

 

Can Excel do this with Columns as well? I.e. Find the actual number of columns with data in them?

Can Excel convert that number to something I can create a DO Loop with? Same principle as above – go until the last column of data is hit.

 

XlLeft doesn’t seem to work or I am using it incorrectly but I can use CTRL Left Arrow to move to the last Column with data it so I assume there is an Excel command somewhere.

 

Richard Hughes

A1Pilot@Flica.net

 OptionsReply to this Message Reply

#2 of 5

     Posted Nov-5 6:51 PM   
Bill Manville
 
From  Bill Manville  Posts 4150  Last 4:00 PM
To  A1pilot      [Msg # 15896.2 Message 15896.2 replying to 15896.1 15896.1 ]    

This statement will find the range of the cells, but can I used the last part of that formula to TELL me how many actual rows there are and then convert that into a number (ACTROWNUM)I can use?

 For lRow = 1 To Sheets("P_Days").Cells(Rows.Count, 1).End(xlUp).Row

You mean like
  ActRowNum = Sheets("P_Days").Cells(Rows.Count, 1).End(xlUp).Row

I don't see that a Do While loop has any advantage over the For loop if you are incrementing iNumRows by 1 within the loop.

Can Excel do this with Columns as well? I.e. Find the actual number of columns with data in them?

Of course.  The equivalent would be

  ActColNum = Sheets("P_Days").Cells(1, Columns.Count).End(xlToLeft).Row 

 

If the data is a contiguous table starting at A1 then I would do it this way:
With Sheets("P_Days").Range("A1").CurrentRegion
  ActRowNum = .Rows.Count
  ActColNum = .Columns.Count
End With

 

 

Bill Manville
Excel MVP/TA
Oxford, England

 OptionsReply to this Message Reply

#3 of 5

     Posted Nov-5 9:10 PM   
A1pilot
 
From  A1pilot  Posts 195  Last Nov-11
To  Bill Manville      [Msg # 15896.3 Message 15896.3 replying to 15896.2 15896.2 ]    

Hi Bill

I think I can see my errors here.

I left teh FOR in front of the statement. I'm not trying to define the range by setting the number. I think I got it.

Syntax is everything. Yup I forgot the TO in the xlTOleft. I'm sure there is a reason why there is a TO there but I don't know why.

Which do you think is better the DO While or the FOR loop. Yes I will be increasing by 1 everytime the code gets to the bottom of the row. I don't have a favorite.. it's just which ever I can get to work at the time.

Unfortunately the data columns are NOT continious, there are blank columns in between the columns with data. So I will have to test for a blank cell and move to the next column if it find the blank.

Thanks again for your help you have saved my butt on more than one occasion.

Richard Hughes
A1pilot@flica.net

 OptionsReply to this Message Reply

#4 of 5

     Posted Nov-5 10:53 PM   
4merCL
 
From  4merCL  Posts 49  Last 9:43 PM
To  A1pilot      [Msg # 15896.4 Message 15896.4 replying to 15896.3 15896.3 ]    

>>  Unfortunately the data columns are NOT continious, there are blank columns in between the columns with data. So I will have to test for a blank cell and move to the next column if it find the blank.  <<

PMFJI, but is it assured that in every non-blank column there is data in every row? 

If so, then, theoretically, only one cell in each column need be tested to identify the column as either blank or data laden. Your comment above is suggestive on this point but I'm not certain it is definitive.

Aside from the foregoing question, should it be desirable to make cell tests in the interest of data integrity? IOW, while it may be the case that a column might have otherwise good data, could some data input or entry error result in a blank or otherwise corrupted data point in one of its cells ? 

Best regards,  4merCL

 OptionsReply to this Message Reply

#5 of 5

     Posted Nov-6 2:46 AM   
Bill Manville
 
From  Bill Manville  Posts 4150  Last 4:00 PM
To  A1pilot      [Msg # 15896.5 Message 15896.5 replying to 15896.3 15896.3 ]    

Which do you think is better the DO While or the FOR loop. Yes I will be increasing by 1 everytime the code gets to the bottom of the row.

I would slightly prefer the For loop in that case (because it is designed for that circumstance).
For one thing, it is easy to forget to increment the loop counter in a Do loop and you end up with an endless loop.

I'm sure there is a reason why there is a TO there but I don't know why.

xlLeft is a constant (-4131) used to select a left-hand position for things (a chart legend, a summary column or, in earlier versions, a left-hand border).
xlToLeft is a constant (-4159) used with End to mean a direction of travel and with Delete to indicate a shift direction.

I guess the people who chose those values about 18 years ago didn't think that they were sufficiently similar that using the same name (and therefore constant value) was appropriate, and from the numbers I would think that the programmer who defined xlLeft got there first.

Bill Manville
Excel MVP/TA
Oxford, England


Edited Nov-6   by  Bill Manville
 OptionsReply to this Message Reply
 Subscribe SubscribeGet a printer-friendly version of this discussion Print Discussion 
Excel & Excel VBA

Find last ROW / Column

  
 
     

Welcome, Guest

  • Post a message
  • New messages to you
  • Log in

Start Search
Advanced Search

Prospero Blocks
 
 
 
Special Offers
 
 
 

Finding People

 
 
 

Cool Clicks!

 
 
 
© 2009 CompuServe Interactive Services, Inc. All rights reserved.

Legal Notices | Privacy Policy