Best use of If, Else, ElseIf in my condition

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
880
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Looking for the best way to accomplish this. Is someone able to assist?

I only want to apply formulas if the data is greater than 2 (with header). So if it is then one formula if a range is defined or another formula if it isnt. seems simple but I can't seem to master it. If range is less than 2 then no formulas to be applied (just skip over them).

if there is data for the formula then apply said formulas
VBA Code:
If WsALL.Range("A" & rows.count).End(xlUp).row > 2 Then

apply this formula if BulkList = ""
VBA Code:
.Range("B2:B" & lr3).Formula2 = "=IFERROR(COUNTIF(ALL!$D$2:$D$" & lr2 & ",A2),0)"

but apply this formula if Bulklist <> ""
VBA Code:
.Range("B2:B" & lr3).Formula2 = "=0"
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Ok I think I got it. Thanks for any that were looking at it. Some trial and error - with probably some luck got me there :)

Here it is if anyone else in the future needs it.

VBA Code:
If WsALL.Range("A" & rows.count).End(xlUp).row < 2 Then
    .Range("B2:B" & lr3).Formula = "=0"
    .Range("C2:C" & lr3).Formula = "=0"
ElseIf BULKlist = "" Then
    .Range("B2:B" & lr3).Formula2 = "=IFERROR(COUNTIF(ALL!$D$2:$D$" & lr2 & ",A2),0)"
    .Range("C2:C" & lr3).Formula = "=if(B2=0,0,COUNTA(UNIQUE(FILTER(ALL!$P$2:$P$" & lr2 & ",ALL!$D$2:$D$" & lr2 & "=A2)))-COUNTIFS(ALL!$D$2:$D$" & lr2 & ",A2))"
ElseIf BULKlist <> "" Then
    .Range("B2:B" & lr3).Formula2 = "=IFERROR(COUNTIFS(ALL!$C$2:$C$" & lr2 & ",BULKlist,ALL!$D$2:$D$" & lr2 & ",A2),0)"
    .Range("C2:C" & lr3).Formula = "=if(B2=0,0,COUNTA(UNIQUE(FILTER(ALL!$P$2:$P$" & lr2 & ",ALL!$D$2:$D$" & lr2 & "=A2)))-COUNTIFS(ALL!$D$2:$D$" & lr2 & ",A2))"
End If
 
Upvote 0
Solution

Forum statistics

Threads
1,224,823
Messages
6,181,175
Members
453,021
Latest member
Justyna P

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