Wrox Programmer Forums
|
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 13th, 2004, 05:26 AM
Registered User
 
Join Date: Jun 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default looping question

hey all

I want to find a value in a worksheet and then delete 5 lines under every occurance of the value.

I have been toying with:

For Count = 1 To x

Range("A1").Select
Cells.Find(What:="Report", After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
ActiveCell.Rows("1:5").EntireRow.Select
Selection.Delete Shift:=xlUp

Next Count
End Sub

How do i find for x

Or if I use a do while loop how do i set the While condition?

Can someone please tell me how this can be achieved.
thanks



 
Old June 13th, 2004, 09:59 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 141
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to acdsky
Default

Hi

From what I could gather; in "For Count = 1 To x" x would be the number of times you expect to find the word "Report" If your code does not find the word it will give you a runtime error. So I would think that its save to assume you could loop till you get an error. What I would have done is add a On Error GOTO ERR: and the on your ERR: part exit the sub. So you could set x to any big number.

I would suggest this as a solution if you only trying to sort out one or two workbooks data but not for a module you would want to distribute. Let me know of the requirement and maybe I could come up with a better solution than this! :)

Regards
Marnus
 
Old June 14th, 2004, 02:08 AM
Registered User
 
Join Date: Jun 2004
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hey acdsky

thanks for your help. Went with your suggestion, however i would also like to know how a more "sophisticated" approach would handle it.

I would have thought that a DO While Loop would be perfect for this task. ie do while Cells.Find(What:="Report",... is not false/null

FYI - Someone on another forum came up with this solution, but i couldnt get it to work.

Sub AAA()
Dim oCell As Range
Dim sAddr As String
Set oCell = Range("IV65536")
Do
Set oCell = Cells.Find(What:="Report", _
After:=oCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False)
Debug.Print oCell.Address
If Not oCell Is Nothing Then
If oCell.Address = sAddr Then Exit Do
If sAddr = "" Then sAddr = oCell.Address
oCell.Offset(1, 0).Resize(5, 1).EntireRow.Delete Shift:=xlUp
End If
Loop While Not oCell Is Nothing
End Sub

Once again thanks for your help








Similar Threads
Thread Thread Starter Forum Replies Last Post
Looping ssaranam SQL Server 2005 2 April 17th, 2008 01:40 AM
looping mrjoka Classic ASP Basics 1 September 26th, 2007 12:21 AM
Looping deepsea007 XSLT 1 June 14th, 2007 12:13 PM
Looping..? dedex C# 2 January 6th, 2005 11:24 PM
looping question Domenico Classic ASP Basics 5 August 8th, 2003 03:49 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.