在此代码,我想每个小时才挑选电脑的总和,所以我EXCELL表有很多行,我有alraedy一个代码,但它总结不够快,在列S充满longtimevalues DD:MM:YYY和HH:MM :在p列中的SS填充有被对时间挑选的颗
AJ2直到AJ10是小时值5,6,7等AJ10 = 13个AK2直到AK10是PCS在于小时拾取
同样是AL2直到AL10是小时值14,15,16等[10] = 22 AM2直到AM10是PCS在于小时拾取
也有总结和检查小时的PC而可见的细胞,在它的值
就目前我的代码是该解决方案的VBA或Excel
我已经写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
关闭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
这可能有助于与速度问题。我注意到,您使用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
通常你想避免循环,但遍历数组,如果你要循环。你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