将公式插入单元格;修改我的代码

时间:2013-01-21 17:23:27

标签: vba excel-vba excel

我目前有这段代码插入一个新行,在行的第二个单元格上使用验证:

Sub RICH()
'
' Macro3 Macro
   Dim ws As Worksheet
   Dim fnd As Range
   Dim fndstr As String

   fndstr = "Targeted Premium Ads"
   Set ws = Worksheets("Inputsheet")

        Set fnd = ws.Columns(2).Find(what:=fndstr, After:=ws.Range("B11"), _
        LookIn:=xlValues, lookat:=xlPart, searchorder:=xlByColumns, _
        searchdirection:=xlNext, MatchCase:=False)

        If Not fnd Is Nothing Then

            Rows(fnd.Row - 1).Select
            Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            Range("B" & fnd.Row - 2).Select
            With Selection.Validation
                .Delete
                .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, Operator:= _
                xlBetween, Formula1:="=Suppliers!$B$2:$B$178"
                .IgnoreBlank = True
                .InCellDropdown = True
                .InputTitle = ""
                .ErrorTitle = ""
                .InputMessage = ""
                .ErrorMessage = ""
                .ShowInput = True
                .ShowError = True



            End With
        End If
End Sub

但是,我现在想要插入两个函数,比如:= sum(A $ 4,B $ 5)和= sum(A $ 9,C $ 3)分别到这个新输出的行的列N,O中。什么是正确的方法?

1 个答案:

答案 0 :(得分:0)

怎么样:

Range("N" & fnd.Row - 2).Formula = "=SUM(A$4,B$5)"
Range("O" & fnd.Row - 2).Formula = "=SUM(A$9,C$3)"

(假设fnd.Row - 2是您要放置公式的行。)