1

我有一个表(A3-6),它根据输入到表(A1)上方的单元格中的数字来取消隐藏行。在行列 A3-6 中,用户在具有数据验证列表的单元格中选择一个选项。现在我要做的是为每个隐藏单元格将隐藏单元格中的值设置为默认选项。

Dim i As Long
        For i = 2 To 10
            If (Rows("2:10").Hidden = True) Then
                'Set the default values for the cell?
        End If
        Next i

此代码根据输入到 A1 中的数字隐藏行

If Target.Address = "$A$1" Then
        Rows("3:6").Hidden = True
        Rows("2:" & 2 + Val(Target.Value)).Hidden = False
    End If 

在此处输入图像描述

4

1 回答 1

1

这可能会给你一些想法:

Sub resetHiddenRangeValue()

    Dim rngMyRange As Range, rngVisible As Range, rngCell As Range

    Set rngMyRange = Range("A3:A6")
    Set rngVisible = Range("A3:A6").SpecialCells(xlCellTypeVisible)

    For Each rngCell In rngMyRange
        If Intersect(rngCell, rngVisible) Is Nothing Then
            ' Is the default value rngcell.offset(0,6).value?
            rngCell.Value = "My Default Value" 
        End If
    Next rngCell

End Sub

可以避免循环,但它有点棘手:

Sub resetHiddenRangeValueNoLooping()

    Dim rngMyRange As Range, rngVisible As Range, rngHidden As Range, varVisible As Variant

    Set rngMyRange = Range("A3:A6")
    Set rngVisible = Range("A3:A6").SpecialCells(xlCellTypeVisible)

    ' Trick: store the initial values of the visible range to a variant array
    varVisible = rngVisible.Value

    ' Substitute the contents of rngVisible with something random
    rngVisible.Value = "Blah_Blah_Blah"

    ' Use the Columndifferences method to set the hidden range
    Set rngHidden = rngMyRange.ColumnDifferences(rngVisible(1, 1))

    ' Fill up the range with the default value-can also be an array or range of values
    rngHidden.Value = "My Default Value"

    ' restore the original values of the visible range
    rngVisible.Value = varVisible

End Sub
于 2013-07-09T10:00:33.780 回答