I have to deal with CSV files output by some commercial software which
is not properly written. It gives long lists in which some of the rows
have more columns than others. These lists can be thousands of lines
long, so I wrote this macro, which moves the offending rows back to
where they should be.

First, I manually open up enough columns so that the rows can be moved
to the left. Then I establish which is the rightmost column containing
data, and I go to Row 1 and run the macro.

It works fine, except that it crashes at row 65535, instead of simply
ending because it has dealt with every cell in the column. I have
highlighted where it crashes, and would appreciate anyone who knows why
this happens.

Sub Line_up_cols()
'
' Keyboard Shortcut: Ctrl+Shift+L
'
Dim myRange As Range
Dim Cell As Range
For Each Cell In ActiveSheet.Columns()
Selection.End(xlDown).Select
Selection.End(xlToLeft).Select
Set myRange = ActiveCell
Range(Selection, Selection.End(xlToRight)).Select
Selection.Cut
myRange.Offset(0, -1).Select <<<<---- Crashes here
ActiveSheet.Paste
ActiveCell.Offset(0, 1).Select
Selection.End(xlToRight).Select
ActiveCell.Offset(0, 1).Select
Next
End Sub

Re: Why does this macro crash? by Bob

Bob
Sun May 11 05:07:50 CDT 2008

Presumably because you are in column A, and there is nothing to the left.

--
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)

"Brian" <bxxcfilm@nildram.co.uk> wrote in message
news:eGQh4r0sIHA.552@TK2MSFTNGP06.phx.gbl...
>I have to deal with CSV files output by some commercial software which is
>not properly written. It gives long lists in which some of the rows have
>more columns than others. These lists can be thousands of lines long, so I
>wrote this macro, which moves the offending rows back to where they should
>be.
>
> First, I manually open up enough columns so that the rows can be moved to
> the left. Then I establish which is the rightmost column containing data,
> and I go to Row 1 and run the macro.
>
> It works fine, except that it crashes at row 65535, instead of simply
> ending because it has dealt with every cell in the column. I have
> highlighted where it crashes, and would appreciate anyone who knows why
> this happens.
>
> Sub Line_up_cols()
> '
> ' Keyboard Shortcut: Ctrl+Shift+L
> '
> Dim myRange As Range
> Dim Cell As Range
> For Each Cell In ActiveSheet.Columns()
> Selection.End(xlDown).Select
> Selection.End(xlToLeft).Select
> Set myRange = ActiveCell
> Range(Selection, Selection.End(xlToRight)).Select
> Selection.Cut
> myRange.Offset(0, -1).Select <<<<---- Crashes here
> ActiveSheet.Paste
> ActiveCell.Offset(0, 1).Select
> Selection.End(xlToRight).Select
> ActiveCell.Offset(0, 1).Select
> Next
> End Sub



Re: Why does this macro crash? by Brian

Brian
Sun May 11 05:23:41 CDT 2008

Bob Phillips wrote:
> Presumably because you are in column A, and there is nothing to the left.
>
No, I open up some columns first and work from the right, so that there
is space to move cells into.


Re: Why does this macro crash? by Brian

Brian
Sun May 11 05:30:48 CDT 2008

Bob Phillips wrote:
> Presumably because you are in column A, and there is nothing to the left.
>
BTW, I'm using XL2000 and XP SP2, if that makes any difference.


Re: Why does this macro crash? by Ron

Ron
Sun May 11 05:42:32 CDT 2008

On Sun, 11 May 2008 10:39:51 +0100, Brian <bxxcfilm@nildram.co.uk> wrote:

>I have to deal with CSV files output by some commercial software which
>is not properly written. It gives long lists in which some of the rows
>have more columns than others. These lists can be thousands of lines
>long, so I wrote this macro, which moves the offending rows back to
>where they should be.
>
>First, I manually open up enough columns so that the rows can be moved
>to the left. Then I establish which is the rightmost column containing
>data, and I go to Row 1 and run the macro.
>
>It works fine, except that it crashes at row 65535, instead of simply
>ending because it has dealt with every cell in the column. I have
>highlighted where it crashes, and would appreciate anyone who knows why
>this happens.
>
>Sub Line_up_cols()
>'
>' Keyboard Shortcut: Ctrl+Shift+L
>'
>Dim myRange As Range
>Dim Cell As Range
>For Each Cell In ActiveSheet.Columns()
> Selection.End(xlDown).Select
> Selection.End(xlToLeft).Select
> Set myRange = ActiveCell
> Range(Selection, Selection.End(xlToRight)).Select
> Selection.Cut
> myRange.Offset(0, -1).Select <<<<---- Crashes here
> ActiveSheet.Paste
> ActiveCell.Offset(0, 1).Select
> Selection.End(xlToRight).Select
> ActiveCell.Offset(0, 1).Select
>Next
>End Sub

