VBA - 如果满足条件,则将公式放入另一列

时间:2017-06-16 14:02:04

标签: excel vba excel-vba

我在宏观上挣扎,我希望能有这个:

我的表有A - AH列,行可变。如果A栏包含" Customer"那么它应该把公式(= $ K $ 1& V3)放到L列。换句话说,如果在第3行,A3列是" Customer&#34 ;,那么在L3上将公式。在第4行,A4列不是" Customer",而是其他东西,则单元格L4应该不受影响。我想将它用于所有单元格,这意味着宏应该识别,表格的末尾在哪里=因为行数将从一天起改变。

我已经有了这段代码:

Sub testFind()
Dim rng As Range
Dim rngFound As Range

Set rng = Range("A:A")

Set rngFound = rng.Find("Customer")

If rngFound = "Customer" Then
    ActiveCell.FormulaR1C1 = "=R1C11&RC[10]"

Else


End If


End Sub

但它显然不起作用,显然:)。

非常感谢任何建议!

2 个答案:

答案 0 :(得分:1)

如果您想使用VBA解决方案,可以使用下面的代码(代码中的说明作为注释):

Option Explicit

Sub testFind()

Dim LastRow As Long
Dim rng As Range, C As Range

With Worksheets("Sheet1") ' <-- replace "Sheet1" with your sheet's name
    LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' last row in column A
    Set rng = .Range("A2:A" & LastRow) ' set the dynamic range to be searched

    ' loop through all cells in column A
    For Each C In rng
        If C.Value = "Customer" Then
            C.Offset(, 11).Formula = "=$K$1&V" & C.Row ' use offset to put the formula on column "L"
        End If
    Next C
End With

End Sub

注意:如果列中的值为&#34; A&#34;由&#34;客户&#34;组成。 (之前和之后不必完全匹配),然后使用以下行:

If C.Value Like "*Customer*" Then

答案 1 :(得分:0)

单元格(Rows.Count,&#34; A&#34;)。End(xlUp).Row - 可用于查找表格中的最后一行 并使用If来检查A列是否包含&#34; customer&#34; -

    If Activesheet.formulaRC[-10]= "customer" then
        ActiveCell.FormulaR1C1 = "=RC[-1]&RC[11]"
    end if

现在循环遍历行

相关问题