PC Review


Reply
Thread Tools Rate Thread

deleting rows with VB

 
 
mantrid
Guest
Posts: n/a
 
      16th Apr 2007
Hello

Can anyone tell ne the correct VB syntax loop through the rows in the active
worksheet and delete any row that has a cell in a given coulumn that does
not = null
I have the following

For Each c In ActiveSheet.Range("k3:k33").Cells
If c.Value Is Not Null Then
ActiveSheet.Row.Delete
End If

but it is not working
Any ideas please
Ian


 
Reply With Quote
 
 
 
 
Norman Jones
Guest
Posts: n/a
 
      16th Apr 2007
Hi Mantrid,

To delete rows, you shoulsd either proceed bottom to top or,
alternatively, delete the rows 'en masse'.

Try something like:

'================>>
Public Sub Tester()
Dim WB As Workbook
Dim SH As Worksheet
Dim Rng As Range
Dim rCell As Range
Dim delRng As Range
Dim CalcMode As Long
Dim ViewMode As Long

Set WB = ActiveWorkbook '<<===== CHANGE
Set SH = WB.Sheets("Sheet1") '<<===== CHANGE
Set Rng = SH.Range("K3:K33") '<<===== CHANGE

On Error GoTo XIT

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

For Each rCell In Rng.Cells
If IsEmpty(rCell.Value) Then
If delRng Is Nothing Then
Set delRng = rCell
Else
Set delRng = Union(rCell, delRng)
End If
End If
Next rCell

If Not delRng Is Nothing Then
delRng.EntireRow.Delete
End If

XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With
End Sub
'<<================

I have assumed that you wish to delete empty rows.


---
Regards,
Norman



"mantrid" <(E-Mail Removed)> wrote in message
news:OAQUh.6126$(E-Mail Removed)...
> Hello
>
> Can anyone tell ne the correct VB syntax loop through the rows in the
> active
> worksheet and delete any row that has a cell in a given coulumn that does
> not = null
> I have the following
>
> For Each c In ActiveSheet.Range("k3:k33").Cells
> If c.Value Is Not Null Then
> ActiveSheet.Row.Delete
> End If
>
> but it is not working
> Any ideas please
> Ian
>
>



 
Reply With Quote
 
mantrid
Guest
Posts: n/a
 
      17th Apr 2007

"Norman Jones" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi Mantrid,
>
> To delete rows, you shoulsd either proceed bottom to top or,
> alternatively, delete the rows 'en masse'.
>
> Try something like:
>
> '================>>
> Public Sub Tester()
> Dim WB As Workbook
> Dim SH As Worksheet
> Dim Rng As Range
> Dim rCell As Range
> Dim delRng As Range
> Dim CalcMode As Long
> Dim ViewMode As Long
>
> Set WB = ActiveWorkbook '<<===== CHANGE
> Set SH = WB.Sheets("Sheet1") '<<===== CHANGE
> Set Rng = SH.Range("K3:K33") '<<===== CHANGE
>
> On Error GoTo XIT
>
> With Application
> CalcMode = .Calculation
> .Calculation = xlCalculationManual
> .ScreenUpdating = False
> End With
>
> For Each rCell In Rng.Cells
> If IsEmpty(rCell.Value) Then
> If delRng Is Nothing Then
> Set delRng = rCell
> Else
> Set delRng = Union(rCell, delRng)
> End If
> End If
> Next rCell
>
> If Not delRng Is Nothing Then
> delRng.EntireRow.Delete
> End If
>
> XIT:
> With Application
> .Calculation = CalcMode
> .ScreenUpdating = True
> End With
> End Sub
> '<<================
>
> I have assumed that you wish to delete empty rows.
>
>
> ---
> Regards,
> Norman
>


Thanks Norman that has given me something to try. It looks like what I need
I will give it a go.
>
> I have assumed that you wish to delete empty rows.
>


Well rows where any cell in the range K3:K33 is empty, but I think your code
is doing this? correct me if this is not right.

Ian


 
Reply With Quote
 
Norman Jones
Guest
Posts: n/a
 
      17th Apr 2007
Hi Ian

'-------------
Well rows where any cell in the range K3:K33 is empty, but I think your code
is doing this? correct me if this is not right.

'-------------

Yes.

---
Regards,
Norman


 
Reply With Quote
 
mantrid
Guest
Posts: n/a
 
      18th Apr 2007

"Norman Jones" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...
> Hi Ian
>
> '-------------
> Well rows where any cell in the range K3:K33 is empty, but I think your

code
> is doing this? correct me if this is not right.
>
> '-------------
>
> Yes.
>
> ---
> Regards,
> Norman
>


