ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If command with 3 options (https://www.excelbanter.com/excel-programming/383260-if-command-3-options.html)

Kevin Porter

If command with 3 options
 
I am trying to compare a row of names with a column list.
If the name is in the list I want it to go to the next cell.
If the name = NULL then I want it to go to the next cell.
If the name = Total I want it to stop.
If the name is not in the list I want it to delete the column.

Sub NameDelete()

With Sheets("RowOfNames")
For Each cell In .Range("$1:$1")
iRow = 0
On Error Resume Next
iRow = Application.Match(cell.Value,
Sheets("List").Range("$A:$A"), 0)
On Error GoTo 0
If cell.Value = Null Then Resume Next
If cell.Value = "Total" Then End
If cell.Value < iRow Then cell.EntireColumn.Delete
Next cell
End With
End Sub

It runs, but deletes everything.

I copied this code from another answer and tried to adopt it to my project.

Thanks in advance.

Kevin Porter

If command with 3 options
 
After learning how to use the debug to see my what the variables are set too
and stepping through it, the problem seems to be with:

iRow = Application.Match(cell.Value, Sheets("List").Range("$A:$A"), 0)

iRow never changes from equaling 0, so it just deletes every column. The
NULL and "Total" arguments seem to work fine.

"Kevin Porter" wrote:

I am trying to compare a row of names with a column list.
If the name is in the list I want it to go to the next cell.
If the name = NULL then I want it to go to the next cell.
If the name = Total I want it to stop.
If the name is not in the list I want it to delete the column.

Sub NameDelete()

With Sheets("RowOfNames")
For Each cell In .Range("$1:$1")
iRow = 0
On Error Resume Next
iRow = Application.Match(cell.Value,
Sheets("List").Range("$A:$A"), 0)
On Error GoTo 0
If cell.Value = Null Then Resume Next
If cell.Value = "Total" Then End
If cell.Value < iRow Then cell.EntireColumn.Delete
Next cell
End With
End Sub

It runs, but deletes everything.

I copied this code from another answer and tried to adopt it to my project.

Thanks in advance.



All times are GMT +1. The time now is 02:55 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com