Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 和变量范围_Excel_Vba_Variables_Sum_Range - Fatal编程技术网

Excel 和变量范围

Excel 和变量范围,excel,vba,variables,sum,range,Excel,Vba,Variables,Sum,Range,如果满足某些条件,我需要一个代码来汇总变量行。 e、 g.如果A12为数字,B12为空,则在单元格C12中插入公式,求和C3:C11。 然后在C22和C30执行相同的操作。 我的问题是不知道如何定义起始行 Sub Test() Dim y As Variant Dim r As Variant Dim StartRow As Variant LastRow = Range("C" & Rows.Count).End(xlUp).Row For y = 3 To 5

如果满足某些条件,我需要一个代码来汇总变量行。 e、 g.如果A12为数字,B12为空,则在单元格C12中插入公式,求和C3:C11。 然后在C22和C30执行相同的操作。 我的问题是不知道如何定义起始行

Sub Test()
Dim y As Variant
Dim r As Variant
Dim StartRow As Variant

   LastRow = Range("C" & Rows.Count).End(xlUp).Row
        For y = 3 To 500
            For r = 1 To LastRow

            If InStr(1, Cells(r, 1), "Amount") Then
                StartRow = r

            If IsNumeric(Cells(y, 1)) And IsEmpty(Cells(y, 2)) Then
            Cells(y, 3).Formula = "=SUM(C" & StartRow + 1 & ":C" & y - 1 & ")"
            End If
         End If
      Next r
  Next y

End Sub

您可以只使用一个额外的列,而不需要VBA…@LS\u dev,因为这只是我整个代码的一部分,所以我希望VBA代码可以执行此操作,您能提供帮助吗?^^嗨,阿贝,好极了!它起作用了!但是我可以知道如何插入这个条件吗?如果IsNumericCellsy,1和IsEmptyCellsy,则2编辑答案以包含该值。如果不起作用,请告诉我。我没有测试它。嗨,阿贝,这很好用!但是…还是一个问题,C12中的公式变成SUMC1:C11,我想要的是C3:C11。我应该修改哪一部分?哈哈,我之前也把y=第一行改为3步骤-1,从1改为3,但不起作用,这就是为什么我要问你。还有一些其他的变化。用这个代码替换你的代码,看看它是否有效。
Sub Test()
Dim y As Variant
Dim firstRow As Variant
Dim lastRow As Variant
lastRow = Range("C" & Rows.Count).End(xlUp).Row
firstRow = Cells(lastRow, 3).End(xlUp).Row
If IsNumeric(Cells(lastRow + 1, 1)) And IsEmpty(Cells(lastRow + 1, 2)) Then
    Cells(lastRow + 1, 3).Formula = "=SUM(C" & firstRow & ":C" & lastRow & ")"
End If
For y = firstRow To 3 Step -1
    lastRow = Cells(y, 3).End(xlUp).Row
    firstRow = Cells(lastRow, 3).End(xlUp).Row
    If firstRow < 3 Then firstRow = 3
    If IsNumeric(Cells(lastRow + 1, 1)) And IsEmpty(Cells(lastRow + 1, 2)) Then
        Cells(lastRow + 1, 3).Formula = "=SUM(C" & firstRow & ":C" & lastRow & ")"
    End If
    y = firstRow
    If firstRow = 3 Then Exit Sub
  Next y
End Sub