Excel:不包括非数字行的百分比更改宏



我在Excel中遇到了一些问题,如果可能的话,我想得到一个宏来帮助我。

问题:

我需要为一行中的一系列列找到百分比差异(A1:A54)。类似=((max(xy:xy))-(MIN(xy:xy))/(MIN(xy:xy)

但是

我需要excel来从公式中排除包含非数字内容的行(随机位置有很多行)。

我需要一些通用的东西,这样我就可以在不同的工作簿等中运行它。

类似的东西会忽略空白和文本单元格

Function percentdifference(r As Range) As Double
    With Application.WorksheetFunction
        percentdifference = (.Max(r) - .Min(r)) / .Min(r)
    End With
End Function

您可以在宏中使用类似以下的"Helper"函数来检查单元格中的数据类型,并且只有当所有单元格都是"Numeric"类型时才应用公式。

Function CellType(c) 
     '   Returns the cell type of the upper left
     '   cell in a range
    Application.Volatile 
    Set c = c.Range("A1") 
    Select Case True 
    Case IsEmpty(c): CellType = "Blank" 
    Case Application.IsText(c): CellType = "Text" 
    Case Application.IsLogical(c): CellType = "Logical" 
    Case Application.IsError(c): CellType = "Error" 
    Case IsDate(c): CellType = "Date" 
    Case InStr(1, c.Text, ":") <> 0: CellType = "Time" 
    Case c.HasFormula: CellType = "Formula" 
    Case IsNumeric(c): CellType = "Numeric" 
    End Select 
End Function 

最新更新