Error 1004 VBA Unable to get StDev property of Worksheet Class

HenkBroam

New Member
Joined
May 4, 2023
Messages
2
Office Version
  1. 2019
Platform
  1. Windows
Hello everyone,

Iam trying to remove outliers in a graph.
Were in collumn A is the time (1,2,3,4,5 etc)
Collumn B are the values (15,10,1,4,5,6 etc)
Iam trying to remove the outliers from the grahp but iam getting the error 1004 Unable to get StDev property of Worksheet Class.
this is my code
VBA Code:
Sub RemoveOutliers()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Blad1") ' Replace "Sheet1" with your sheet name
    
    Dim lastRow As Long
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    
    Dim mean As Double, sd As Double
    Dim i As Long, j As Long
    
    For i = 2 To lastRow ' Assumes data starts in row 2
        mean = WorksheetFunction.Average(ws.Range("B" & i))
        sd = Application.WorksheetFunction.StDev_S(ws.Range("B" & i))
        For j = 1 To 1 ' Only one column
            If Abs(ws.Cells(i, j + 1).Value - mean) > (2 * sd) Then
                ws.Cells(i, j + 1).ClearContents
            End If
        Next j
    Next i
End Sub
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
What is the point in taking the average of one cell? Or attempting the SD of one cell?
 
Upvote 0
What is the point in taking the average of one cell? Or attempting the SD of one cell?
I dont mean to take the average or stdev of 1 cell. But for the whole B column. I thought this was the correct code, if not what should i change?
 
Upvote 0
Code:
mean = WorksheetFunction.Average(ws.Range("B2:B" & lastrow ))
and the same for stdev. Both should be before the For loop.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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