Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Input Box Cancel Event

I'm using Excel XP. I can't seem to get the following
code to work. The following code deletes any blank row,
and then moves to the next row and checks if it's blank,
too. If it is, the row is deleted. If not, the row is
skipped, etc.

The problem I run into is that after the text that I want
has had all the blank rows removed, the code will continue
executing indefinitely. I need the code to STOP after it
has completed deleting the blank rows from my block of
text.

What I'm trying to do below is set a counter ('y') so that
if 25 consecutive rows are blank, then the code will know
that the end of the text has come and it's time to end the
macro. But the line "y = y + 1" generates the error "code
execution has been interrupted." Any thoughts?

Sub DeleteRows()
Dim y As Long
y = 0
Do While y < 25
If ActiveCell.Value = "" Then
Selection.EntireRow.Delete
y = y + 1
Else
ActiveCell.Offset(rowOffset:=1,
columnOffset:=0).Activate
y = 0
End If
Loop
End Sub

Thanks.

C.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 80
Default Input Box Cancel Event

Craig,

I suspect the problem is in your condition:

If ActiveCell.Value = "" Then


which will NOT be satisfied in an empty cell, so the True part will never be
executed! Try using the following instead:

If IsEmpty(ActiveCell) Or ActiveCell.Value = "" Then

I have included both because I believe your 'blank' cells in between the
data are indeed zero length strings (""), otherwise your code wouldn't
remove any rows as is. Including both conditions with an OR should do it.

Yet, this is not necessarily the most efficient way to do it. I would start
from the end working up, either going to the last used cell via:
ActiveCell.SpecialCells(xlLastCell).Select
ActiveCell.Offset(0,1-ActiveCell.Column).Select
(the second line will take you to column A in the last row)

or, by going to the very last row of the sheet (65536) and then finding the
first non-empty cell up:
Range("A65536").Select
ActiveCell.End(xlUp).Select

Then, I would work my way up in the same manner as you, stopping when
ActiveCell.Row =1.

Another way to make it faster would be to keep your ActiveCell constant
throughout, and work with the Offset (just remember to not advance the
Offset when a row is deleted).

HTH,
Nikos

"Craig" wrote in message
...
I'm using Excel XP. I can't seem to get the following
code to work. The following code deletes any blank row,
and then moves to the next row and checks if it's blank,
too. If it is, the row is deleted. If not, the row is
skipped, etc.

The problem I run into is that after the text that I want
has had all the blank rows removed, the code will continue
executing indefinitely. I need the code to STOP after it
has completed deleting the blank rows from my block of
text.

What I'm trying to do below is set a counter ('y') so that
if 25 consecutive rows are blank, then the code will know
that the end of the text has come and it's time to end the
macro. But the line "y = y + 1" generates the error "code
execution has been interrupted." Any thoughts?

Sub DeleteRows()
Dim y As Long
y = 0
Do While y < 25
If ActiveCell.Value = "" Then
Selection.EntireRow.Delete
y = y + 1
Else
ActiveCell.Offset(rowOffset:=1,
columnOffset:=0).Activate
y = 0
End If
Loop
End Sub

Thanks.

C.



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 26
Default Input Box Cancel Event

Hi,
If you use the below function (from Walkenbach's book) it
will give you the row number for the last row which have
any content (checking only the first column of rng). You
could then loop a definite number of times.

Function LastRow(rng As Range) As Long
Dim LastCell As Range
Set LastCell = rng.Parent.Cells(Rows.Count,
rng.Column).End(xlUp)
LastRow = LastCell.Row
If rng.Parent.Cells(Rows.Count, rng.Column) < "" Then
LastRow = Rows.Count
End Function

jacob
-----Original Message-----
I'm using Excel XP. I can't seem to get the following
code to work. The following code deletes any blank row,
and then moves to the next row and checks if it's blank,
too. If it is, the row is deleted. If not, the row is
skipped, etc.

The problem I run into is that after the text that I

want
has had all the blank rows removed, the code will

continue
executing indefinitely. I need the code to STOP after

it
has completed deleting the blank rows from my block of
text.

What I'm trying to do below is set a counter ('y') so

that
if 25 consecutive rows are blank, then the code will

know
that the end of the text has come and it's time to end

the
macro. But the line "y = y + 1" generates the

error "code
execution has been interrupted." Any thoughts?

Sub DeleteRows()
Dim y As Long
y = 0
Do While y < 25
If ActiveCell.Value = "" Then
Selection.EntireRow.Delete
y = y + 1
Else
ActiveCell.Offset(rowOffset:=1,
columnOffset:=0).Activate
y = 0
End If
Loop
End Sub

Thanks.

C.
.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 119
Default Input Box Cancel Event

Hi Craig,

When you delete a row, the row numbers of items below all change, so the
"Last Row" keeps changing. Starting from the bottom up overcomes this.The
following uses the Column of the Activecell to determine the last occupied
cell and works from there up. Note that there is no need to Select or
Activate.

Sub DelRows()
Dim LastRow, Rw, Col
Col = ActiveCell.Column
LastRow = Cells(Rows.Count, Col).End(xlUp).Row
For Rw = LastRow To 1 Step -1
If Cells(Rw, Col) = "" Then
Rows(Rw).Delete
End If
Next
End Sub

Depending on your circumstances, it may be prudent to specify the column
directly rather than rely on the selected cell. e.g. Col = 4 Results could
be wierd if a cell in the wrong column was selected


regards,
Don

"Craig" wrote in message
...
I'm using Excel XP. I can't seem to get the following
code to work. The following code deletes any blank row,
and then moves to the next row and checks if it's blank,
too. If it is, the row is deleted. If not, the row is
skipped, etc.

The problem I run into is that after the text that I want
has had all the blank rows removed, the code will continue
executing indefinitely. I need the code to STOP after it
has completed deleting the blank rows from my block of
text.

What I'm trying to do below is set a counter ('y') so that
if 25 consecutive rows are blank, then the code will know
that the end of the text has come and it's time to end the
macro. But the line "y = y + 1" generates the error "code
execution has been interrupted." Any thoughts?

Sub DeleteRows()
Dim y As Long
y = 0
Do While y < 25
If ActiveCell.Value = "" Then
Selection.EntireRow.Delete
y = y + 1
Else
ActiveCell.Offset(rowOffset:=1,
columnOffset:=0).Activate
y = 0
End If
Loop
End Sub

Thanks.

C.



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot seem to Cancel EXCEL App Right Click Event using C# Nick Biggs Excel Programming 18 October 28th 09 08:16 AM
Input Box Cancel Jase Excel Discussion (Misc queries) 2 September 15th 08 11:42 PM
Cancel sheet change event NSK Charts and Charting in Excel 1 July 17th 07 08:25 PM
cancel input ME @ Home Excel Discussion (Misc queries) 1 February 22nd 06 09:49 AM
Cancel terminate event?? Pop Excel Programming 1 May 19th 04 12:00 PM


All times are GMT +1. The time now is 11:42 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"