在 Win 10 中使用 Excel 365
我在定位/识别正在使用的工作表单元格时遇到了一个非常小的问题。例如,在单元格 A1 和单元格 H5 中有常量。单元格 B2 包含可溢出的动态数组常量:
={1,2,"",4,5,6;7,8,9,"",11,99;100,"",0,0,100,0}
因为这张表既有公式也有常量,我试了下我信得过的:
Sub LocateCellsWithStuffInThem()
Dim rng As Range
With ActiveSheet.Cells
Set rng = Union(.SpecialCells(xlCellTypeFormulas), .SpecialCells(xlCellTypeConstants))
End With
MsgBox rng.Address(0, 0)
End Sub
这给出:
我希望看到 B2:G4,A2,H5
。
显然正在使用像 D2 这样的单元格。它是数组常量的一部分,即使 SpecialCells
不认为它填充了公式或常量并且长度为零!
如何将代码写入容易定位的已占用单元格?我是否必须遍历 UsedRange
中的所有单元格?
最佳答案
您可以循环公式单元格并添加溢出区域(如果有):
Sub LocateCellsWithStuffInThem()
Dim rng As Range
Dim rng2 As Range
With ActiveSheet.Cells
Set rng = .SpecialCells(xlCellTypeConstants)
For Each rng2 In .SpecialCells(xlCellTypeFormulas).Cells
If rng2.HasSpill Then
Set rng = Union(rng2.SpillingToRange, rng)
Else
Set rng = Union(rng2, rng)
End If
Next rng2
End With
MsgBox rng.Address(0, 0)
End Sub
关于excel - 找到被占用的单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61938169/