这可能会给你一些想法:
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