如何在固定行和变化列上找到所有'0',然后将它们相加?

时间:2012-03-04 09:39:04

标签: vba excel-vba excel

我的代码需要帮助,我不确定为什么它运行不正常需要很长时间。我想要做的是找到重复的温度,例如,0。找到0后,我将继续在临时行寻找更多的0,如果有的话我会将B3的test1和H3的test1相加一起...它将一直持续到行的结尾,并将粘贴在N列或O列,这是一个空列。在那之后,整体而言,必须做同样的事情。

enter image description here

结果应该是这样的

enter image description here

我无法运行以下尝试编写的代码。

Dim temprow As Long, ColMax1 As Long, tempcell As Range, ColCount1 As Long
Dim temprow1 As Long, valuetohighlight As Variant, valuetohighlight1 As Variant 
Dim totalvalue As Double, findvalues As Long

temprow = 1
ColMax1 = 10

Do

   Set tempcell = Sheets("Sheet1").Cells(temprow, 1)

    'Look for the word temp in column A

    If tempcell = "temp" Then
     'Look for values = 0
     For ColCount1 = 2 To ColMax1

        findvalues = Sheets("Sheet1").Cells(temprow, ColCount1)

        If findvalues = 0 Then

            temprow1 = temprow + 1

            valuetohighlight = Sheets("Sheet1").Cells(temprow1, ColCount1)

        End If
        Next

     'Look for other values that is equal to 0
    For ColCount1 = 3 To ColMax1

        findvalues = Sheets("Sheet1").Cells(temprow, ColCount1)

        If findvalues = 0 Then

            temprow1 = temprow + 1

            valuetohighlight1 = Sheets("Sheet1").Cells(temprow1, ColCount1)

        End If
        Next

        temprow = temprow + 1

End If

Loop

For ColCount1 = 1 To ColMax1

    If Sheets("Sheet1").Cells(temprow, ColCount1) = "" Then


       totalvalue = 0

       totalvalue = valuetohighlight + valuetohighlight1

     End If

     Next

End Sub

如果您有任何想法或意见,请与我分享..感谢您的帮助!

轻微修改

enter image description here

现在还需要考虑名称。

1 个答案:

答案 0 :(得分:2)

您想要实现的目标可以通过公式完成。诀窍是将第2行的Col O中的Cell Headers保持为要比较的实际值。

单元格O3中的公式

=SUMPRODUCT(($B$2:$M$2=$O$2)*B3:M3)

<强>快照

enter image description here

关注

  
    
      

嗨,我记得你使用那个公式并在之前为我输入VBA,我已经尝试过并且它有效.. Sheets(“Sheet1”)。[O5] =评估(“SUMPRODUCT((B2:M2 = O2)*(B5:M5))“)但是,我真的没有一个固定的列用于打印结果,并且温度可能不会落在第2行......

    
  

以下是示例代码。将15更改为要显示结果的相关列。我已经对代码进行了评论,因此在理解代码时不应该有任何问题。如果您仍然这样做,那么只需询问:)

<强> CODE

Option Explicit

Sub Sample()
    Dim ColNo As Long, tempRow As Long
    Dim ws As Worksheet
    Dim aCell As Range

    '~~> Change this to the column number where you want to display the result
    '~~> The code assumes that Row 2 in this column has headers
    '~~> for which you want to retrieve values
    ColNo = 18 '<~~ Example :- Column R

    '~~> Change this to relevant sheet name
    Set ws = Sheets("Sheet1")

    '~~> Get the row number which has "Temp"
    Set aCell = ws.Columns(1).Find(What:="Temp", LookIn:=xlValues, _
    LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)

    If Not aCell Is Nothing Then
        '~~> This is the row which has 'Temp'
        tempRow = aCell.Row

        '~~> Sample for putting the value in Row 3 (assuming that 'temp' is not in row 3)
        '~~> SNAPSHOT 1
        ws.Cells(3, ColNo).Value = Evaluate("=SUMPRODUCT(($B$" & tempRow & ":$M$" & tempRow & "=" & _
        ws.Cells(2, ColNo).Address & ")*(B3:M3))")

        '~~> If you want to use formula in the cell in lieu of values then uncomment the below

        '~~> SNAPSHOT 2
        'ws.Cells(3, ColNo).Formula = "=SUMPRODUCT(($B$" & tempRow & ":$M$" & tempRow & "=" & _
        ws.Cells(2, ColNo).Address & ")*(B3:M3))"

    Else
        MsgBox "Temp Not Found. Exiting sub"
    End If
End Sub

SNAPSHOT(如果您在上面的代码中使用评估)

enter image description here

SNAPSHOT(如果您在上面的代码中使用.FORMULA)

enter image description here

HTH

西特