使用VBA选择范围而不是特定的单元格值

时间:2019-09-25 05:03:21

标签: excel vba

我在VBA中的项目很少,并且停留在以下主题上

enter image description here

我需要对B列中第一个空单元格中的选定范围求和。我尝试了一个小宏,在其中将vba中提到的同一行相加。

这是我发现并尝试使用的

{
  "id" : "BNK056003413",
  "name" : "prof_name",
  "metadataSet" : {
    "description" : "Template for working with PRI",
    "type" : "Live"
  },
  "workflow" : {
    "workflowInputProperties" : {
      "assetNamePrefix" : "bank_",
      "recordId" : "",
      "sourceUri":"",
      "processingUri": "",
      "recorderType": "ABC",
      "completionTimeout": 600
      "loopBackTimer": 10,
      "numberOfRetries": 3,
      "numberOfRetriesForScheduling": 3,
      "scheduleDelay" : 3600
    },
  }
}

我尝试搜索选定范围内的最后一个非空单元格,因此它不会搜索整列

2 个答案:

答案 0 :(得分:0)

要获取图像中我所能理解的,要获取列“ B”中所有非空单元格的总和并将其返回到列“ B2”,以下代码将为您提供帮助:

Range("B2").FormulaR1C1 = "=Sum(R3C:R" & ActiveSheet.UsedRange.Rows.Count + 1 & "C)"

您可以使用以下代码获取直到第一个空单元格的和:

Range("B2").FormulaR1C1 = "=Sum(R3C:R" & Range("B3").End(xlDown).Row & "C)"

答案 1 :(得分:0)

使用Range对象的SpecialCells方法获取给定范围内的非空单元格

然后使用Range对象的Areas属性遍历每组连续的非空单元格

Option Explicit

Sub SumThem()
    Dim c As Range

    For Each c In Intersect(ActiveSheet.UsedRange, Columns(2)).Offset(1).SpecialCells(xlCellTypeConstants).Areas ' loop through each "area" (i.e.: group of consecutive cells with "constant" values) of column B from row 2 downwards
        c.Resize(1).Offset(-1, 0).Formula = "=+SUM(" & c.Address & ")" ' place the sum of current "area" in the cell right above it
    Next
End Sub