VBA WorksheetFunction StDev with Filter and multiple conditions

Sir_BlahBlah

New Member
Joined
Apr 17, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
So this is a simplified version of a problem I am having in a much larger VBA macro.

Essentially I need to perform the following worksheet calculations in VBA:

=STDEV.S(IF(A1:A100 = "a",B1:B100))
=STDEV.S(IF((A1:A100 = "a")+(A1:A100 = "c")+(A1:A100 = "e")+(A1:A100 = "h")+(A1:A100 = "i"),B1:B100))
=STDEV.S(IF((A1:A100 = "a")*(B1:B100>12),B1:B100))
=STDEV.S(IF((A1:A100 = "a")*(B1:B100>12)+(A1:A100 = "c")*(B1:B100>12)+(A1:A100 = "e")*(B1:B100>12)+(A1:A100 = "h")*(B1:B100>12)+(A1:A100 = "i")*(B1:B100>12),B1:B100))

The first issue I had is that there isn't a worksheet function for an IF statement so I reworked the calculations to the following:

=STDEV.S(FILTER(B1:B100, A1:A100 = "a"))
=STDEV.S(FILTER(B1:B100,(A1:A100="a")+(A1:A100="c")+(A1:A100="e")+(A1:A100="h")+(A1:A100="i")))
=STDEV.S(FILTER(B1:B100, (A1:A100 = "a")*(B1:B100>12)))
=STDEV.S(FILTER(B1:B100, (A1:A100 = "a")*(B1:B100>12)+(A1:A100 = "c")*(B1:B100>12)+(A1:A100 = "e")*(B1:B100>12)+(A1:A100 = "h")*(B1:B100>12)+(A1:A100 = "i")*(B1:B100>12)))

I was able to code the first calculation pretty simply:

VBA Code:
Sub TestStDev()
    Dim sdNum1 As Double

    sdNum1 = Application.WorksheetFunction.StDev_S(Application.WorksheetFunction.Filter(ActiveWorkbook.Sheets("Sheet1").Range("B1:B100"), Evaluate(ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""a""")))
    
    ActiveWorkbook.Sheets("Sheet1").Range("E1").Value = sdNum1
    
End Sub

But I am really struggling with the additional conditions. the EVALUATE statement seems to be what is really holding me up here. I have tried the following and much more, but everything gives me a 'Type Mismatch' error.

VBA Code:
x1 = Evaluate(ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""a""") + Evaluate(ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""c""") + Evaluate(ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""e""") + Evaluate(ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""h""") + Evaluate(ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""i""")
x2 = Evaluate(ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""a""") & "+" & (ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""c""") & "+" & (ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""e""") & "+" & (ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""h""") & "+" & (ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""i""")

I feel like I am just getting hung up in the syntax of the EVALUATE statement, but I also know that I am not the most efficient at coding so maybe there is a simpler way too.



test.xlsm
ABCDE
1a14.082.3638412.3638412.363841
2b11.332.6014062.601406
3c13.402.0638442.063844
4d16.622.2493412.249341
5e17.41
6f18.79
7g19.17
8h10.24
9i10.90
10j10.44
11a15.88
12b17.47
13c15.81
14d12.57
15e12.73
16f14.30
17g15.12
18h12.11
19i10.97
20j16.36
21a18.40
22b10.55
23c11.75
24d17.81
25e15.84
26f17.46
27g13.94
28h11.94
29i11.80
30j13.93
31a12.05
32b19.24
33c11.41
34d16.75
35e12.37
36f14.89
37g19.71
38h14.49
39i17.13
40j11.44
41a14.29
42b15.72
43c11.51
44d19.81
45e11.58
46f13.65
47g19.03
48h11.78
49i15.55
50j15.92
51a16.48
52b17.39
53c16.79
54d18.32
55e10.46
56f19.05
57g12.66
58h13.35
59i13.38
60j14.82
61a18.55
62b14.53
63c12.79
64d17.78
65e18.03
66f12.49
67g12.46
68h12.33
69i18.39
70j18.59
71a15.71
72b17.89
73c11.67
74d19.05
75e16.06
76f17.21
77g19.01
78h12.72
79i11.69
80j17.60
81a15.40
82b11.18
83c16.23
84d12.66
85e17.63
86f11.86
87g17.17
88h14.22
89i19.75
90j14.85
91a11.41
92b10.51
93c13.54
94d16.83
95e14.41
96f17.05
97g12.62
98h12.15
99i19.02
100j19.64
Sheet1
Cell Formulas
RangeFormula
C1C1=STDEV.S(IF(A1:A100 = "a",B1:B100))
D1D1=STDEV.S(FILTER(B1:B100, A1:A100 = "a"))
C2C2=STDEV.S(IF((A1:A100 = "a")+(A1:A100 = "c")+(A1:A100 = "e")+(A1:A100 = "h")+(A1:A100 = "i"),B1:B100))
D2D2=STDEV.S(FILTER(B1:B100,(A1:A100="a")+(A1:A100="c")+(A1:A100="e")+(A1:A100="h")+(A1:A100="i")))
C3C3=STDEV.S(IF((A1:A100 = "a")*(B1:B100>12),B1:B100))
D3D3=STDEV.S(FILTER(B1:B100, (A1:A100 = "a")*(B1:B100>12)))
C4C4=STDEV.S(IF((A1:A100 = "a")*(B1:B100>12)+(A1:A100 = "c")*(B1:B100>12)+(A1:A100 = "e")*(B1:B100>12)+(A1:A100 = "h")*(B1:B100>12)+(A1:A100 = "i")*(B1:B100>12),B1:B100))
D4D4=STDEV.S(FILTER(B1:B100, (A1:A100 = "a")*(B1:B100>12)+(A1:A100 = "c")*(B1:B100>12)+(A1:A100 = "e")*(B1:B100>12)+(A1:A100 = "h")*(B1:B100>12)+(A1:A100 = "i")*(B1:B100>12)))
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I solved this, it was a syntax issue. If you think you have enough quotes, you don't.


VBA Code:
x2 = Evaluate("(" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""a"")  + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""c"") + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""e"") + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""h"") + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""i"")")

I think this is what works, but it is difficult to check on my phone. I will double check, post a complete solution, Nd mark this as solved when I get home.
 
Upvote 0
Here is my full solution. some of the lines are super long, so if someone else has a more efficient/elegant solution I would love to hear it.

VBA Code:
Sub TestStDev()
    Dim sdNum1 As Double
    Dim sdNum2 As Double
    Dim sdNum3 As Double
    Dim sdNum4 As Double

    sdNum1 = Application.WorksheetFunction.StDev_S(Application.WorksheetFunction.Filter(ActiveWorkbook.Sheets("Sheet1").Range("B1:B100"), Evaluate(ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""a""")))
    sdNum2 = Application.WorksheetFunction.StDev_S(Application.WorksheetFunction.Filter(ActiveWorkbook.Sheets("Sheet1").Range("B1:B100"), Evaluate("(" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""a"") + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""c"") + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""e"") + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""h"") + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""i"")")))
    sdNum3 = Application.WorksheetFunction.StDev_S(Application.WorksheetFunction.Filter(ActiveWorkbook.Sheets("Sheet1").Range("B1:B100"), Evaluate("(" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""a"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12)")))
    sdNum4 = Application.WorksheetFunction.StDev_S(Application.WorksheetFunction.Filter(ActiveWorkbook.Sheets("Sheet1").Range("B1:B100"), Evaluate("(" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""a"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12) + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""c"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12) + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""e"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12) + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""h"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12) + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""i"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12)")))
    
    ActiveWorkbook.Sheets("Sheet1").Range("E1").Value = sdNum1
    ActiveWorkbook.Sheets("Sheet1").Range("E2").Value = sdNum2
    ActiveWorkbook.Sheets("Sheet1").Range("E3").Value = sdNum3
    ActiveWorkbook.Sheets("Sheet1").Range("E4").Value = sdNum4
    
