Very new to vba and I'm having some issues with a piece of code. In essence, I'm trying to perform different worksheet functions on user selected items from a list box in a user form.
Private Sub cmdRunStat_Click()
Dim averageValue As Single
Dim sdValue As Single
Dim maxValue As Variant
Dim minValue As Single
Dim modeValue As Single
Dim UserRange As String, sheetName As String
Set UserRange = ListBox1.Selected = True
If optAverage.Value = True Then
averageValue = WorksheetFunction.Average(UserRange)
MsgBox "The average of the selected data is " & averageValue
ElseIf optSD.Value = True Then
sdValue = WorksheetFunction.StDev(UserRange)
MsgBox "The standard deviation of the selected data is " & sdValue
ElseIf optMax.Value = True Then
maxValue = WorksheetFunction.Max(UserRange)
MsgBox "The maximum of the slected data is " & maxValue
ElseIf optMin.Value = True Then
minValue = WorksheetFunction.Min(UserRange)
MsgBox "The minimum of the slected data is " & minValue
Else
modeValue = WorksheetFunction.Mode(UserRange)
MsgBox "The mode of the slected data is " & modeValue
End If
End Sub
Set UserRange = ListBox1.Selected = True?Setvalue to string . this is wrongSet UserRange = ListBox1.Selected = TrueSet UserRange = ListBox1.Value, just remember that string need to contain a format likeWorksheets("Sheet1").Range("A1:D10")