Есть ли более быстрый способ суммировать выбранные шт в час - PullRequest
0 голосов
/ 03 февраля 2019

в этом коде я хочу получить в час сумму выбранных штук, поэтому на моем листе исключений много строк, у меня есть код, но он суммируется недостаточно быстро, в столбце s задано длинное значение dd: mm: yyy &чч: мм: сс в столбце p заполняется набранными в это время единицами

aj2 до aj10 имеют часовое значение 5, 6, 7 и т. д. aj10 = 13 ak2 до ak10 - это числа, выбранные вв тот час

то же самое от al2 до al10 - это часовое значение 14, 15, 16 и т. д. al10 = 22 am2 до am10 - это единицы, отобранные в этот час

, также необходимо суммировать и проверитьчас шт. только видимые ячейки, в которых есть значения

код на данный момент у меня есть это решение VBA или отлично

у меня на данный момент написано 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 голосов
/ 04 февраля 2019

Это может помочь с проблемой скорости.Я заметил, что вы часто используете 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

0 голосов
/ 04 февраля 2019

Обычно вы хотите избежать зацикливания, но зацикливаться на массивах, если вам приходится зацикливаться.Ваши SpecialCells (xlCellTypeVisible) представляют проблему, поскольку в пределах диапазона могут быть несмежные области, но с ними можно справиться.

Вы написали If ElseIf ElseIf ElseIf ... End If сравнения.Я изменил это на Сравнение соответствия на рабочем листе.

Range.Value2 (без региональной даты / времени или информации о валюте) немного быстрее, чем Range.Value.Числовой сбор и сравнение выполняется быстрее, чем сбор и сравнение строк.

Похоже, что это записано как частное подпрограмма в закрытом кодовом листе рабочего листа, поэтому явное определение ссылки на родительский рабочий лист не требуется.

Option Explicit

Private Sub CheckBox6_Click()

    If CheckBox6.Value Then 'CheckBox6 is either True of False; you don't have to compare it to True

        Dim i As Long, a As Long, lr As Long, rngP As Range, rngAN As Range
        Dim arr1 As Variant, arr2 As Variant, m As Variant

        'Application.ScreenUpdating = False

        'build the hours tables
        ReDim hrs(1 To 18) As Variant
        ReDim pAK(1 To 9) As Variant
        ReDim pAm(1 To 9) As Variant
        For i = 2 To 10
            hrs(i - 1) = Hour(Cells(i, "AJ").Value2)
            hrs(i + 8) = Hour(Cells(i, "AL").Value2)
        Next i

        'collect the filtered values from columns P and AN
        lr = Cells(15, "AN").End(xlDown).Row
        Set rngP = Range(Cells(15, "P"), Cells(lr, "P")).SpecialCells(xlCellTypeVisible)
        Set rngAN = Range(Cells(15, "AN"), Cells(lr, "AN")).SpecialCells(xlCellTypeVisible)

        'loop through the areas of SpecialCells(xlCellTypeVisible)
        For a = 1 To rngAN.Areas.Count
            'collect the Area's values
            arr1 = rngAN.Areas(a).Cells.Value2
            arr2 = rngP.Areas(a).Cells.Value2

            'loop through the array
            For i = LBound(arr1, 1) To UBound(arr1, 1)

                'determine if Hour is in AJ2:AJ10 or AL2AL10
                m = Application.Match(Hour(arr1(i, 1)), hrs, 0)
                If Not IsError(m) Then
                    If m < 10 Then
                        pAK(m) = pAK(m) + arr2(i, 1)
                    Else
                        pAm(m - 9) = pAm(m - 9) + arr2(i, 1)
                    End If
                End If

            Next i

        Next a

        'dump processed values back to worksheet
        Cells(2, "AK").Resize(UBound(pAK), 1) = Application.Transpose(pAK)
        Cells(2, "AM").Resize(UBound(pAm), 1) = Application.Transpose(pAm)

        Application.ScreenUpdating = True

    End If

End Sub
0 голосов
/ 04 февраля 2019

Отключение вычислений Excel обычно повышает производительность.Следующий код включает это вместе с очищенными 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...