View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
ryguy7272 ryguy7272 is offline
external usenet poster
 
Posts: 2,836
Default finding last column not always working correctly

You must have some formatting or some kind of 'invisible' data in those
columns that get passed over. In one of those troublesome sheets, click a
cell, and hit Ctrl+End on the keyboard. Do you go beyond the column that you
expect to stop at? If so, click on the letter to the right of the last
column you want to stop at, Ctrl+Shift+RightArrowKey, then right-click and
click Delete. Save the workbook, hit Ctrl+End, now you should stop at the
column that you expect to stop at.

Give it a try and post back with your findings.

HTH,
Ryan---

--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"Sheela" wrote:


I have a macro to find a last column in all the worksheets in a workbook.
In some sheets it is correctly selecting the last column, but in some sheets
it is passing the last column and selecting some other column on way right to
it.
I tried to use the following two methods, both are working the same way.

Could someone figure out the correct way to find the last column?

Public Sub test()

Dim ws As Worksheet
Dim Lastrow, Lastcolumn As Long

On Error Resume Next

For Each ws In ActiveWorkbook.Worksheets

With ws

Lastrow = .Range("A" & Rows.Count).End(xlUp).Row
'Lastcolumn = ActiveCell.SpecialCells(xlCellTypeLastCell).Column
Lastcolumn = ActiveSheet.Cells(2, Columns.Count).End(xlToLeft).Column

.Cells(Lastrow, 1).Copy
.Range(.Cells(1, Lastcolumn + 1), .Cells(Lastrow, Lastcolumn +
1)).PasteSpecial Paste:=xlPasteAll, Transpose:=True

End With

Next ws
On Error GoTo 0

End Sub