View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Combining multiple IF statements

The first macro I didn't see any problems. I changed the code to use Select
Case because it is easier to read.

The problem with delete rows is yo have to start with the last row and move
up otherwise the code doesn't make sense. Using a For Each with a range
doesn't ensure that the cells are removed in the correct order. It somehow
confuses Excel. Using a RowcCount like I did solves this problem. Your
second code should of worked also except the second If statement is outside
the FOR loop. I like Select Case so I chnae the code although it is not
neessary.

The 3rd macro should be written like the first two using a row counter
instead of a Range becuase of the delete row.

Sub Replace1()
Dim r As Range
Dim srng As Range
Set srng = Range("I1", Range("I" & Rows.Count). _
End(xlUp)).SpecialCells(xlCellTypeConstants, xlTextValues)
For Each r In srng
Select Case r.Value
Case "Capula", "Microsoft", "iSOFT", "System C"
r.Offset(0, 1).Value = "AP"
End Select
Next r
End Sub
Sub dr()
mc = "I"
For RowCount = Cells(Rows.Count, mc).End(xlUp).Row To 2 Step -1
Select Case Range(mc & RowCount).Value
Case "AMS Subcon", "Red Tray", "Microsoft", "iSOFT"
Rows(i).Delete
End Select
End Sub
Sub DeleteRowsbyDate()
Lastrow = [x1].End(xlDown).Row
For RowCount = Lastrow To 1 Step -1
If IsDate(Range("C" & RowCount)) And _
Range("C" & RowCount) Now() Then _

cll.EntireRow.Delete
End If
Next
End Sub


"Zak" wrote:

Please can you start me off please? The below are 3 of my conditions, as
evident below the first one asks it to find a word then change the
corresponding cell in the next column to a given word. The second and third
tells it to to delete certain lines but both refering to different columns..

I am just unsure of the syntax and how i should combine them and specify
each time i want it to look at a different column. Please can you combine
these 3 so that i can get some sort of idea as to how to word things then i
can add the other few in myself.

thanks a lot

my code:

code 1:

Sub Replace1()
Dim r As Range
Dim srng As Range
Set srng = Range("I1", Range("I" & Rows.Count). _
End(xlUp)).SpecialCells(xlCellTypeConstants, xlTextValues)
For Each r In srng
If r.Value = "Capula" Then
r.Offset(0, 1).Value = "AP"
Else
If r.Value = "Microsoft" Then
r.Offset(0, 1).Value = "AP"
Else
If r.Value = "iSOFT" Then
r.Offset(0, 1).Value = "AP"
Else
If r.Value = "System C" Then
r.Offset(0, 1).Value = "AP"
End If
End If
End If
End If

code 2:

Sub dr()
mc = "I"
For i = Cells(Rows.Count, mc).End(xlUp).Row To 2 Step -1
If Cells(i, mc) = "AMS Subcon" _
Or Cells(i, mc) = "Red Tray" Then Rows(i).delete
Next i
If Cells(i, mc) = "Microsoft" _
Or Cells(i, mc) = "iSOFT" Then Rows(i).delete
End Sub

code 3:

Sub DeleteRowsbyDate()
For Each cll In Range([x1], [x1].End(xlDown))
If IsDate(cll) And cll Now() Then _
cll.EntireRow.delete
Next
End Sub

"Joel" wrote:

there is no limitation to the number or types of IF statements that can be
put into a macro.. You just have to be smart and make sure there are no
conflicts between the IFs

For example, if the first IF deletes columns J,K,L then a second IF must
cosider two cases. First case, when the first IF did delete the columns and
the second case where the first IF did not delete the columns.

"Zak" wrote:

I have 9 different IF statements that need to be put into the same macro.
Some of these conditions are to delete something that meets a criteria, some
replace words with something else etc.

Will excel 2003 allow the combining of IF statements of different kinds?
(the statements do different things and refer to different columns etc)? I've
been told i have to use the word IF's rather than IF, i dont have a clue
where to start!

please help. thanks.