必须连续滤除大于100且小于-100的数字并找到最小值的函数

时间:2019-07-05 15:59:51

标签: excel vba

我正在研究VBA函数Loss,该函数读取另一个工作表的第11行并在该行中找到最小值。

但是,最小值的绝对值必须小于100。(该行同时显示百分比和常规值,我只需要最小值。)

此外,一旦有了第11行中的最小值,我就想通过函数Worst将上面单元格的值赋给变量。

这是我到目前为止所拥有的,这要归功于我问过的上一个问题的答案:

Function Loss(worksheet1 As Worksheet) As Double

    Dim min As Double
    Dim i As Integer
    Dim myRight As Long, Colcount As Long

    min = 100

    With worksheet1
        myRight = .Cells(1, .Columns.Count).End(xlToLeft).Column
        For Colcount = 4 To myRight
            If (.Cells(11, Colcount).Value < min) And (Abs(.Cells(11, Colcount).Value) <= 100) Then
                min = .Cells(11, Colcount).Value
            End If
        Next Colcount
    End With

    Loss = min

End Function
Function Worst(worksheet1 As Worksheet) As String

    Dim min As Double
    Dim i As Integer
    Dim myRight As Long, Colcount As Long

    min = 100

    With worksheet1
        myRight = .Cells(1, .Columns.Count).End(xlToLeft).Column
        For Colcount = 4 To myRight
             If (.Cells(11, Colcount).Value < min) And (Abs(.Cells(11, Colcount).Value) <= 100) Then
                 min = .Cells(11, Colcount).Value
                  Worst = .Cells(10, Colcount).Value
             End If
        Next Colcount
    End With

End Function

1 个答案:

答案 0 :(得分:0)

将困难的任务分解为工件。

Worst _ 
Сell_Minimum( _
Range_of_Values_without_Percent (_
Range_get))

'与大多数VBA程序员一样,您希望在一个过程中执行很多操作-这会导致复杂性。 '让我们简化-让我们编写简单的单音节方法,其方法将不可避免地解决问题。

Function Range_get(Optional ByVal sMsg As String) _
        As Range
' Your simple CODE
'
End Function

Function Range_of_Values_without_Percent(ByVal r As Range) _
        As Range
' Your simple CODE
'
End Function

Function Cell_Minimum(ByVal r As Range) _
        As Range
' Your simple CODE
'
End Function