IF....., If....., If.......else VBA

SAMCRO2014

Board Regular
Joined
Sep 3, 2015
Messages
160
I am trying to create coding that will loop through column L (12) and if the cell contains the specific text or value then enter "Previous" or "Current" in adjacent cell in column M (13):

"Error" = Current
"[Rr]etro = Previous
> 201814 = Current
all other cells = Previous

Dim f As Long ' for fiscal

Dim Data As Worksheet
Set Data = ThisWorkbook.Sheets("Data")

Dim LastRowData As Long
LastRowData = Data.Cells(Rows.Count, 12).End(xlUp).Row


For f = 2 To LastRowData

If Cells(f, 12).Value Like "*[Rr]etro*" Then
Cells(f, 13) = "Previous"
If Cells(f, 12).Value Like "*Error*" Then
Cells(f, 13) = "Current"
ElseIf Cells(f, 12).Value > 201814 Then
Cells(f, 13) = "Current"
Else: Cells(f, 13) = "Previous"

End If

Next f

I think I may have too many arguments?
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Untested, however, try:
Code:
Sub M1()

    Dim x   As Long
    
    Application.ScreenUpdating = False
    
    With Sheets("Data")
        If .AutoFilterMode Then .AutoFilterMode = False
        x = .Cells(.Rows.Count, 12).End(xlUp).Row
        .Cells(2, 13).Resize(x - 1).Value = "Previous"
        
        With .Cells(1, 12).Resize(x, 2).AutoFilter
            .AutoFilter field:=1, Criteria1:="*Error*"
            .Offset(1, 1).Resize(x - 1, 1).SpecialCells(xlCellTypeVisible).Value = "Current"
            .ShowAllData
            .AutoFilter field:=1, Criteria1:=">" & 201814
            .Offset(1, 1).Resize(x - 1, 1).SpecialCells(xlCellTypeVisible).Value = "Current"
        End With
        
        .AutoFilterMode = False
    End With
    
    Application.ScreenUpdating = True
    
End Sub
Macro defaults all values in column 13 to "Previous" and then filters only for *Error* and values > 201814 in column 12 and replaces values in column 13 with "Current"
 
Upvote 0
Another option
Code:
Sub test1()
With Range("M2", Range("L" & Rows.count).End(xlUp).Offset(, 1))
   .Value = Evaluate(Replace("if((@>201814)+(isnumber(search(""error"",@))),""Current"",""Previous"")", "@", .Offset(, -1).Address))
End With
End Sub
 
Upvote 0
VBA does not like the coding:

.AutoFilter field:=1, Criteria1:="*Error*"

it is giving me a compile error

I really like the idea of this coding though. I tried to use tag to your post but I could not figure how to
 
Last edited:
Upvote 0
You can look up "VBA autofilter" find some code and adapt or record it and review the resulting code.

Fluff's suggestion is shorter and one I'm trying to learn/use more often with EVALUATE but with both, they're not looping, so both are alternate ways to test data and output results
 
Upvote 0
Another option
Code:
Sub test1()
With Range("M2", Range("L" & Rows.count).End(xlUp).Offset(, 1))
   .Value = Evaluate(Replace("if((@>201814)+(isnumber(search(""error"",@))),""Current"",""Previous"")", "@", .Offset(, -1).Address))
End With
End Sub


This coding did not work. The cells containing the word "[Rr]etro are showing as "Current" when it shoud be "Previous"
 
Upvote 0
How about
Code:
Sub test1()
With Range("M2", Range("L" & Rows.count).End(xlUp).Offset(, 1))
   .Value = Evaluate(Replace("if(((isnumber(@))*(@>201814))+(isnumber(search(""error"",@))),""Current"",""Previous"")", "@", .Offset(, -1).Address))
End With
End Sub
 
Upvote 0
How about
Code:
Sub test1()
With Range("M2", Range("L" & Rows.count).End(xlUp).Offset(, 1))
   .Value = Evaluate(Replace("if(((isnumber(@))*(@>201814))+(isnumber(search(""error"",@))),""Current"",""Previous"")", "@", .Offset(, -1).Address))
End With
End Sub

It did change the cells containing the word "retro" to Previous but it also change any cells in that column with text to Previous including "Error"
 
Upvote 0
This is what I get


Excel 2013 32 bit
LM
2E05004787Previous
3ab error yzCurrent
4123Previous
5JanerroruaryCurrent
6JanuaryPrevious
7[Rr]etroPrevious
8201815Current
912[Rr]etro89Previous
10ERRORCurrent
111234567Current
Sheet1


If it's not working for you, can you supply some sample data. If you follow the link in my signature, you can find some tools that will enable you to do that
 
Upvote 0

Forum statistics

Threads
1,223,958
Messages
6,175,636
Members
452,662
Latest member
Aman1997

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