Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Multiple Criteria - Shortening Code


following is my code:

For i = 100 to 1

If cells(1,i) < "GM" AND If cells(1,i) < "tURNOVER" AND If cells(1,i)
< "cONTRIBUTION" AND............Then
Selection.EntireColumn.Delete

There are 15 criteria, how do I shorten my code, maybe using an array?
not sure.

Thanks


--
T De Villiers
------------------------------------------------------------------------
T De Villiers's Profile: http://www.excelforum.com/member.php...o&userid=26479
View this thread: http://www.excelforum.com/showthread...hreadid=566269

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,124
Default Multiple Criteria - Shortening Code

you need the step if going from the bottom up. You don't need the IF more
than once.You don't need to select.
for i=100 to 1 step-1
if cells(1,i)<"GM" and cells(1,i) < "tURNOVER" then
cells(1,i).entirecolumn.delete
next i

Are you trying to delete more than one column? If not, then use FIND to find
the column to delete. You could use select case for lots of criteria

--
Don Guillett
SalesAid Software

"T De Villiers"
wrote in message
news:T.De.Villiers.2bpig2_1154176206.1429@excelfor um-nospam.com...

following is my code:

For i = 100 to 1

If cells(1,i) < "GM" AND If cells(1,i) < "tURNOVER" AND If cells(1,i)
< "cONTRIBUTION" AND............Then
Selection.EntireColumn.Delete

There are 15 criteria, how do I shorten my code, maybe using an array?
not sure.

Thanks


--
T De Villiers
------------------------------------------------------------------------
T De Villiers's Profile:
http://www.excelforum.com/member.php...o&userid=26479
View this thread: http://www.excelforum.com/showthread...hreadid=566269



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,092
Default Multiple Criteria - Shortening Code

