0

my intent of building a dynamic alternate cells range as failed, obviously for lack of knowledge.

For j = 1 To numFrame
    Set MaxStRange_OD_OUT = ActiveSheet.Range(Cells(1, 2 * (j + 1)))
    MaxVal = Application.Max(MaxStRange_OD_OUT)
    Debug.Print MaxVal
Next j

what I would like to achive is

MaxStRange_OD_OUT = ActiveSheet.Range(Cells(1, 4),Cells(1, 6),Cells(1, 8))

of course the number of cells can vary let say from 1 to 10 (numFrame)

thanks

4

2 回答 2

1

您可以在此处尝试此代码

Function MyMaxValue() as Double
    Dim maxValue As Double, numFrame As Double, i As Integer, currentValue As Double

    numFrame = 7

    For i = 1 To numFrame
        currentValue = Cells(1, 2 * (j + 1)).value
        If currentValue > maxValue Then
            maxValue = currentValue
            Debug.Print "new value = " & maxValue
        End If
    Next i

    MyMaxValue = maxValue    
End Function
于 2013-11-29T16:49:56.623 回答
1

你可以Application.Union用来建立一个范围。

Sub UnionRangeExample()

Dim rng As Range
Dim i As Long

    For i = 1 To 20 Step 2

    If rng Is Nothing Then

    Set rng = ActiveSheet.Cells(i, 1)

    Else

    Set rng = Application.Union(rng, ActiveSheet.Cells(i, 1))
    End If

    Next i

MsgBox rng.Address

End Sub

在您的情况下,您可以像这样使用它:

Sub UnionRangeExample_v2()

Dim rng As Range
Dim j As Long
Dim numFrame As Long


numFrame = 10


    For j = 1 To numFrame
        If rng Is Nothing Then

        Set rng = ActiveSheet.Cells(1, 2 * (j + 1))

        Else

        Set rng = Application.Union(rng, ActiveSheet.Cells(1, 2 * (j + 1)))
        End If
    Next j

MsgBox "max for: " & rng.Address & " is " & Application.Max(rng)

End Sub
于 2013-11-29T16:44:21.960 回答