查找文本值,偏移量,并根据偏移值输入公式

时间:2020-04-21 20:01:29

标签: excel vba

请问我还有另一个VBA问题。

  1. 我在Excel中有一个表,我想查找文本:“所有其他”将始终位于列B中,但可能不在同一行号中。

  2. 找到“所有其他”单元格后,我想在(0,1)的下一列中输入一个Sum公式。

  3. 该公式将求和未知范围,从Activecell向下3行开始到数据末尾。

我遇到一个错误:引用无效或不合格。

PrintScreen

enter image description here

我目前有:

Dim ws As Worksheet
Dim aOther As Range
Dim DataLastRow As Range

Set ws = ActiveSheet

Set DataLastRow = ws.Cells.Range(ws.Rows.Count, 1).End(xlUp).Rows

    Set aOther = ws.Range("B:B").Find("All Other", LookIn:=xlValues, lookat:=xlWhole)
    ActiveCell.Offset(0, 1).Formula = "=SUM(" & .Offset(3, 0) & DataLastRow & ")"

1 个答案:

答案 0 :(得分:1)

Dim ws As Worksheet
Dim aOther As Range
Dim DataLastRow As Long

Set ws = ActiveSheet

DataLastRow = ws.Cells(ws.Rows.Count, 3).End(xlUp).Row

    Set aOther = ws.Range("B:B").Find("All Other", LookIn:=xlValues, lookat:=xlWhole)

    If Not aOther is Nothing Then
        aOther.Offset(0, 1).Formula = "=SUM(" & aOther.Offset(3, 1).Address & ":" & ws.Cells(DataLastRow,3).Address & ")"
    Else
        MsgBox """All Other"" not found in column."
    End If