0

我有一个 CheckBox 并点击类似这样的事件:

If CheckBox1.Value = True Then
Range("H5:H38").Value = Range("H4").Value

使 h5 到 H38 的列值与单元格 h4 中的值相同。


If CheckBox1.Value = False Then

现在我需要知道H5:H38的值如何在一个数组中,以便当用户取消选中复选框时,返回值与事件之前相同。也就是在转换h5:h38中的值之前h4中包含的值需要将值保存在一个数组中才能在用户取消勾选复选框的情况下撤消该动作。谢谢您的帮助。

4

2 回答 2

1

这是一个将任何范围转换为数组的函数:

Public Function CreateArrayFromRange(yourRange As Range)
Dim retVal() As Variant, xCell As Range, upperIndex As Integer, tic As Integer

    If yourRange.Cells.Count > 0 Then
        upperIndex = yourRange.Cells.Count - 1
        ReDim retVal(0 To upperIndex)
        tic = -1
        For Each xCell In yourRange
            tic = tic + 1
            retVal(tic) = xCell.Value
        Next xCell
        CreateArrayFromRange = retVal
    Else
        CreateArrayFromRange = Array("ERROR")
    End If

End Function

这个的实现看起来像这样。

Dim myArray()
myArray = CreateArrayFromRange(Range("H5:H38"))
于 2012-10-01T22:19:19.503 回答
0

这是执行您要求的操作的专家。因为你说你需要知道怎么做,所以我在几行之前添加了一些评论,以完全清楚它是如何工作的。

Option Explicit
'Declare a private variant to store the range formulas
'Variable is declared as private so that it remains in scope within the module
'Variable could also be declared as static within the sub with the same results.
Private rangeFormulas As Variant
Private Sub CheckBox1_Click()
If CheckBox1.Value = True Then
    'Transfer the range formulas to the variant.
    rangeFormulas = Range("H5:H38").Formula
    Range("H5:H38").Value = Range("H4").Value
Else
    'Restore the formulas from the private variant variable.
    Range("H5:H38").Formula = rangeFormulas 
End If
End Sub

从技术上讲,我可以使用rangeFormulas = Range("H5:H38").Valueand Range("H5:H38").Value = rangeFormulas,但我认为如果您恢复具有相同可见结果的公式会更好。

于 2012-10-01T00:26:16.277 回答