如何识别每列中的usedrange并将其插入公式中?

时间:2019-04-08 12:18:56

标签: excel vba

有人可以建议如何识别每一列中的usedrange并将其插入公式吗?

问题是我大约有1300列,每列具有不同数量的填充单元格(在行1:380之间)。因此,例如A列的数据范围可能=到A7:A251,B列的数据范围可能=到B68:B94,依此类推。

我需要做的是为每一列拉出RANGE(我知道如何处理其余的列),例如D45:D195

该公式对于excel是非标准的,因此请忽略“ ACF”位。我的附加组件中的标准ACF公式应为=ACF(RANGE,1)

我已经开始研究某些东西,但我认为它不能满足我的需要,因为它只会拉出最后填充的行的数目。

Sub Range1()

Dim LastRow As Integer

LastRow = ActiveSheet.UsedRange.Rows.Count

ActiveSheet.Range("G1") = LastRow

ActiveSheet.Range("H1").Formula = "=ACF(" & "D" & LastRow & ":" & "D" & ",1)"

End Sub

1 个答案:

答案 0 :(得分:3)

This should take into account differing start and end cells in each column.

You find the first used cell by starting at the last row of the sheet and looping forward to the top.

You find the last used cell by starting at the first row and looping back to the bottom.

Sub Range1()

Dim c As Long, r1 As Range, r2 As Range, r As Range

For c = 1 To 10
    Set r1 = Columns(c).Find(What:="*", after:=Cells(Rows.Count, c), searchdirection:=xlNext) 'first used row
    Set r2 = Columns(c).Find(What:="*", after:=Cells(1, c), searchdirection:=xlPrevious)      'last used row
    Set r = Range(r1, r2)                                                                     'used range
    Cells(r2.row + 1, c).Formula = "=ACF(" & r.Address & ",1)"
Next c

End Sub

enter image description here