If CheckBox1.Value = True Then
Range("H5:H38").Value = Range("H4").Value
使列值h5直到H38变得与单元格h4中的值相同。
If CheckBox1.Value = False Then
现在我需要知道数组中H5:H38的值如何,以便当用户取消选中该复选框时,返回值在事件之前是相同的。 也就是说,在转换值h5之前,h4中包含的值需要保存数组中的值,以便在用户取消选中复选框时撤消操作。谢谢你的帮助。
答案 0 :(得分: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"))
答案 1 :(得分: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").Value
和Range("H5:H38").Value = rangeFormulas
,但我认为如果你恢复了具有相同可见结果的公式会更好。