B列第三行到最后一行的总和

时间:2019-05-23 14:20:21

标签: excel vba sum row

我遇到一个错误,使我无法正常运行宏。当我尝试运行宏时,出现错误消息runtime error 1004 application defined or object defined error

我检查了我的代码,该错误来自我的代码的这一部分:Range("K1") = "= SUM(" & thirdRow & "B:B" & LastRow & ")"

在我的代码中似乎无法正确识别此行的“ B:B”部分。实际上,我希望宏在单元格K1中将B列中第三个单元格的总和返回到B列中最后一个单元格的总和;在这种情况下,为587,29(请参见随附的屏幕截图,我用红色圆圈圈出)。 提前谢谢了。 Xavi enter image description here

Sub jfdjdgfjg()
  Dim i as Long, counter As Long
  Dim thirdcell As Range
  Dim r As Range
  Set r = ActiveCell
  Dim LastRow As Long
  Dim thirdRow As Long

  LastRow = ActiveSheet.Range("B" & Rows.Count).End(xlUp).Row
  Range("B1").Activate

  For i = 2 To LastRow 'assuming a header row not to be counted
    If r.Rows(i).EntireRow.Hidden = False Then counter = counter + 1
      If counter = 2 Then
        Set thirdcell = r.Cells(i, "A")
        Exit For
      End If

  Next i

  Debug.Print thirdcell
  Debug.Print LastRow
  thirdRow = thirdcell.Row
  Debug.Print thirdRow
  Range("K1") = "= SUM(" & thirdRow & "B:B" & LastRow & ")"
End Sub

0 个答案:

没有答案