Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Determine the last column of data
Question 1:
How can I determine the last column of data that I have imported from a text file? The line I came up with is: LastColumn = Range(Columns.Count & 1).End(xlLeft).Column But, obviously it doesn't work. Question 2: How do I increment characters? When I write a while loop looking for the first cell with no data, I need to search cells A1, B1, C1, etc. How do I increment the letter part of the cell address. -- Bryan Kelly Time is the medium we use to express out priorities. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Determine the last column of data
Bryan,
'------------------------------- Save this one for future use... LastColumn = ActiveSheet.Cells.Find(what:="*", SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column '---------------------------------------- FirstCell with no data in Row 1... Note this gives you 257 if the column is blank Dim lngNextCol as Long lngNextCol = Range("A1").End(xlToRight).Column +1 '------------------------------------------- How to increment... Take a look at loops (For/Next) in help for more information. Note that "Cells" uses row as the first argument and column as the second. Dim lngNum As Long For lngNum = 1 To Columns.Count If Len(Cells(1, lngNum)) = 0 Then MsgBox "Column " & lngNum & " in row 1 is effectively empty" Exit For End If Next 'lngNum '---------------------------------- Regards, Jim Cone San Francisco, CA "Bryan Kelly" wrote in message ... Question 1: How can I determine the last column of data that I have imported from a text file? The line I came up with is: LastColumn = Range(Columns.Count & 1).End(xlLeft).Column But, obviously it doesn't work. Question 2: How do I increment characters? When I write a while loop looking for the first cell with no data, I need to search cells A1, B1, C1, etc. How do I increment the letter part of the cell address. Bryan Kelly Time is the medium we use to express out priorities. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Determine the last column of data
Yes, I am saving those. But there are some things I don't understand.
The original LastColumn = ActiveSheet.Cells.Find(what:="*", SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column My intrepertation LastColumn = ActiveSheet.Cells.Find( ?? ).Column This seems to say, within the active sheet, look at each cells, and find a column based on the criteria presented by ??. Now I look at the ?? what : = "*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious what : = "*" is defined as find anything. I presume that this means anything except an empty cell. SearchOrder : = xlByColumns Search order is defined as by column. It there a significance to the leading x1? Does that mean row 1? SearchDirection : = xlPrevious This seems to define the search direction as previous. But previous to what? Which way is previous and which way is next? Is next the antonym of previous? In what row does this search? Thank you for your response, Bryan "Jim Cone" wrote in message ... Bryan, '------------------------------- Save this one for future use... LastColumn = ActiveSheet.Cells.Find(what:="*", SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column '---------------------------------------- FirstCell with no data in Row 1... Note this gives you 257 if the column is blank Dim lngNextCol as Long lngNextCol = Range("A1").End(xlToRight).Column +1 '------------------------------------------- How to increment... Take a look at loops (For/Next) in help for more information. Note that "Cells" uses row as the first argument and column as the second. Dim lngNum As Long For lngNum = 1 To Columns.Count If Len(Cells(1, lngNum)) = 0 Then MsgBox "Column " & lngNum & " in row 1 is effectively empty" Exit For End If Next 'lngNum '---------------------------------- Regards, Jim Cone San Francisco, CA "Bryan Kelly" wrote in message ... Question 1: How can I determine the last column of data that I have imported from a text file? The line I came up with is: LastColumn = Range(Columns.Count & 1).End(xlLeft).Column But, obviously it doesn't work. Question 2: How do I increment characters? When I write a while loop looking for the first cell with no data, I need to search cells A1, B1, C1, etc. How do I increment the letter part of the cell address. Bryan Kelly Time is the medium we use to express out priorities. |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Determine the last column of data
Bryan,
It finds the last column (the right most column) with any kind of entry it it. It starts at the upper left cell of the specified area - in this case "Cells" which is cell A1. It starts looking in the "xlPrevious" direction (to the left) and since there are no columns to the left it actually goes back around and starts at the last column (Column IV). It looks down (or maybe up) each column looking for any data and stops at the first column with anything in it. xlPrevious, xlNext, xlByColumns and xlByRows are built in predefined "constants" in Excel. Look in the VBA help under "Find Method" for more information, it will actually make sense after you read it a few times and try out the various options. Hope this helps. Regards, Jim Cone San Francisco, CA "Bryan Kelly" wrote in message om... Yes, I am saving those. But there are some things I don't understand. The original LastColumn = ActiveSheet.Cells.Find(what:="*", SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column My intrepertation LastColumn = ActiveSheet.Cells.Find( ?? ).Column This seems to say, within the active sheet, look at each cells, and find a column based on the criteria presented by ??. Now I look at the ?? what : = "*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious what : = "*" is defined as find anything. I presume that this means anything except an empty cell. SearchOrder : = xlByColumns Search order is defined as by column. It there a significance to the leading x1? Does that mean row 1? SearchDirection : = xlPrevious This seems to define the search direction as previous. But previous to what? Which way is previous and which way is next? Is next the antonym of previous? In what row does this search? Thank you for your response, Bryan "Jim Cone" wrote in message ... Bryan, '------------------------------- Save this one for future use... LastColumn = ActiveSheet.Cells.Find(what:="*", SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column '---------------------------------------- FirstCell with no data in Row 1... Note this gives you 257 if the column is blank Dim lngNextCol as Long lngNextCol = Range("A1").End(xlToRight).Column +1 '------------------------------------------- How to increment... Take a look at loops (For/Next) in help for more information. Note that "Cells" uses row as the first argument and column as the second. Dim lngNum As Long For lngNum = 1 To Columns.Count If Len(Cells(1, lngNum)) = 0 Then MsgBox "Column " & lngNum & " in row 1 is effectively empty" Exit For End If Next 'lngNum '---------------------------------- Regards, Jim Cone San Francisco, CA "Bryan Kelly" wrote in message ... Question 1: How can I determine the last column of data that I have imported from a text file? The line I came up with is: LastColumn = Range(Columns.Count & 1).End(xlLeft).Column But, obviously it doesn't work. Question 2: How do I increment characters? When I write a while loop looking for the first cell with no data, I need to search cells A1, B1, C1, etc. How do I increment the letter part of the cell address. Bryan Kelly Time is the medium we use to express out priorities. |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Determine the last column of data
Hope this helps.
Yes, as a mater of fact it does. Thanks for your time, Bryan "Jim Cone" wrote in message ... Bryan, It finds the last column (the right most column) with any kind of entry it it. It starts at the upper left cell of the specified area - in this case "Cells" which is cell A1. It starts looking in the "xlPrevious" direction (to the left) and since there are no columns to the left it actually goes back around and starts at the last column (Column IV). It looks down (or maybe up) each column looking for any data and stops at the first column with anything in it. xlPrevious, xlNext, xlByColumns and xlByRows are built in predefined "constants" in Excel. Look in the VBA help under "Find Method" for more information, it will actually make sense after you read it a few times and try out the various options. Hope this helps. Regards, Jim Cone San Francisco, CA "Bryan Kelly" wrote in message om... Yes, I am saving those. But there are some things I don't understand. The original LastColumn = ActiveSheet.Cells.Find(what:="*", SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column My intrepertation LastColumn = ActiveSheet.Cells.Find( ?? ).Column This seems to say, within the active sheet, look at each cells, and find a column based on the criteria presented by ??. Now I look at the ?? what : = "*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious what : = "*" is defined as find anything. I presume that this means anything except an empty cell. SearchOrder : = xlByColumns Search order is defined as by column. It there a significance to the leading x1? Does that mean row 1? SearchDirection : = xlPrevious This seems to define the search direction as previous. But previous to what? Which way is previous and which way is next? Is next the antonym of previous? In what row does this search? Thank you for your response, Bryan "Jim Cone" wrote in message ... Bryan, '------------------------------- Save this one for future use... LastColumn = ActiveSheet.Cells.Find(what:="*", SearchOrder:=xlByColumns, _ SearchDirection:=xlPrevious).Column '---------------------------------------- FirstCell with no data in Row 1... Note this gives you 257 if the column is blank Dim lngNextCol as Long lngNextCol = Range("A1").End(xlToRight).Column +1 '------------------------------------------- How to increment... Take a look at loops (For/Next) in help for more information. Note that "Cells" uses row as the first argument and column as the second. Dim lngNum As Long For lngNum = 1 To Columns.Count If Len(Cells(1, lngNum)) = 0 Then MsgBox "Column " & lngNum & " in row 1 is effectively empty" Exit For End If Next 'lngNum '---------------------------------- Regards, Jim Cone San Francisco, CA "Bryan Kelly" wrote in message ... Question 1: How can I determine the last column of data that I have imported from a text file? The line I came up with is: LastColumn = Range(Columns.Count & 1).End(xlLeft).Column But, obviously it doesn't work. Question 2: How do I increment characters? When I write a while loop looking for the first cell with no data, I need to search cells A1, B1, C1, etc. How do I increment the letter part of the cell address. Bryan Kelly Time is the medium we use to express out priorities. |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Determine the last column of data
How do I increment characters? How to increment... Take a look at loops (For/Next) in help for more information. Note that "Cells" uses row as the first argument and column as the second. Dim lngNum As Long For lngNum = 1 To Columns.Count If Len(Cells(1, lngNum)) = 0 Then MsgBox "Column " & lngNum & " in row 1 is effectively empty" Exit For End If Next 'lngNum The reason I asked the question the way I did was that in my very limited experience, a cell is identified in the manner LN or letter followed by number, which is column first then row. I just looked at Range and it is described in terms of A1 through A5. Why is Cells done different? Or is Range the different one? So this seems to side step the question of incrementing letters to build a Range identifier. Can I (Should I) completely avoid that problem by using the Cells objects/methods? Bryan <SNIP |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Determine the last column of data
Bryan,
You asked "Why is Cells done different?" I wondered the same thing when I got started with VBA. There are some logical inconsistencies in VBA that may make sense behind the covers. May own cynical view is that many are done on purpose for questionable reasons. Enough of that, Range("B5") is a slightly more efficient way to refer to a cell than Cells(5,2). However, trying to increment the characters in a range callout is exceedingly slow. You would have to use something like Range("B" & N) and that is very inefficient. It is much faster to use the cells method....Cells(N, 2). You will not see the difference when doing just a few cells, but with programs running large loops you could be talking several minutes versus a few seconds. Regards, Jim Cone San Francisco, CA "Bryan Kelly" wrote in message m... How do I increment characters? How to increment... Take a look at loops (For/Next) in help for more information. Note that "Cells" uses row as the first argument and column as the second. Dim lngNum As Long For lngNum = 1 To Columns.Count If Len(Cells(1, lngNum)) = 0 Then MsgBox "Column " & lngNum & " in row 1 is effectively empty" Exit For End If Next 'lngNum The reason I asked the question the way I did was that in my very limited experience, a cell is identified in the manner LN or letter followed by number, which is column first then row. I just looked at Range and it is described in terms of A1 through A5. Why is Cells done different? Or is Range the different one? So this seems to side step the question of incrementing letters to build a Range identifier. Can I (Should I) completely avoid that problem by using the Cells objects/methods? Bryan <SNIP |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
Determine the last column of data
Yes Jim,
That does help. Thank you, Bryan "Jim Cone" wrote in message ... Bryan, It finds the last column (the right most column) with any kind of entry it it. It starts at the upper left cell of the specified area - in this case "Cells" which is cell A1. It starts looking in the "xlPrevious" direction (to the left) and since there are no columns to the left it actually goes back around and starts at the last column (Column IV). It looks down (or maybe up) each column looking for any data and stops at the first column with anything in it. xlPrevious, xlNext, xlByColumns and xlByRows are built in predefined "constants" in Excel. Look in the VBA help under "Find Method" for more information, it will actually make sense after you read it a few times and try out the various options. Hope this helps. Regards, Jim Cone San Francisco, CA <SNIP |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
Determine a result of one column based on conditions in two column | Excel Discussion (Misc queries) | |||
How do I determine the last cell in a Column | Excel Discussion (Misc queries) | |||
delete duplicate record but only determine 1 column data | Excel Worksheet Functions | |||
Determine if Value in column A exists in Column B | Excel Discussion (Misc queries) | |||
formula to determine the first column containing any data | Excel Worksheet Functions |