excel 多个活动单元行的总和

qlckcl4x  于 2023-06-07  发布在  其他
关注(0)|答案(2)|浏览(148)

我能够在活动单元格行中获得表中特定列之间的计算。但是我找不到一种方法来获得多个活动单元格行之间的计算。它只是从我点击的最后一行开始计算,而忽略了前一行。
我希望能够点击任何列,仍然得到所需的值,因为我需要计算多个值
这是最接近的代码。我试过使用其他方程和公式,但只有“和”是工作的这种格式。

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Sheet1.Range("A1").Formula = "=SUM(" & Sheet1.Range("H" & (ActiveCell.Row)).Address(False, False) & "," & _
Sheet1.Range("K" & (ActiveCell.Row)).Address(False, False) & ")"
End Sub

附加所需输出的剪切IMAGE OF THE DESIRED OUTPUT
非常感谢

2nbm6dog

2nbm6dog1#

尝试
更新为仅与列W相交

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim rng As Range
    Dim selectedSum As Double
    
    On Error Resume Next
    Set rng = Intersect(Target, Me.Columns("W"))
    On Error GoTo 0
    
    If Not rng Is Nothing Then
        selectedSum = WorksheetFunction.Sum(rng)
        Me.Range("C11").Value = selectedSum
    End If
End Sub
wnrlj8wa

wnrlj8wa2#

下面的代码将在您在工作表中单击的任何位置触发,并在单元格“C11”中写入列W的Sum:

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    Dim LastRow As Long, SumRng As Range
    Dim MySum As Double
    
    Application.ScreenUpdating = False
    
    LastRow = Cells(Rows.Count, "W").End(xlUp).Row '  get last row with data in column W
    Set SumRng = Range("W1:W" & LastRow) ' set the range object
  
    MySum = WorksheetFunction.Sum(SumRng)
    Range("C11").Value = MySum
    
    Application.ScreenUpdating = True
    
End Sub

相关问题