Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 149
Default Looping through Sheet

I'm trying to start at B1, then go down until I find a cell containing
"myword" and delete the row with "myword". I'm failing bad and need help.
I've looked at many examples like below, but can't get them to run.

Sub DeleteRows()
Dim theRange As Range, nCells As Integer, I As Integer
Set theRange = Selection
nCells = theRange.Cells.Count
For I = nCells To 1 Step -1
If theRange.Cells(I).Value = "myword" Then
theRange.Cells(I).EntireRow.Delete
End If
Next
End Sub


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,337
Default Looping through Sheet

I just tested your code and it worked fine IF I selected the cells in col B
first. I would have written to automatically use the range
sub dr()
lc=cells(rows.count,"b").end(xlup).row
for i= lc to 1 step -1
if cells(i,2)="myword" then rows(i).delete
next i
end sub

Sub DeleteRows()
Dim theRange As Range, nCells As Integer, I As Integer
Set theRange = Selection
nCells = theRange.Cells.Count
For I = nCells To 1 Step -1
If theRange.Cells(I).Value = "myword" Then
theRange.Cells(I).EntireRow.Delete
End If
Next
End Sub


--
Don Guillett
SalesAid Software

"scott" wrote in message
...
I'm trying to start at B1, then go down until I find a cell containing
"myword" and delete the row with "myword". I'm failing bad and need help.
I've looked at many examples like below, but can't get them to run.

Sub DeleteRows()
Dim theRange As Range, nCells As Integer, I As Integer
Set theRange = Selection
nCells = theRange.Cells.Count
For I = nCells To 1 Step -1
If theRange.Cells(I).Value = "myword" Then
theRange.Cells(I).EntireRow.Delete
End If
Next
End Sub




  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Looping through Sheet

Hi Scott,

With the appropriate worksheet and the requisite cells pre-selected, your
code worked for me.

Avoiding selections, try:

Sub DeleteRows()
Dim theRange As Range, nCells As Integer, I As Integer
Dim lastRow As Long
With Sheets("Sheet1") '<<===== CHANGE TO SUIT
lastRow = .Cells(Rows.Count, "B").End(xlUp).Row
Set theRange = .Range("B2:B" & lastRow)
End With
nCells = theRange.Cells.Count
For I = nCells To 1 Step -1
If theRange.Cells(I).Value = "myword" Then
theRange.Cells(I).EntireRow.Delete
End If
Next
End Sub

---
Regards,
Norman



"scott" wrote in message
...
I'm trying to start at B1, then go down until I find a cell containing
"myword" and delete the row with "myword". I'm failing bad and need help.
I've looked at many examples like below, but can't get them to run.

Sub DeleteRows()
Dim theRange As Range, nCells As Integer, I As Integer
Set theRange = Selection
nCells = theRange.Cells.Count
For I = nCells To 1 Step -1
If theRange.Cells(I).Value = "myword" Then
theRange.Cells(I).EntireRow.Delete
End If
Next
End Sub



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default Looping through Sheet

What problem do you get, it works fine for me?

--

HTH

RP
(remove nothere from the email address if mailing direct)


"scott" wrote in message
...
I'm trying to start at B1, then go down until I find a cell containing
"myword" and delete the row with "myword". I'm failing bad and need help.
I've looked at many examples like below, but can't get them to run.

Sub DeleteRows()
Dim theRange As Range, nCells As Integer, I As Integer
Set theRange = Selection
nCells = theRange.Cells.Count
For I = nCells To 1 Step -1
If theRange.Cells(I).Value = "myword" Then
theRange.Cells(I).EntireRow.Delete
End If
Next
End Sub




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,302
Default Looping through Sheet

Hi Scott,

Change:

Set theRange = .Range("B2:B" & lastRow)


to

Set theRange = .Range("B1:B" & lastRow)

---
Regards,
Norman



"Norman Jones" wrote in message
...
Hi Scott,

With the appropriate worksheet and the requisite cells pre-selected, your
code worked for me.

Avoiding selections, try:

Sub DeleteRows()
Dim theRange As Range, nCells As Integer, I As Integer
Dim lastRow As Long
With Sheets("Sheet1") '<<===== CHANGE TO SUIT
lastRow = .Cells(Rows.Count, "B").End(xlUp).Row
Set theRange = .Range("B2:B" & lastRow)
End With
nCells = theRange.Cells.Count
For I = nCells To 1 Step -1
If theRange.Cells(I).Value = "myword" Then
theRange.Cells(I).EntireRow.Delete
End If
Next
End Sub

---
Regards,
Norman





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,163
Default Looping through Sheet

What problems are you having? Your code seems to work, but only if the
entire range is selected. For example, if the selection is only the cell B1,
it will only look in that one cell, but as long as you select the entire
column (or at least the portion containing "myword") it should work. But if
you want a more user-friendly version where that won't matter you could
modify your code just a bit. Note particularly the need to change nCells and
I from Integer to Long to avoid an overflow error:

Sub DeleteRows()
Dim theRange As Range, nCells As Long, I As Long
Set theRange = Selection.EntireColumn.
nCells = theRange.Cells.Count
For I = nCells To 1 Step -1
If theRange.Cells(I).Value = "myword" Then
theRange.Cells(I).EntireRow.Delete
End If
Next
End Sub



"scott" wrote:

I'm trying to start at B1, then go down until I find a cell containing
"myword" and delete the row with "myword". I'm failing bad and need help.
I've looked at many examples like below, but can't get them to run.

Sub DeleteRows()
Dim theRange As Range, nCells As Integer, I As Integer
Set theRange = Selection
nCells = theRange.Cells.Count
For I = nCells To 1 Step -1
If theRange.Cells(I).Value = "myword" Then
theRange.Cells(I).EntireRow.Delete
End If
Next
End Sub



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
Looping David T Excel Discussion (Misc queries) 2 August 30th 06 10:51 PM
looping through selected sheet tabs Gary Adamson[_2_] Excel Programming 4 December 7th 04 01:09 PM
looping to End Nabeel Moeen Excel Programming 2 February 25th 04 10:52 AM
Looping [email protected] Excel Programming 0 October 31st 03 07:47 PM
Need Looping Help [email protected] Excel Programming 2 October 29th 03 08:11 PM


All times are GMT +1. The time now is 04:07 AM.

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

About Us

"It's about Microsoft Excel"