【问题标题】:Automate formula to selected rows for vise versa calculation将公式自动化到选定的行以进行反之亦然计算
【发布时间】:2020-07-02 16:51:04
【问题描述】:

我想将下面的公式填写到第 4 行到第 40 行:

If Target.Address = Range("E13").Address Then
        ActiveSheet.Range("H13").Value = ActiveSheet.Range("E13").Value * ActiveSheet.Range("G13")
    ElseIf Target.Address = Range("H13").Address Then
        ActiveSheet.Range("E13").Value = ActiveSheet.Range("H13").Value / ActiveSheet.Range("G13")
    End If

我该怎么做?

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    根据您对 Target 的使用,我假设代码来自 Worksheet_Change 事件过程。

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        if not intersect(target, Range("E4:E40, H4:H40")) is nothing then
            on error goto safe_exit
            application.enableevents = false
            dim t as range 
            for each t in intersect(target, Range("E4:E40, H4:H40"))
                select case t.column
                    case 5
                        cells(t.row, "H") = cells(t.row, "E") * cells(t.row, "G")
                    case 8
                        'you might want to make sure cells(t.row, "G") isn't zero
                        cells(t.row, "E") = cells(t.row, "H") / cells(t.row, "G")
                end select
            next t
        End If
    
    safe_exit:
        application.enableevents = true
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 1970-01-01
      • 2011-02-02
      相关资源
      最近更新 更多