By: Musiha Mahfuza Mukta

In reply to Dave Gilblom.

Thank you Dave Gilblom, for your comment. Yes, Excel can do this. You should use some VBA codes for this. Below, I am attaching these codes.
–> In the Module 1 write the following code. Which will return the Sum of selected cells.

Sub CalculateSum()
Dim selectedRange As Range
Dim selectedArea As Range
Dim cell As Range
Dim sumValue As Double
On Error Resume Next
Set selectedRange = Application.Selection
On Error GoTo 0
If Not selectedRange Is Nothing Then
sumValue = 0
For Each selectedArea In selectedRange.Areas
For Each cell In selectedArea
If IsNumeric(cell.Value) Then
sumValue = sumValue + cell.Value
End If
Next cell
Next selectedArea
MsgBox "Sum: " & sumValue
Else
MsgBox "No valid range selected."
End If
End Sub

–> In the Module 2 write the following code. Which will return the Average of selected cells.

Sub CalculateSelectedAverage()
Dim selectedRange As Range
Dim avgValue As Double
' Check if any cells are selected
If Selection.Cells.count > 0 Then
' Set the selected range
Set selectedRange = Selection
' Calculate the average of the selected range
avgValue = WorksheetFunction.Average(selectedRange)
' Display the result in a message box
MsgBox "The average value of selected cells is: " & avgValue
Else
MsgBox "No cells are currently selected."
End If
End Sub

–> In the Module 3 write the following code. Which will return the Median of selected cells.

Sub CalculateSelectedMedian()
Dim selectedRange As Range
Dim cell As Range
Dim valuesArray() As Double
Dim medianValue As Double
Dim i As Long, j As Long
Dim temp As Double
' Check if any cells are selected
If Selection.Cells.count > 0 Then
' Set the selected range
Set selectedRange = Selection
' Copy selected cell values to an array
ReDim valuesArray(1 To selectedRange.Cells.count)
i = 1
For Each cell In selectedRange
valuesArray(i) = cell.Value
i = i + 1
Next cell
' Sort the values array using bubble sort (simple and not efficient)
For i = LBound(valuesArray) To UBound(valuesArray) - 1
For j = i + 1 To UBound(valuesArray)
If valuesArray(j) < valuesArray(i) Then
temp = valuesArray(i)
valuesArray(i) = valuesArray(j)
valuesArray(j) = temp
End If
Next j
Next i
' Calculate the median based on sorted values array
If UBound(valuesArray) Mod 2 = 0 Then
medianValue = (valuesArray(UBound(valuesArray) \ 2) + valuesArray(UBound(valuesArray) \ 2 + 1)) / 2
Else
medianValue = valuesArray(UBound(valuesArray) \ 2 + 1)
End If
' Display the result in a message box
MsgBox "The median value of selected cells is: " & medianValue
Else
MsgBox "No cells are currently selected."
End If
End Sub

–> In the Module 4 write the following code. Which will return the standard deviation of selected cells.

Sub CalculateSelectedStdDeviation()
Dim selectedRange As Range
Dim cell As Range
Dim valuesArray() As Double
Dim sum As Double
Dim mean As Double
Dim varianceSum As Double
Dim stdDeviation As Double
Dim count As Long
' Check if any cells are selected
If Selection.Cells.count > 0 Then
' Set the selected range
Set selectedRange = Selection
' Copy selected cell values to an array
ReDim valuesArray(1 To selectedRange.Cells.count)
count = 0
For Each cell In selectedRange
valuesArray(count + 1) = cell.Value
sum = sum + cell.Value
count = count + 1
Next cell
' Calculate the mean
mean = sum / count
' Calculate the sum of squared differences for variance
For i = 1 To count
varianceSum = varianceSum + (valuesArray(i) - mean) ^ 2
Next i
' Calculate the variance and standard deviation
If count > 1 Then
variance = varianceSum / (count - 1)
stdDeviation = Sqr(variance)
Else
variance = 0
stdDeviation = 0
End If
' Display the result in a message box
MsgBox "The standard deviation of selected cells is: " & stdDeviation
Else
MsgBox "No cells are currently selected."
End If
End Sub

–> Finally, in the ThisWorkbook >> write the following code.

Private Sub Workbook_Open()
Dim cBar As CommandBar
Set cBar = Application.CommandBars("Cell")
With cBar.Controls.Add(Type:=msoControlButton, Before:=1)
.OnAction = "'" & ThisWorkbook.Name & "'!CalculateSum"
.Caption = "Sum"
.FaceId = 213
.Tag = "My_Cell_Control_Tag"
End With
With cBar.Controls.Add(Type:=msoControlButton, Before:=1)
.OnAction = "'" & ThisWorkbook.Name & "'!CalculateSelectedAverage"
.Caption = "Average"
.FaceId = 17
.Tag = "My_Cell_Control_Tag"
End With
With cBar.Controls.Add(Type:=msoControlButton, Before:=1)
.OnAction = "'" & ThisWorkbook.Name & "'!CalculateSelectedMedian"
.Caption = "Median"
.FaceId = 50
.Tag = "My_Cell_Control_Tag"
End With
With cBar.Controls.Add(Type:=msoControlButton, Before:=1)
.OnAction = "'" & ThisWorkbook.Name & "'!CalculateSelectedStdDeviation"
.Caption = "StdDeviation"
.FaceId = 2130
.Tag = "My_Cell_Control_Tag"
End With
End Sub

–> Now, save the code >> press on Run button >> close the Excel file >> re open the file >> select some cells >> right click >> from the Context Menu Bar.

–> Then, choose the desire operation >> get the answer in MsgBox.