PC Review


Reply
Thread Tools Rate Thread

Delete Macro with OR/ELSE condition

 
 
Mike
Guest
Posts: n/a
 
      11th Jan 2010
I am a beginner a writing Macros in Excel. I have a Macro that deletes any
record that does not equal "Server/Midrange Software" in column M. I need to
expand this to include other criteria. Such as,

if column M does not equal "Server/Midrange Software" or does not equal
"Data Telecom" then delete the record.

The Macro I have currently is.....

Sub TryMe()
'Deletes rows where the value in column M are not equal to Server/Midrange
Software
Dim RowMdx As Long
Dim LastRow As Long

LastRow = Cells(Rows.Count, "M").End(xlUp).Row

For RowMdx = LastRow To 1 Step -1
If Cells(RowMdx, "M").Value <> "Server/Midrange Software" Then
Rows(RowMdx).Delete

End If
Next RowMdx
End Sub

How do I put the OR condition in this for Data Telecom and possibly more
conditions?

Any help appreciated,,,,,,thanks

--
Mike
 
Reply With Quote
 
 
 
 
Dave Peterson
Guest
Posts: n/a
 
      11th Jan 2010
If the number of options is small, you can use something like:

if lcase(Cells(RowMdx, "M").Value) = lcase("Server/Midrange Software") _
or lcase(Cells(RowMdx, "M").Value) = lcase("Data Telecom") _
or lcase(Cells(RowMdx, "M").Value) = lcase("some other value") _
'keep it
else
rows(rowmdx).delete
end if

When the number of entries gets bigger, you may want to use a different
approach...

Dim res as variant
dim myList as variant

mylist = array("Server/Midrange Software", _
"Data Telecom", _
"some other value")

....

for RowMdx = LastRow To 1 Step -1
res = application.match(cells(rowmdx,"M").value, mylist,0)

if isnumber(res) then
'there was a match, skip it
else
Rows(RowMdx).Delete
End If
Next RowMdx

The worksheet function =match() isn't case sensitive.

Mike wrote:
>
> I am a beginner a writing Macros in Excel. I have a Macro that deletes any
> record that does not equal "Server/Midrange Software" in column M. I need to
> expand this to include other criteria. Such as,
>
> if column M does not equal "Server/Midrange Software" or does not equal
> "Data Telecom" then delete the record.
>
> The Macro I have currently is.....
>
> Sub TryMe()
> 'Deletes rows where the value in column M are not equal to Server/Midrange
> Software
> Dim RowMdx As Long
> Dim LastRow As Long
>
> LastRow = Cells(Rows.Count, "M").End(xlUp).Row
>
> For RowMdx = LastRow To 1 Step -1
> If Cells(RowMdx, "M").Value <> "Server/Midrange Software" Then
> Rows(RowMdx).Delete
>
> End If
> Next RowMdx
> End Sub
>
> How do I put the OR condition in this for Data Telecom and possibly more
> conditions?
>
> Any help appreciated,,,,,,thanks
>
> --
> Mike


--

Dave Peterson
 
Reply With Quote
 
Mike
Guest
Posts: n/a
 
      11th Jan 2010
I tried this below but it deletes every record.

Sub TryMe()
'Deletes rows where the value in column M is not equal to Server/Midrange
Software
Dim RowMdx As Long
Dim LastRow As Long

LastRow = Cells(Rows.Count, "M").End(xlUp).Row

For RowMdx = LastRow To 1 Step -1
If LCase(Cells(RowMdx, "M").Value) <> LCase("Server/Midrange Software") _
Or LCase(Cells(RowMdx, "M").Value) <> LCase("Data Telecom") Then
Rows(RowMdx).Delete
End If

Next RowMdx
End Sub

When I use this it deletes only Server/Midrange Software

Sub TryMe()
'Deletes rows where the value in column M is not equal to Server/Midrange
Software
Dim RowMdx As Long
Dim LastRow As Long

LastRow = Cells(Rows.Count, "M").End(xlUp).Row

For RowMdx = LastRow To 1 Step -1
If Cells(RowMdx, "M").Value <> "Server/Midrange Software" Then
Rows(RowMdx).Delete
End If

Next RowMdx

End Sub


So I need obviously I am doing something wrong with the first Macro..

Any Help?
--
Mike


"Dave Peterson" wrote:

