有没有一种更快的方法来每小时汇总采摘的电脑

时间:2019-02-03 20:42:41

标签: excel vba

在此代码中,我想每小时获取所选择的pc的总和 所以我的excell工作表有很多行,我有一个代码,但是总和不够快,在s列中填充了longtimevalues dd:mm:yyy&hh:mm:s:在p列中填充了所选择的pc那个时候

aj2直到aj10是小时值5、6、7,依此类推aj10 = 13 ak2至ak10是该小时内挑选的pc

相同是 al2至al10是小时值14、15、16,以此类推al10 = 22 从am2到am10是那一小时内挑选的电脑

它还必须仅对具有值的可见单元格求和并检查小时数

我目前的代码是此解决方案vba或excell

我现在已经写了vba,但是就像我说的不够快,总结所有代码花了很长的路

Private Sub CheckBox6_Click()

If CheckBox6.Value = True Then

Dim lijnen As String
lijnen = "an15:an" & Range("s15").End(xlDown).Row

Application.ScreenUpdating = False

For Each cell In Range(lijnen).SpecialCells(xlCellTypeVisible)

If cell.Value <> "" Then

                    If Format(cell.Value, "hh") = Format(Range("aj2").Value, "hh") Then
                    Range("ak2").Value = Range("ak2").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("aj3").Value, "hh") Then
                    Range("ak3").Value = Range("ak3").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("aj4").Value, "hh") Then
                    Range("ak4").Value = Range("ak4").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("aj5").Value, "hh") Then
                    Range("ak5").Value = Range("ak5").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("aj6").Value, "hh") Then
                    Range("ak6").Value = Range("ak6").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("aj7").Value, "hh") Then
                    Range("ak7").Value = Range("ak7").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("aj8").Value, "hh") Then
                    Range("ak8").Value = Range("ak8").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("aj9").Value, "hh") Then
                    Range("ak9").Value = Range("ak9").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("aj10").Value, "hh") Then
                    Range("ak10").Value = Range("ak10").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("al2").Value, "hh") Then
                    Range("am2").Value = Range("am2").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("al3").Value, "hh") Then
                    Range("am3").Value = Range("am3").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("al4").Value, "hh") Then
                    Range("am4").Value = Range("am4").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("al5").Value, "hh") Then
                    Range("am5").Value = Range("am5").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("al6").Value, "hh") Then
                    Range("am6").Value = Range("am6").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("al7").Value, "hh") Then
                    Range("am7").Value = Range("am7").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("al8").Value, "hh") Then
                    Range("am8").Value = Range("am8").Value + Range("p" & cell.Row).Value
                    Else
                    If Format(cell.Value, "hh") = Format(Range("al9").Value, "hh") Then
                    Range("am9").Value = Range("am9").Value + Range("p" & cell.Row).Value


                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    End If
                    Next cell
              End If
              Application.ScreenUpdating = True
End Sub

3 个答案:

答案 0 :(得分:0)

关闭Excel Calculations通常可以提高性能。以下代码包括这些代码以及清理过的If语句。

If CheckBox6.Value = True Then

Dim lijnen As String
lijnen = "an15:an" & Range("s15").End(xlDown).Row

Dim calc As XlCalculation: calc = Application.Calculation 'captures your current setting

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

For Each cell In Range(lijnen).SpecialCells(xlCellTypeVisible)

    If cell.Value <> "" Then

                    If Format(cell.Value, "hh") = Format(Range("aj2").Value, "hh") Then
                    Range("ak2").Value = Range("ak2").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("aj3").Value, "hh") Then
                    Range("ak3").Value = Range("ak3").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("aj4").Value, "hh") Then
                    Range("ak4").Value = Range("ak4").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("aj5").Value, "hh") Then
                    Range("ak5").Value = Range("ak5").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("aj6").Value, "hh") Then
                    Range("ak6").Value = Range("ak6").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("aj7").Value, "hh") Then
                    Range("ak7").Value = Range("ak7").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("aj8").Value, "hh") Then
                    Range("ak8").Value = Range("ak8").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("aj9").Value, "hh") Then
                    Range("ak9").Value = Range("ak9").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("aj10").Value, "hh") Then
                    Range("ak10").Value = Range("ak10").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("al2").Value, "hh") Then
                    Range("am2").Value = Range("am2").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("al3").Value, "hh") Then
                    Range("am3").Value = Range("am3").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("al4").Value, "hh") Then
                    Range("am4").Value = Range("am4").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("al5").Value, "hh") Then
                    Range("am5").Value = Range("am5").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("al6").Value, "hh") Then
                    Range("am6").Value = Range("am6").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("al7").Value, "hh") Then
                    Range("am7").Value = Range("am7").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("al8").Value, "hh") Then
                    Range("am8").Value = Range("am8").Value + Range("p" & cell.Row).Value

                    ElseIf Format(cell.Value, "hh") = Format(Range("al9").Value, "hh") Then
                    Range("am9").Value = Range("am9").Value + Range("p" & cell.Row).Value

                    End If
        End If
