如何在VBA中编写最大function?
这是我的代码,但它似乎并没有工作。 我不知道为什么…
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
谢谢
这里有两个function供您考虑:
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
第一个函数在固定范围内查找最大值。 这意味着你不能在该函数的不同范围内寻找最大值。
第二个function是期待一个单元格的范围。 当查找最大值时,将考虑该范围内的所有单元格。