> If the number of options is small, you can use something like:
>
> if lcase(Cells(RowMdx, "M").Value) = lcase("Server/Midrange Software") _
> or lcase(Cells(RowMdx, "M").Value) = lcase("Data Telecom") _
> or lcase(Cells(RowMdx, "M").Value) = lcase("some other value") _
> 'keep it
> else
> rows(rowmdx).delete
> end if
>
> When the number of entries gets bigger, you may want to use a different
> approach...
>
> Dim res as variant
> dim myList as variant
>
> mylist = array("Server/Midrange Software", _
> "Data Telecom", _
> "some other value")
>
> ....
>
> for RowMdx = LastRow To 1 Step -1
> res = application.match(cells(rowmdx,"M").value, mylist,0)
>
> if isnumber(res) then
> 'there was a match, skip it
> else
> Rows(RowMdx).Delete
> End If
> Next RowMdx
>
> The worksheet function =match() isn't case sensitive.
>
> Mike wrote:
> >
> > I am a beginner a writing Macros in Excel. I have a Macro that deletes any
> > record that does not equal "Server/Midrange Software" in column M. I need to
> > expand this to include other criteria. Such as,
> >
> > if column M does not equal "Server/Midrange Software" or does not equal
> > "Data Telecom" then delete the record.
> >
> > The Macro I have currently is.....
> >
> > Sub TryMe()
> > 'Deletes rows where the value in column M are not equal to Server/Midrange
> > Software
> > Dim RowMdx As Long
> > Dim LastRow As Long
> >
> > LastRow = Cells(Rows.Count, "M").End(xlUp).Row
> >
> > For RowMdx = LastRow To 1 Step -1
> > If Cells(RowMdx, "M").Value <> "Server/Midrange Software" Then
> > Rows(RowMdx).Delete
> >
> > End If
> > Next RowMdx
> > End Sub
> >
> > How do I put the OR condition in this for Data Telecom and possibly more
> > conditions?
> >
> > Any help appreciated,,,,,,thanks
> >
> > --
> > Mike

>
> --
>
> Dave Peterson
> .
>

 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      11th Jan 2010
Change the OR to AND.

Or use OR and Else like I did.



Mike wrote:
>
> I tried this below but it deletes every record.
>
> Sub TryMe()
> 'Deletes rows where the value in column M is not equal to Server/Midrange
> Software
> Dim RowMdx As Long
> Dim LastRow As Long
>
> LastRow = Cells(Rows.Count, "M").End(xlUp).Row
>
> For RowMdx = LastRow To 1 Step -1
> If LCase(Cells(RowMdx, "M").Value) <> LCase("Server/Midrange Software") _
> Or LCase(Cells(RowMdx, "M").Value) <> LCase("Data Telecom") Then
> Rows(RowMdx).Delete
> End If
>
> Next RowMdx
> End Sub
>
> When I use this it deletes only Server/Midrange Software
>
> Sub TryMe()
> 'Deletes rows where the value in column M is not equal to Server/Midrange
> Software
> Dim RowMdx As Long
> Dim LastRow As Long
>
> LastRow = Cells(Rows.Count, "M").End(xlUp).Row
>
> For RowMdx = LastRow To 1 Step -1
> If Cells(RowMdx, "M").Value <> "Server/Midrange Software" Then
> Rows(RowMdx).Delete
> End If
>
> Next RowMdx
>
> End Sub
>
> So I need obviously I am doing something wrong with the first Macro..
>
> Any Help?
> --
> Mike
>
> "Dave Peterson" wrote:
>
> > If the number of options is small, you can use something like:
> >
> > if lcase(Cells(RowMdx, "M").Value) = lcase("Server/Midrange Software") _
> > or lcase(Cells(RowMdx, "M").Value) = lcase("Data Telecom") _
> > or lcase(Cells(RowMdx, "M").Value) = lcase("some other value") _
> > 'keep it
> > else
> > rows(rowmdx).delete
> > end if
> >
> > When the number of entries gets bigger, you may want to use a different
> > approach...
> >
> > Dim res as variant
> > dim myList as variant
> >
> > mylist = array("Server/Midrange Software", _
> > "Data Telecom", _
> > "some other value")
> >
> > ....
> >
> > for RowMdx = LastRow To 1 Step -1
> > res = application.match(cells(rowmdx,"M").value, mylist,0)
> >
> > if isnumber(res) then
> > 'there was a match, skip it
> > else
> > Rows(RowMdx).Delete
> > End If
> > Next RowMdx
> >
> > The worksheet function =match() isn't case sensitive.
> >
> > Mike wrote:
> > >
> > > I am a beginner a writing Macros in Excel. I have a Macro that deletes any
> > > record that does not equal "Server/Midrange Software" in column M. I need to
> > > expand this to include other criteria. Such as,
> > >
> > > if column M does not equal "Server/Midrange Software" or does not equal
> > > "Data Telecom" then delete the record.
> > >
> > > The Macro I have currently is.....
> > >
> > > Sub TryMe()
> > > 'Deletes rows where the value in column M are not equal to Server/Midrange
> > > Software
> > > Dim RowMdx As Long
> > > Dim LastRow As Long
> > >
> > > LastRow = Cells(Rows.Count, "M").End(xlUp).Row
> > >
> > > For RowMdx = LastRow To 1 Step -1
> > > If Cells(RowMdx, "M").Value <> "Server/Midrange Software" Then
> > > Rows(RowMdx).Delete
> > >
> > > End If
> > > Next RowMdx
> > > End Sub
> > >
> > > How do I put the OR condition in this for Data Telecom and possibly more
> > > conditions?
> > >
> > > Any help appreciated,,,,,,thanks
> > >
> > > --
> > > Mike

> >
> > --
> >
> > Dave Peterson
> > .
> >


--

Dave Peterson
 
