ELSE following elseif

Puffin617

New Member
Joined
Nov 13, 2008
Messages
27
I want to check my column headings to see whether it contains certain pieces of text or match entries in an array BUT if neither of my checks are true, then it must perform the statement after ELSE (moving to next column)

Currently I get a "Else without IF" error

Code:
If ActiveCell.Text = "#N/A" Then ActiveCell.EntireColumn.Delete Shift:=xlToLeft
        ElseIf Application.Match(ActiveCell.Text, AnalyteArray, 0) > 0 Then FirstAnalyteColumn = ActiveCell.Column
        Else
            Cells(ActiveCell.Row, ActiveCell.Column + 1).Select
        End If
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Try

Code:
If ActiveCell.Text = "#N/A" Then
    ActiveCell.EntireColumn.Delete Shift:=xlToLeft
ElseIf Application.Match(ActiveCell.Text, AnalyteArray, 0) > 0 Then
    FirstAnalyteColumn = ActiveCell.Column
Else
    Cells(ActiveCell.Row, ActiveCell.Column + 1).Select
End If
 
Upvote 0
You are actually missing an If.

I think it might actually be worth looking at your logic as well.

eg Do you even need any ElseIfs?

If you do want the offset to happen every time just remove it from the Ifs completely.

Also, if this code is meant to delete columns you should loop from right to left.
 
Upvote 0
It gives you the error because it treats this line as a separate statement, separate from the rest of the code:
Code:
If ActiveCell.Text = "#N/A" Then ActiveCell.EntireColumn.Delete Shift:=xlToLeft

Just like VoG suggested, move anything after the "Then" onto a separate line.
 
Upvote 0
You can loop from right to left using something like this.

I've used a dummy array of 3 values.
Code:
Dim rng As Range
Dim I As Long
Dim AnalyteArray
Dim FirstAnalyteColumn As Long
Dim res
 
    AnalyteArray = Array("Field1", "Field2", "Field3")
 
    For I = Cells(1, Columns.Count).End(xlToLeft).Column To 1 Step -1

        Set rng = Cells(1, I)

        If rng.Text = "#N/A#" Then
 
            rng.EntireColumn.Delete Shift:=xlToLeft

        Else

            res = Application.Match(rng.Text, AnalyteArray, 0)

            If Not IsError(res) Then

                FirstAnalyteColumn = rng.Column

            End If
 
        End If
 
    Next I
 
Upvote 0

Forum statistics

Threads
1,224,564
Messages
6,179,543
Members
452,924
Latest member
JackiG

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top