# Excel formula vba

Hello,

Dim LastRow As Long
Sheets("Orders").Select
LastRow = Range("A" & Rows.Count).End(xlUp).Row
With Range("AF2:AF" & LastRow)
.Formula =

IF CELL (Z2 = 109 OR Z2 = 313) AND CELL (E2 = MONDAY_12 OR E2 = MONDAY_17) THEN CELL AF2 = RUN 1
IF CELL (Z2 = 109 OR Z2 = 313) AND CELL (E2 = TUESDAY_12 OR E2 = TUESDAY_17) THEN CELL AF2 = RUN 2
IF CELL (V2 Starts with "K") AND CELL Z2 = 124 THEN CELL AF2 = OTTAWA
ANYTHING ELSE CELL AF2 = OTHER

End With
###### Who is Participating?

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Commented:
Use this..

``````Sheets("Orders").Select
LastRow = Range("A" & Rows.Count).End(xlUp).Row
With Range("AF2:AF" & LastRow)
.Formula = "=IF(AND(OR(Z2=109,313),OR(E2=""Monday_12"",E2=""Monday_17"")),""RUN 1"",IF(AND(OR(Z2=109,313),OR(E2=""Tuesday_12"",E2=""Tuesday_17"")),""RUN 2"",IF(AND(LEFT(TRIM(V2),1)=""K"",Z2=124),""OTTAWA"",""Other"")))"
End With
``````

Saurabh...
Author Commented:
Hello,
not quite , it's not recognizing the conditions properly,

Example
MONDAY_17     J6RXXX        124      is showing as   RUN 1 (Should be OTHER)
MONDAY_17     K0A2A0        124      is showing as   RUN 1 (Should be OTTAWA)

Thanks
Commented:
Use this..

``````Sheets("Orders").Select
LastRow = Range("A" & Rows.Count).End(xlUp).Row
With Range("AF2:AF" & LastRow)
.Formula = "=IF(AND(LEFT(TRIM(V2),1)=""K"",Z2=124),""OTTAWA"",IF(AND(OR(Z2=109,Z2=313),OR(E2=""Monday_12"",E2=""Monday_17"")),""RUN 1"",IF(AND(OR(Z2=109,313),OR(E2=""Tuesday_12"",E2=""Tuesday_17"")),""RUN 2"",""Other"")))"

End With
``````

Saurabh...

Experts Exchange Solution brought to you by