What is the value of myRange.Address at the time of the crash?
--ron

RE: Why does this macro crash? by FSt1

FSt1
Sun May 11 10:35:01 CDT 2008

hi
here is my guess.
your code goes through all cells in the column ie 65535 via for each....
when at the last cell, you try to select......myRange.Offset(0,
-1).Select....which is techniquelly off the sheet because you seem to be
coming from the bottom up. seems like you should be getting a "script out of
range" error.
i thing ron is suspecting the same thing i am.

regards
FSt1

"Brian" wrote:

> I have to deal with CSV files output by some commercial software which
> is not properly written. It gives long lists in which some of the rows
> have more columns than others. These lists can be thousands of lines
> long, so I wrote this macro, which moves the offending rows back to
> where they should be.
>
> First, I manually open up enough columns so that the rows can be moved
> to the left. Then I establish which is the rightmost column containing
> data, and I go to Row 1 and run the macro.
>
> It works fine, except that it crashes at row 65535, instead of simply
> ending because it has dealt with every cell in the column. I have
> highlighted where it crashes, and would appreciate anyone who knows why
> this happens.
>
> Sub Line_up_cols()
> '
> ' Keyboard Shortcut: Ctrl+Shift+L
> '
> Dim myRange As Range
> Dim Cell As Range
> For Each Cell In ActiveSheet.Columns()
> Selection.End(xlDown).Select
> Selection.End(xlToLeft).Select
> Set myRange = ActiveCell
> Range(Selection, Selection.End(xlToRight)).Select
> Selection.Cut
> myRange.Offset(0, -1).Select <<<<---- Crashes here
> ActiveSheet.Paste
> ActiveCell.Offset(0, 1).Select
> Selection.End(xlToRight).Select
> ActiveCell.Offset(0, 1).Select
> Next
> End Sub
>

Re: Why does this macro crash? by Per

Per
Sun May 11 14:46:27 CDT 2008

On 11 Maj, 17:35, FSt1 <F...@discussions.microsoft.com> wrote:
> hi
> here is my guess.
> your code goes through all cells in the column ie 65535 via for each....
> when at the last cell, you try to select......myRange.Offset(0,
> -1).Select....which is techniquelly off the sheet because you seem to be
> coming from the bottom up. seems like you should be getting a "script out =
of
> range" error.
> i thing ron is suspecting the same thing i am.
>
> regards
> FSt1
>
>
>
> "Brian" wrote:
> > I have to deal with CSV files output by some commercial software which
> > is not properly written. It gives long lists in which some of the rows
> > have more columns than others. These lists can be thousands of lines
> > long, so I wrote this macro, which moves the offending rows back to
> > where they should be.
>
> > First, I manually open up enough columns so that the rows can be moved
> > to the left. Then I establish which is the rightmost column containing
> > data, and I go to Row 1 and run the macro.
>
> > It works fine, except that it crashes at row 65535, instead of simply
> > ending because it has dealt with every cell in the column. I have
> > highlighted where it crashes, and would appreciate anyone who knows why
> > this happens.
>
> > Sub Line_up_cols()
> > '
> > ' Keyboard Shortcut: Ctrl+Shift+L
> > '
> > Dim myRange As Range
> > Dim Cell As Range
> > For Each Cell In ActiveSheet.Columns()
> > =A0 =A0 =A0Selection.End(xlDown).Select
> > =A0 =A0 =A0Selection.End(xlToLeft).Select
> > =A0 =A0 =A0Set myRange =3D ActiveCell
> > =A0 =A0 =A0Range(Selection, Selection.End(xlToRight)).Select
> > =A0 =A0 =A0Selection.Cut
> > =A0 =A0 =A0myRange.Offset(0, -1).Select =A0 =A0 =A0 =A0 =A0 <<<<---- Cra=
shes here
> > =A0 =A0 =A0ActiveSheet.Paste
> > =A0 =A0 =A0ActiveCell.Offset(0, 1).Select
> > =A0 =A0 =A0Selection.End(xlToRight).Select
> > =A0 =A0 =A0ActiveCell.Offset(0, 1).Select
> > Next
> > End Sub- Skjul tekst i anf=F8rselstegn -
>
> - Vis tekst i anf=F8rselstegn -

Hi Brian

Try if this macro does what you need.

Sub ColToLeft()
Dim MyRange As Range
Set MyRange =3D Range("A1").CurrentRegion
rCount =3D MyRange.Rows.Count
For r =3D 1 To rCount
If Cells(r, 1).Value =3D "" Then
Range(Cells(r, 1).End(xlToRight), Cells(r,
1).End(xlToRight).End(xlToRight)).Cut Cells(r, 1)
End If
Next
End Sub

Regards,
Per