Ive looked at your code and it worked fine. I now need to change it a bit as
the rows I want to delete are NOT a definate range as in my original
posting. but can be any number. I thought the best way to takle this is to
use a do .... while .... loop incorporating a counter starting from 8 to
represent the first row looping until a number refering to a cell in column
A where it has nothing in it. This being the last row to examine for the
previous criteria we were looking for (ie a blank in column K). I have
attached my code below which is not working. Perhaps you would be kind
enough to have a look and see if you can see where I have gone wrong. Again
I relly appreciate your assistance

Thanks again
Ian
********************************************

Set WB = ActiveWorkbook
Set SH = WB.Sheets(newname)

On Error GoTo XIT

With Application
CalcMode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With

counter = 8

Do While Not IsEmpty(SH.Cells(counter, 1).Value)

If Not IsEmpty(SH.Cells(counter, 11).Value) Then
If delRng Is Nothing Then
Set delRng = SH.Cells(counter, 11)
Else
Set delRng = Union(rCell, delRng)
End If
End If

counter = counter + 1
Loop

If Not delRng Is Nothing Then
delRng.EntireRow.Delete
End If

XIT:
With Application
.Calculation = CalcMode
.ScreenUpdating = True
End With




 
Reply With Quote
 
Norman Jones
Guest
Posts: n/a
 
      18th Apr 2007
Hi Mantrid,

My code was designed to delete all rows without a
value in the range K3:K33.

I regret that you statement:

> Ive looked at your code and it worked fine. I now need to change it a bit
> as
> the rows I want to delete are NOT a definate range as in my original
> posting. but can be any number

is unclear and indicate no specific criteria.

Try explaining the range of interest and the criteria which
should govern the proposed deletion of rows.


---
Regards,
Norman


"mantrid" <(E-Mail Removed)> wrote in message
news:gXvVh.3523$(E-Mail Removed)...
>
> "Norman Jones" <(E-Mail Removed)> wrote in message
> news:%(E-Mail Removed)...
>> Hi Ian
>>
>> '-------------
>> Well rows where any cell in the range K3:K33 is empty, but I think your

> code
>> is doing this? correct me if this is not right.
>>
>> '-------------
>>
>> Yes.
>>
>> ---
>> Regards,
>> Norman
>>

>
> Ive looked at your code and it worked fine. I now need to change it a bit
> as
> the rows I want to delete are NOT a definate range as in my original
> posting. but can be any number. I thought the best way to takle this is to
> use a do .... while .... loop incorporating a counter starting from 8 to
> represent the first row looping until a number refering to a cell in
> column
> A where it has nothing in it. This being the last row to examine for the
> previous criteria we were looking for (ie a blank in column K). I have
> attached my code below which is not working. Perhaps you would be kind
> enough to have a look and see if you can see where I have gone wrong.
> Again
> I relly appreciate your assistance
>
> Thanks again
> Ian
> ********************************************
>
> Set WB = ActiveWorkbook
> Set SH = WB.Sheets(newname)
>
> On Error GoTo XIT
>
> With Application
> CalcMode = .Calculation
> .Calculation = xlCalculationManual
> .ScreenUpdating = False
> End With
>
> counter = 8
>
> Do While Not IsEmpty(SH.Cells(counter, 1).Value)
>
> If Not IsEmpty(SH.Cells(counter, 11).Value) Then
> If delRng Is Nothing Then
> Set delRng = SH.Cells(counter, 11)
> Else
> Set delRng = Union(rCell, delRng)
> End If
> End If
>
> counter = counter + 1
> Loop
>
> If Not delRng Is Nothing Then
> delRng.EntireRow.Delete
> End If
>
> XIT:
> With Application
> .Calculation = CalcMode
> .ScreenUpdating = True
> End With
>
>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
deleting blank rows for up to 60000 rows of data gbpg Microsoft Excel Programming 3 27th Dec 2009 08:37 PM
Macro for deleting rows and serialising the remaing rows Srinivasulu Bhattaram Microsoft Excel Programming 2 13th Nov 2008 01:32 PM
Macro for deleting rows and serialising the remaing rows Srinivasulu Bhattaram Microsoft Excel Worksheet Functions 1 12th Nov 2008 01:39 PM
Macro for deleting rows and serialising the remaing rows Srinivasulu Bhattaram Microsoft Excel Discussion 1 12th Nov 2008 01:32 PM
Excel 2000 VBA Deleting Rows when certain text in rows exists scain2004 Microsoft Excel New Users 1 15th Mar 2004 02:11 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:29 PM.