如何在Excel中使用2个范围

时间:2015-06-25 12:54:36

标签: vba excel-vba excel

这是我的代码,它在我使用1个范围时起作用,但如果我使用2个或更多它不起作用。 我真的不知道如何修复我的代码。任何帮助将非常感激

$basedir = 'C:\ppp\nnn\trunk\www\client'
$pattern = [regex]::Escape("^$basedir\")

Get-ChildItem -Path "$basedir\nnn" -Filter *.js -Recurse -File |
  Sort-Object Length -Descending |
  ForEach-Object { $_.FullName -replace $pattern }

任何一个例子都会很棒。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

Option Explicit

Function CUSTOMAVERAGE(ParamArray ranges())
    Dim rng As Range
    Dim part As Variant
    Dim cell As Range
    Dim i As Double
    Dim suma As Double
    Dim sk As Double
    Dim min As Double
    Dim max As Double
    Dim vidurkis As Double
    Dim dup As Double
    Dim sk1 As Double
    Dim ddown As Double
    CUSTOMAVERAGE = CVErr(xlErrNA)
    Set rng = Nothing
    For Each part In ranges
        If TypeName(part) = "Range" Then
            If TypeName(rng) = "Range" Then
                Set rng = Union(rng, part)
            Else
                Set rng = part
            End If
        End If
    Next
    If rng Is Nothing Then Exit Function
    suma = 0
    sk = 0
    min = 1.79769313486231E+308
    max = -1.79769313486231E+308
    For Each cell In rng
        suma = suma + cell.Value
        sk = sk + 1
        If min > cell.Value Then min = cell.Value
        If max < cell.Value Then max = cell.Value
    Next
    vidurkis = suma / sk
    sk = 0
    dup = 0
    sk1 = 0
    ddown = 0
    For Each cell In rng
        If vidurkis < cell.Value Then
            dup = dup + cell.Value
            sk = sk + 1
        ElseIf vidurkis > cell.Value Then
            ddown = ddown + cell.Value
            sk1 = sk1 + 1
        End If
    Next cell
    If sk = 0 Or sk1 = 0 Then Exit Function
    dup = dup / sk
    ddown = ddown / sk1
    CUSTOMAVERAGE = "V=" & CStr(vidurkis) & " Min=" & CStr(min) & " Max=" & CStr(max) & " Dup=" & CStr(dup) & " Ddown=" & CStr(ddown)
End Function