This requires a list of your 'Criteria' somewhere in the workbook and the
list has a Name. In my example it is "Crit_List".
This works on column A and finds how long the list is in case it is not 100.
This puts your list into myArray and compares every cell in column A for a
Match to the list. If no Match is found (If IsError(Application.Match) then
the row is deleted. This also works from the bottom, up.

Sub Filter()
Dim myArray As Variant
Dim LastRow As Long
Dim FirstRow As Long
Dim iRow As Long
myArray = ThisWorkbook.Names("Crit_List").RefersToRange.Valu e

FirstRow = 1
LastRow = Cells(Rows.Count, "A").End(xlUp).Row

For iRow = LastRow To FirstRow Step -1
If IsError(Application.Match(Cells(iRow, "A").Value, myArray, 0))
Then
Rows(iRow).Delete
Else
'do nothing
End If
Next iRow
End Sub

Mike F

"T De Villiers"
wrote in message
news:T.De.Villiers.2bpig2_1154176206.1429@excelfor um-nospam.com...

following is my code:

For i = 100 to 1

If cells(1,i) < "GM" AND If cells(1,i) < "tURNOVER" AND If cells(1,i)
< "cONTRIBUTION" AND............Then
Selection.EntireColumn.Delete

There are 15 criteria, how do I shorten my code, maybe using an array?
not sure.

Thanks


--
T De Villiers
------------------------------------------------------------------------
T De Villiers's Profile:
http://www.excelforum.com/member.php...o&userid=26479
View this thread: http://www.excelforum.com/showthread...hreadid=566269



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 10,593
Default Multiple Criteria - Shortening Code

Dim i As Long
Dim aryTest

aryTest = Array("GM", "Turnover", "Contribution", "etc") '<=== Update

For i = 100 To 1 Step -1
If Not IsError(Application.Match(Cells(1, i).Value, aryTest, 0))
Then
Columns(i).Delete
End If
Next i


--
HTH

Bob Phillips

(replace somewhere in email address with gmail if mailing direct)

"T De Villiers"
wrote in message
news:T.De.Villiers.2bpig2_1154176206.1429@excelfor um-nospam.com...

following is my code:

For i = 100 to 1

If cells(1,i) < "GM" AND If cells(1,i) < "tURNOVER" AND If cells(1,i)
< "cONTRIBUTION" AND............Then
Selection.EntireColumn.Delete

There are 15 criteria, how do I shorten my code, maybe using an array?
not sure.

Thanks


--
T De Villiers
------------------------------------------------------------------------
T De Villiers's Profile:

http://www.excelforum.com/member.php...o&userid=26479
View this thread: http://www.excelforum.com/showthread...hreadid=566269



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Multiple Criteria - Shortening Code

Hello
As Don stated in his reply, you must have STEP-1 to be looping down. In
fact you must loop down because it is better (in this case) to delete from
the right.

You cannot use Selection.EntireColumn.Delete as this will delete the column
wherever you current selected cell is.

Here are three versions of getting the shortest code.
I would use V1 or V2 as V3 is just silly. You could use it if you are
challenging yourself to write the least amount of lines, and with a bit of
lateral thinking get it even shorter.

You would add all your criteria to the assignment of criteriaString

[Code samples]
Sub DeleteColumnV1()
Dim criteriaString As String
Dim currentCell As String
Dim z As Integer
Dim i As Integer
criteriaString = "GMtURNOVERcONTRIBUTIONpAYMENTbALANCE"
For i = 10 To 1 Step -1
currentCell = Cells(1, i).Value
z = InStr(criteriaString, currentCell)
If z 0 And currentCell < "" Then
Columns(Cells(1, i).Column).Delete
End If
Next
End Sub

Sub DeleteColumnV2()
Dim criteriaString As String
Dim currentCell As String
Dim z As Integer
Dim i As Integer
criteriaString = "GMtURNOVERcONTRIBUTIONpAYMENTbALANCE"
For i = 10 To 1 Step -1
z = InStr(criteriaString, Cells(1, i))
If z 0 And Cells(1, i) < "" Then
Columns(Cells(1, i).Column).Delete
End If
Next
End Sub

Sub DeleteColumnV3()
For i = 10 To 1 Step -1
If (InStr("GMtURNOVERcONTRIBUTIONpAYMENTbALANCE", Cells(1, i))) And
(Cells(1, i) < "") Then
Columns(Cells(1, i).Column).Delete
End If
Next
End Sub

Hope this helps

ChasAA

"T De Villiers" wrote:


following is my code:

For i = 100 to 1

If cells(1,i) < "GM" AND If cells(1,i) < "tURNOVER" AND If cells(1,i)
< "cONTRIBUTION" AND............Then
Selection.EntireColumn.Delete

There are 15 criteria, how do I shorten my code, maybe using an array?
not sure.

Thanks


--
T De Villiers
------------------------------------------------------------------------
T De Villiers's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=26479
View this thread: http://www.excelforum.com/showthread.php?threadid=566269




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 33
Default Multiple Criteria - Shortening Code

Hello Again,
Sorry I wrote the code to delete the column if match was found rather than
the other way.
But I would go with Bob's reply anyway
Sorry for the mixup on my part.
My code could be reversed easily by changing the "if z0 then..." to "if z=0
then.."

"ChasAA" wrote:

Hello
As Don stated in his reply, you must have STEP-1 to be looping down. In
fact you must loop down because it is better (in this case) to delete from
the right.

You cannot use Selection.EntireColumn.Delete as this will delete the column
wherever you current selected cell is.

Here are three versions of getting the shortest code.
I would use V1 or V2 as V3 is just silly. You could use it if you are
challenging yourself to write the least amount of lines, and with a bit of
lateral thinking get it even shorter.

You would add all your criteria to the assignment of criteriaString

[Code samples]
Sub DeleteColumnV1()
Dim criteriaString As String
Dim currentCell As String
Dim z As Integer
Dim i As Integer
criteriaString = "GMtURNOVERcONTRIBUTIONpAYMENTbALANCE"
For i = 10 To 1 Step -1
currentCell = Cells(1, i).Value
z = InStr(criteriaString, currentCell)
If z 0 And currentCell < "" Then
Columns(Cells(1, i).Column).Delete
End If
Next
End Sub

Sub DeleteColumnV2()
Dim criteriaString As String
Dim currentCell As String
Dim z As Integer
Dim i As Integer
criteriaString = "GMtURNOVERcONTRIBUTIONpAYMENTbALANCE"
For i = 10 To 1 Step -1
z = InStr(criteriaString, Cells(1, i))
If z 0 And Cells(1, i) < "" Then
Columns(Cells(1, i).Column).Delete
End If
Next
End Sub

Sub DeleteColumnV3()
For i = 10 To 1 Step -1
If (InStr("GMtURNOVERcONTRIBUTIONpAYMENTbALANCE", Cells(1, i))) And
(Cells(1, i) < "") Then
Columns(Cells(1, i).Column).Delete
End If
Next
End Sub

Hope this helps

ChasAA

"T De Villiers" wrote:


following is my code:

For i = 100 to 1

If cells(1,i) < "GM" AND If cells(1,i) < "tURNOVER" AND If cells(1,i)
< "cONTRIBUTION" AND............Then
Selection.EntireColumn.Delete

There are 15 criteria, how do I shorten my code, maybe using an array?
not sure.

Thanks


--
T De Villiers
------------------------------------------------------------------------
T De Villiers's Profile: http://www.excelforum.com/member.php?action=getinfo&userid=26479
View this thread: http://www.excelforum.com/showthread.php?threadid=566269


  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Multiple Criteria - Shortening Code


Thanks for all this - very helpfu

--
T De Villier
-----------------------------------------------------------------------
T De Villiers's Profile: http://www.excelforum.com/member.php...fo&userid=2647
View this thread: http://www.excelforum.com/showthread.php?threadid=56626

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
match multiple criteria ina range from multiple criteria multiplet RG Excel Worksheet Functions 8 September 28th 07 04:21 AM
Shortening a formula GTVT06 Excel Programming 10 July 20th 06 02:43 AM
Counting Cells with multiple criteria.One criteria supporting wild Azhar Saleem Excel Worksheet Functions 1 January 12th 05 10:54 AM
Counting Cells with multiple criteria.One criteria supporting wild Azhar Arain Excel Worksheet Functions 1 January 12th 05 08:33 AM
Help with shortening/cleaning some code please roy Excel Programming 3 June 3rd 04 11:49 PM


All times are GMT +1. The time now is 06:10 AM.

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

About Us

"It's about Microsoft Excel"