FILTER formula criteria based of a variable with VBA

ItalianPlatinum

Well-known Member
Joined
Mar 23, 2017
Messages
843
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I have a FILTER formula where I need one of the criteria to come through as a text (CompList2). I don't have control over the source data so before it is asked I cannot change that to "general" as a workaround. Does anyone see an easy way to resolve? I provided parts of my VBA below that pertain to this condition.

Formula is in column C (shown below). The variable I have as a string. It populates - should it not be as a string?

VBA Code:
Dim CompList, CompList2 As String
'Other code
WsFT.Names.Add Name:="CompList", RefersTo:=Range("B11")
CompList = Range("CompList")
'other code
'run for each 
  j = 0
  Do Until WsFT.Range("CompList").Offset(j, 0) = ""
    CompList2 = WsFT.Range("CompList").Offset(j, 0)
'other code including lr2 variable
With WsSum
    .Range("B2:B" & lr3).Formula2 = "=IFERROR(COUNTIFS(ALL!$C$2:$C$" & lr2 & "," & CompList2 & ",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!$C$2:$C$" & lr2 & "=" & CompList2 & ")*(ALL!$D$2:$D$" & lr2 & "=A2))))-B2)"
    .Range("B1:E" & lr3).Value = .Range("B1:E" & lr3).Value
End With
'other code

How it looks after VBA
1721076939041.png


How it looks after I wrap it in "" and this is the desired value I should see
1721076914896.png


So how can I modify either the formula to work or how I conduct the usage of the variable. I do use the variable CompList2 in other places but I don't think it should impact those areas.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Give this a try:

VBA Code:
"=IF(B2=0,0,COUNTA(UNIQUE(FILTER(ALL!$P$2:$P$" & lr2 & ",(ALL!$C$2:$C$" & lr2 & "=""" & CompList2 & """)*(ALL!$D$2:$D$" & lr2 & "=A2))))-B2)"

Added extra double quotes in to see if it will add them around CompList2 value.
 
Upvote 0
Solution

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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