End Sub
 
Upvote 0
Solution
You can shorten the formula in D4 like
Excel Formula:
=STDEV.S(FILTER(B1:B100, (ISNUMBER(XMATCH(A1:A100, {"a","c","e","h","i"})))*(B1:B100>12)))
 
Upvote 0
You can also shorten the code like
VBA Code:
Sub TestStDev()
    Dim sdNum1 As Double
    Dim sdNum2 As Double
    Dim sdNum3 As Double
    Dim sdNum4 As Double

   With ActiveWorkbook.Sheets("Sheet1").Range("A1:B100")
      sdNum1 = .Worksheet.Evaluate("StDev.S(filter(" & .Columns(2).Address & "," & .Columns(1).Address & "=""a""))")
      sdNum2 = .Worksheet.Evaluate("StDev.S(Filter(" & .Columns(2).Address & ",isnumber(xmatch(" & .Columns(1).Address & ",{""a"",""c"",""e"",""h"",""i""}))))")
'    sdNum3 = Application.WorksheetFunction.StDev_S(Application.WorksheetFunction.Filter(ActiveWorkbook.Sheets("Sheet1").Range("B1:B100"), Evaluate("(" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""a"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12)")))
'    sdNum4 = Application.WorksheetFunction.StDev_S(Application.WorksheetFunction.Filter(ActiveWorkbook.Sheets("Sheet1").Range("B1:B100"), Evaluate("(" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""a"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12) + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""c"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12) + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""e"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12) + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""h"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12) + (" & ActiveWorkbook.Sheets("Sheet1").Range("A1:A100").Address & "=""i"") * (" & ActiveWorkbook.Sheets("Sheet1").Range("B1:B100").Address & ">12)")))
   End With
   With ActiveWorkbook.Sheets("Sheet1")
      .Range("E1").Value = sdNum1
      .Range("E2").Value = sdNum2
      .Range("E3").Value = sdNum3
      .Range("E4").Value = sdNum4
   End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
Members
452,616
Latest member
intern444

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