Thread: Looping through
View Single Post
  #10   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Looping through

I do not recommend using the select method and moving around cells using the
offset!!!!!!!!!!! this type of coding is difficult to follow and is prone to
errors.

Code should be written to be clear and and easily documented. The change
Looping through recommended is "BAD Program Style". I have a Master's in
computerr Science and have sat through plenty of lectures where teachers
scold students like Looping Through. Looping through deserves a "C" grade.

Using offsets is "sometimes" acceptable when you are writing code that
depends on the selected cell when the code is started.

"Incidental" wrote:

Hi there

First off sorry for the late reply but i had not checked my posts all
weekend. I'm not sure i have got exactly what you want but it should
be workable to get you sorted. It will check the values as with the
previous code but if it finds a text value it will then begin a loop
offsetting one cell to the right until it finds a numerical value then
it will set the range and delete it. You may have to call this code
from a for each next sort of loop if you want it to run every time it
finds the set value but it should give you an idea of a way around
your problem i hope.

Option Explicit
Dim LastRow As Integer
Dim MyRng As Range
Dim FindCell As Range
Dim FirstCell As Range
Dim SecondCell As Range
Dim i As Integer '########Added variable
Dim LastCol As Integer '######## Added variable

Function StepFive()

On Error Resume Next 'catch error if nothing found

Set MyRng = Range("A1", [A65535].End(xlUp))

Set FindCell = MyRng.Find(What:="Part", LookAt:=xlPart)

If FindCell < "" Then

Set FirstCell = FindCell.Offset(2, 1)

Select Case FindCell.Offset(2, 1).Value

Case "-"
Set SecondCell = FirstCell.Offset(1, 0)

Case Is = 0
Set SecondCell = FirstCell.Offset(0, -1)

Case Is < "" 'Changed to look for a text value
'and moved to the end select case
'statement to check the other
'"case" values first
'The line below will check which column holds
'the last value to limit the loop size
LastCol = FirstCell.End(xlToRight).Column

For i = 1 To LastCol 'Begin a loop
'The line below will check for a numerical
'value in the offset cell
If IsNumeric(FirstCell.Offset(0, i)) Then

Set SecondCell = FirstCell.Offset(0, i)
'Call the deletedata function from here
'for this "case" type so you can exit
'the loop to prevent further checking
'of the row
DeleteData

Exit Function

End If

Next i 'Iterate the loop

End Select
'Call the deletedata function here for the other
'"case" types
DeleteData

End If

End Function
Function DeleteData()
'This is just the same code to delete the
'data from the sheet but it has been moved
'into it's own function so you can exit the
'loop with out any problems
Range(FirstCell, SecondCell).Select
Selection.Delete Shift:=xlToLeft
End Function


I hope this helps you out if not post back and i will see what i can
do

Steve