如何在VBA中编写最大函数?

时间:2017-04-22 15:36:33

标签: excel vba

这是我的代码,但它似乎不起作用。我不知道为什么......

Function maximum()
Dim i As Integer
Dim dernLigne As Long
Dim somme as Variant
somme = 0
lastLigne = Range("C65536").End(xlUp).Row
Range("C65536").Value = valuemax
i = 2
While i <= lastLigne
    If Range("C" & i).Value > valeurmax Then
        valuemax = Range("C" & i).Value
        i = i + 1
    End If
Wend
maximum = valeurmax
End Function

由于

1 个答案:

答案 0 :(得分:2)

以下是两个需要考虑的功能:

Option Explicit

Function maxFixedRange() As Double

Dim i As Long
Dim ws As Worksheet

Set ws = ThisWorkbook.Worksheets("Sheet1")
For i = 2 To ws.Cells(ws.Rows.Count, "C").End(xlUp).Row
    If IsNumeric(ws.Cells(i, 3).Value2) Then
        If ws.Cells(i, 3).Value2 > maxFixedRange Then
            maxFixedRange = ws.Cells(i, 3).Value2
        End If
    End If
Next i

End Function
Function maxVariableRange(rng As Range) As Double

Dim cell As Range

For Each cell In rng
    If IsNumeric(cell.Value2) Then
        If cell.Value2 > maxVariableRange Then
            maxVariableRange = cell.Value2
        End If
    End If
Next cell

End Function

第一个函数在固定范围中查找最大值。这意味着您无法在该功能的不同范围内查找最大值。

第二个功能是期待一系列细胞。在寻找最大值时,将考虑该范围内的所有单元格。

enter image description here