EXCEL VBA, IF odd and even with condition

xdenama

New Member
Joined
Feb 12, 2016
Messages
39
Office Version
  1. 365
Sub OddRowAlert()
With Range("B16:B100")
.Formula = "=IF((MOD(ROW(B16),2)),"YES","NO")"
.Formula = .Value
End With
End Sub

I get this code in another forum, but I don't know how to give condition, example if "A16" is not empty than "B16" can show either "YES" or "NO". If "A16" is empty, than nothing to show?
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Welcome! I'm new to the forum as well but I think I can answer your question. The function ROW(B16) returns the row number "16". The mod() function takes the first number (16) and divides it by the second number (2) and returns the remainder. For an even number, the function will return 0 or FALSE and hence give you the "NO". An odd number returns a value of 1 or TRUE and gives the "YES". The line of the code runs irrespective of what values are in the range of B16:B100 since it based on the row number. I often use a similar function MOD(ROW(),2) or MOD(ROW()+1,2) with conditional formatting to selectively fill rows in a big table to ease readability. The choice of the 2 MOD functions depends on whether I want the even or odd colors formatted.
 
Upvote 0
Welcome! I'm new to the forum as well but I think I can answer your question. The function ROW(B16) returns the row number "16". The mod() function takes the first number (16) and divides it by the second number (2) and returns the remainder. For an even number, the function will return 0 or FALSE and hence give you the "NO". An odd number returns a value of 1 or TRUE and gives the "YES". The line of the code runs irrespective of what values are in the range of B16:B100 since it based on the row number. I often use a similar function MOD(ROW(),2) or MOD(ROW()+1,2) with conditional formatting to selectively fill rows in a big table to ease readability. The choice of the 2 MOD functions depends on whether I want the even or odd colors formatted.
Thank for reply, I'm asking about condition at another column, "A16", range for write the answer is "B16" and so on.
 
Upvote 0
Sorry, I misunderstood your question. I believe this code would do what you are asking. Note that the first part just writes the result and the second would leave the formula in the range

VBA Code:
Sub firstColAlert()
    With Range("B16:B100") ' just writes the result in the range
    .Formula = "=IF(ISBLANK(A16),"""",""Not empty"")"
    .Formula = .Value
    End With
    
    With Range("D16:D100") ' writes the formula the range
    .Value = "=IF(ISBLANK(A16),"""",""Not empty"")"
    End With
    
End Sub
 
Upvote 0
Sorry, I misunderstood your question. I believe this code would do what you are asking. Note that the first part just writes the result and the second would leave the formula in the range

VBA Code:
Sub firstColAlert()
    With Range("B16:B100") ' just writes the result in the range
    .Formula = "=IF(ISBLANK(A16),"""",""Not empty"")"
    .Formula = .Value
    End With
  
    With Range("D16:D100") ' writes the formula the range
    .Value = "=IF(ISBLANK(A16),"""",""Not empty"")"
    End With
  
End Sub
Thanks again, but not consider odd and even row...
 
Upvote 0
So you want every other row to evaluate whether the first column has a value? Then, you need to just nest the IF statements

VBA Code:
Sub firstColAlert()
    With Range("B16:B100") ' just writes the result in the range
    .Formula = "=IF(MOD(ROW(B16),2),"""",IF(ISBLANK(A16),"""",""Not empty""))"
    .Formula = .Value
    End With
    
    With Range("D16:D100") ' writes the formula the range
    .Value = "=IF(MOD(ROW(B16),2),"""",IF(ISBLANK(A16),"""",""Not empty""))"
    End With
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,614
Messages
6,120,519
Members
448,968
Latest member
Ajax40

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