Reply With Quote
 
Dave Peterson
Guest
Posts: n/a
 
      11th Jan 2010
Ps. Notice that I used OR and ELSE and a comparison operator of =.

I find that easier to understand.



Dave Peterson wrote:
>
> Change the OR to AND.
>
> Or use OR and Else like I did.
>
> Mike wrote:
> >
> > I tried this below but it deletes every record.
> >
> > Sub TryMe()
> > 'Deletes rows where the value in column M is not equal to Server/Midrange
> > Software
> > Dim RowMdx As Long
> > Dim LastRow As Long
> >
> > LastRow = Cells(Rows.Count, "M").End(xlUp).Row
> >
> > For RowMdx = LastRow To 1 Step -1
> > If LCase(Cells(RowMdx, "M").Value) <> LCase("Server/Midrange Software") _
> > Or LCase(Cells(RowMdx, "M").Value) <> LCase("Data Telecom") Then
> > Rows(RowMdx).Delete
> > End If
> >
> > Next RowMdx
> > End Sub
> >
> > When I use this it deletes only Server/Midrange Software
> >
> > Sub TryMe()
> > 'Deletes rows where the value in column M is not equal to Server/Midrange
> > Software
> > Dim RowMdx As Long
> > Dim LastRow As Long
> >
> > LastRow = Cells(Rows.Count, "M").End(xlUp).Row
> >
> > For RowMdx = LastRow To 1 Step -1
> > If Cells(RowMdx, "M").Value <> "Server/Midrange Software" Then
> > Rows(RowMdx).Delete
> > End If
> >
> > Next RowMdx
> >
> > End Sub
> >
> > So I need obviously I am doing something wrong with the first Macro..
> >
> > Any Help?
> > --
> > Mike
> >
> > "Dave Peterson" wrote:
> >
> > > If the number of options is small, you can use something like:
> > >
> > > if lcase(Cells(RowMdx, "M").Value) = lcase("Server/Midrange Software") _
> > > or lcase(Cells(RowMdx, "M").Value) = lcase("Data Telecom") _
> > > or lcase(Cells(RowMdx, "M").Value) = lcase("some other value") _
> > > 'keep it
> > > else
> > > rows(rowmdx).delete
> > > end if
> > >
> > > When the number of entries gets bigger, you may want to use a different
> > > approach...
> > >
> > > Dim res as variant
> > > dim myList as variant
> > >
> > > mylist = array("Server/Midrange Software", _
> > > "Data Telecom", _
> > > "some other value")
> > >
> > > ....
> > >
> > > for RowMdx = LastRow To 1 Step -1
> > > res = application.match(cells(rowmdx,"M").value, mylist,0)
> > >
> > > if isnumber(res) then
> > > 'there was a match, skip it
> > > else
> > > Rows(RowMdx).Delete
> > > End If
> > > Next RowMdx
> > >
> > > The worksheet function =match() isn't case sensitive.
> > >
> > > Mike wrote:
> > > >
> > > > I am a beginner a writing Macros in Excel. I have a Macro that deletes any
> > > > record that does not equal "Server/Midrange Software" in column M. I need to
> > > > expand this to include other criteria. Such as,
> > > >
> > > > if column M does not equal "Server/Midrange Software" or does not equal
> > > > "Data Telecom" then delete the record.
> > > >
> > > > The Macro I have currently is.....
> > > >
> > > > Sub TryMe()
> > > > 'Deletes rows where the value in column M are not equal to Server/Midrange
> > > > Software
> > > > Dim RowMdx As Long
> > > > Dim LastRow As Long
> > > >
> > > > LastRow = Cells(Rows.Count, "M").End(xlUp).Row
> > > >
> > > > For RowMdx = LastRow To 1 Step -1
> > > > If Cells(RowMdx, "M").Value <> "Server/Midrange Software" Then
> > > > Rows(RowMdx).Delete
> > > >
> > > > End If
> > > > Next RowMdx
> > > > End Sub
> > > >
> > > > How do I put the OR condition in this for Data Telecom and possibly more
> > > > conditions?
> > > >
> > > > Any help appreciated,,,,,,thanks
> > > >
> > > > --
> > > > Mike
> > >
> > > --
> > >
> > > Dave Peterson
> > > .
> > >

>
> --
>
> Dave Peterson


--

Dave Peterson
 
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
HELP modifying macro to delete rows if more than 1 condition met & Mayte Microsoft Excel Programming 1 1st Mar 2010 09:54 PM
Macro to delete on condition KingdomGirl Microsoft Excel Programming 3 22nd Sep 2008 06:17 PM
Macro to delete rows based on a condition =?Utf-8?B?RGFycmlseW4=?= Microsoft Excel Worksheet Functions 1 6th Sep 2007 12:12 AM
macro to delete rows on condition =?Utf-8?B?VG9kZA==?= Microsoft Excel Programming 1 24th Aug 2006 11:44 PM
macro to delete on a condition =?Utf-8?B?VG9kZA==?= Microsoft Excel Programming 3 28th Jun 2006 08:32 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:43 PM.