View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Bryan Kelly Bryan Kelly is offline
external usenet poster
 
Posts: 25
Default 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.