I am trying to write a function that automatically deletes the minimum value for a given selection of cells. I know how to find the minimum value but I just don’t know how to delete that value.
Here’s what I’ve got.
MinDel = Application.Worksheetfunction.min(stuff)
How do I delete the MinDel value?
You could modify the function from here like that
Option Explicit Function AddressOfMax(rng As Range) As Range Set AddressOfMax = rng.Cells(WorksheetFunction.Match(WorksheetFunction.Max(rng), rng, 0)) End Function Function AddressOfMin(rng As Range) As Range Set AddressOfMin = rng.Cells(WorksheetFunction.Match(WorksheetFunction.Min(rng), rng, 0)) End Function Sub TestIt() Dim rg As Range Dim rgMin As Range Set rg = ActiveSheet.Range("A1:A6") Set rgMin = AddressOfMin(rg) rgMin.Clear End Sub
As mentioned in the comments, a UDF (User Defined Function) cannot change a value or range in Excel by default and a
Sub should be used. This is my way to delete the minimal value in the
Public Sub DeleteMinimum() Dim myRange As Range Dim minValue As Double Dim myMin As Range If Not TypeOf Selection Is Excel.Range Then Exit Sub Dim valueAssigned As Boolean: valueAssigned = False minValue = 0 For Each myRange In Selection If IsNumeric(myRange) Then If Not valueAssigned Then valueAssigned = True minValue = myRange Set myMin = myRange Else If myRange < minValue Then minValue = myRange Set myMin = myRange End If End If End If Next myRange If Not myMin Is Nothing Then myMin = "DELETED!" End If End Sub
The procedure below will delete the lowest value in a selection, provided that the selection comprises more than one cell. It ignores all but the first column of the selection.
Sub DelMin() ' 05 Jan 2019 Dim Arr As Variant, i As Integer Dim Mm As Variant, m As Integer With Selection If .Cells.Count > 1 Then Arr = .Value For i = 1 To UBound(Arr) If Not IsEmpty(Arr(i, 1)) Then If IsEmpty(Mm) Or (Arr(i, 1) < Mm) Then Mm = Arr(i, 1) m = i End If End If Next i .Cells(m, 1).ClearContents End If End With End Sub