我想通过使用函数将所选列的范围缩小为仅包含非空白单元格。
大多数其他线程用于sub。
我仍然是VBA的初学者。我尝试以多种方式修改以下代码,但无法正常工作。如何退回缩小范围?
Function RangeReduce(rng As Range) As Range
Dim first_row, last_row As Double
first_row = rng.Cells(1, 1).Address
last_row = rng.Cells(Rows.Count).End(xlUp).Address
Set RangeReduce = Range(first_row, last_row)
End Function
答案 0 :(得分:0)
您可以使用SpecialCells
的{{1}}修饰符(?不确定是什么意思)。另外,我假设您肯定有常量,但是可能没有公式。
Range()
您可以像这样拨打电话:
Function RangeReduce(rng As Range) As Range
Dim constRng As Range, formulaRng As Range
On Error Resume Next
Set constRng = rng.SpecialCells(xlCellTypeConstants)
Set formulaRng = rng.SpecialCells(xlCellTypeFormulas)
On Error GoTo 0
If formulaRng Is Nothing Then
Set RangeReduce = constRng
Else
Set RangeReduce = Union(constRng, formulaRng)
End If
End Function