Next cell
    Application.ScreenUpdating = True
    Application.Calculation = calc 'resets this back to whatever it previously was

End If

End Sub

答案 1 :(得分:0)

这可能有助于解决速度问题。我注意到您经常使用cell.value,这消除了这一点。它还可以清理您的代码。但是,它不会对单元格为空进行任何错误检查。

    Option Explicit

    Private Sub CheckBox6_Click()

        Dim strValue As String
        Dim lngRow As Long
        Dim lngPValue As Long
        Dim strPValue As String


        If CheckBox6.Value = True Then

            Dim lijnen As String
            lijnen = "an15:an" & Range("s15").End(xlDown).Row

            Application.ScreenUpdating = False

            For Each cell In Range(lijnen).SpecialCells(xlCellTypeVisible)
                strValue = Trim(cell.value)

                If strValue <> "" Then
                    strValue = Format(cell.Value, "hh")

                    lngRow = cell.Row
                    strPValue = Trim(Range("p" & lngRow).Value)
                    lngPValue = CLng(strPValue)

                    If strValue = Format(Range("aj2").Value, "hh") Then
                        Range("ak2").Value = Range("ak2").Value + lngPValue
                    ElseIf strValue = Format(Range("aj3").Value, "hh") Then
                        Range("ak3").Value = Range("ak3").Value + lngPValue
                    ElseIf strValue = Format(Range("aj4").Value, "hh") Then
                        Range("ak4").Value = Range("ak4").Value + lngPValue
                    ElseIf strValue = Format(Range("aj5").Value, "hh") Then
                        Range("ak5").Value = Range("ak5").Value + lngPValue
                    ElseIf strValue = Format(Range("aj6").Value, "hh") Then
                        Range("ak6").Value = Range("ak6").Value + lngPValue
                    ElseIf strValue = Format(Range("aj7").Value, "hh") Then
                        Range("ak7").Value = Range("ak7").Value + lngPValue
                    ElseIf strValue = Format(Range("aj8").Value, "hh") Then
                        Range("ak8").Value = Range("ak8").Value + lngPValue
                    ElseIf strValue = Format(Range("aj9").Value, "hh") Then
                        Range("ak9").Value = Range("ak9").Value + lngPValue
                    ElseIf strValue = Format(Range("aj10").Value, "hh") Then
                        Range("ak10").Value = Range("ak10").Value + lngPValue
                    ElseIf strValue = Format(Range("al2").Value, "hh") Then
                        Range("am2").Value = Range("am2").Value + lngPValue
                    ElseIf strValue = Format(Range("al3").Value, "hh") Then
                        Range("am3").Value = Range("am3").Value + lngPValue
                    ElseIf strValue = Format(Range("al4").Value, "hh") Then
                        Range("am4").Value = Range("am4").Value + lngPValue
                    ElseIf strValue = Format(Range("al5").Value, "hh") Then
                        Range("am5").Value = Range("am5").Value + lngPValue
                    ElseIf strValue = Format(Range("al6").Value, "hh") Then
                        Range("am6").Value = Range("am6").Value + lngPValue
                    ElseIf strValue = Format(Range("al7").Value, "hh") Then
                        Range("am7").Value = Range("am7").Value + lngPValue
                    ElseIf strValue = Format(Range("al8").Value, "hh") Then
                        Range("am8").Value = Range("am8").Value + lngPValue
                    ElseIf strValue = Format(Range("al9").Value, "hh") Then
                        Range("am9").Value = Range("am9").Value + lngPValue
                    End If
                End If
            Next cell
        End If
        Application.ScreenUpdating = True
    End Sub

答案 2 :(得分:0)

通常,您希望避免循环,但如果必须循环,则循环遍历数组。您的SpecialCells(xlCellTypeVisible)出现问题,因为该范围内可能存在不连续的区域,但是可以处理这些区域。

您已经编写了[disp32]个比较。我已将其更改为工作表的匹配比较。

Range.Value2(没有区域日期/时间或货币信息)比Range.Value快一点。数值收集和比较比字符串收集和比较要快。

这似乎是作为工作表的私有代码表中的私有子项编写的,因此无需明确定义父工作表引用。

DWORD PTR [rip+0x6000c0]