我有24列,其中包含美元金额(H:AE)。我将它们分为3类,AG的ATC(24小时),AH的On Peak(8-23)和AI的Off Peak(1-7,24)。在列AK,AL和AM中,我对列E中的PNODENAME的所有实例求和了ATC,On Peak和Off Peak。在列AT中,选择了正确的价格带(来自列AP),但是,如果A列中的日期是周末,然后返回ATC总和。
我被困在下一部分,在AV栏中有一段时间与atc(全天候),峰值或非峰值相加的时间段相关,基于日期。例如,DAY需要DEC月份的所有非高峰价格(以及周末的ATC)。为了保持有趣,一些节点需要按季度求和,如6月到8月。当然要让它变得有趣,这需要循环。考虑将总和放在行AX中。
我想知道我是否可以宣布一段时间?欢迎任何建议。这就是我到目前为止......
Sub LMPTest()
'Looping Sum for ATC by row
For i = 2 To Cells(Rows.Count, "H").End(xlUp).Row
Range("AG" & i) = WorksheetFunction.Sum(Range("H" & i & ":AE" & i))
Next i
'Looping Sum for ONPEAK by row
For i = 2 To Cells(Rows.Count, "H").End(xlUp).Row
Range("AH" & i) = WorksheetFunction.Sum(Range("O" & i & ":AD" & i))
Next i
'Looping Sum for OFFPEAK by row
For i = 2 To Cells(Rows.Count, "H").End(xlUp).Row
Range("AI" & i) = Val(Range("AG" & i).Value) - Val(Range("AH" & i).Value)
Next i
'Looping ATC Sumif Node Matches #### Needs to be from PJM Period
For i = 2 To Cells(Rows.Count, "AG").End(xlUp).Row
Worksheets("pjm_lmp_table").Range("AK" & i).Formula = "=SUMIF(pjm_lmp_table!E:E, pjm_lmp_table!E:E, pjm_lmp_table!AG:AG)"
Next i
'Looping ONPEAK Sumif Node Matches #### Needs to be from PJM Period
For i = 2 To Cells(Rows.Count, "AH").End(xlUp).Row
Worksheets("pjm_lmp_table").Range("AL" & i).Formula ="=SUMIF(pjm_lmp_table!E:E, pjm_lmp_table!E:E, pjm_lmp_table!AH:AH)"
Next i
'Looping OFFPEAK Sumif Node Matches #### Needs to be from PJM Period
For i = 2 To Cells(Rows.Count, "AI").End(xlUp).Row
Worksheets("pjm_lmp_table").Range("AM" & i).Formula = "=SUMIF(pjm_lmp_table!E:E, pjm_lmp_table!E:E, pjm_lmp_table!AI:AI)"
Next i
'Looping Sumif Day of Week
For i = 2 To Cells(Rows.Count, "A").End(xlUp).Row
If Weekday(Range("A" & i).Value) > 6 Or Weekday(Range("A" & i).Value) < 2 Then
Range("AT" & i).Value = Range("AK" & i).Value
ElseIf Range("AP" & i) = "ONPEAK" Then
Range("AT" & i).Value = Range("AL" & i)
ElseIf Range("AP" & i) = "OFFPEAK" Then
Range("AT" & i).Value2 = Range("AM" & i)
End If
Next
End Sub
答案 0 :(得分:0)
虽然这不是一个完整的答案,但我想指出你正确的方向,通过删除循环加快过程,并且只是重要,这样你就可以自己做更多的调整。
Sub LMPTest()
Dim lr As Long
With Worksheets("pjm_lmp_table") '<~~ always specify the worksheet!
lr = Cells(Rows.Count, "H").End(xlUp).Row
'Looping Sum for ATC by row
.Range("AG2:AG" & lr).Formula = "=SUM(H2:AE2)"
.Range("AH2:AH" & lr).Formula = "=SUM(O2:AD2)"
'Looping Sum for OFFPEAK by row
.Range("AI2:AI" & lr) = "=AG2-AH2"
'Looping ATC Sumif Node Matches #### Needs to be from PJM Period
.Range("AK2:AK" & lr).Formula = "=SUMIF(E:E, E2, AG:AG)"
'revert formula to result values
.Range("AG2:AK" & lr) = .Range("AG2:AL" & lr).Value2
'Looping OFFPEAK Sumif Node Matches #### Needs to be from PJM Period
.Range("AM2:AM" & lr).Formula = "=SUMIFS(AI:AI, E:E, E2, AP:AP, AM$1)"
'revert formula to result values
.Range("AM2:AM" & lr) = .Range("AM2:AM" & lr).Value2
'Looping Sumif Day of Week
.Range("AT2:AT" & lr).Formula = "=IF(OR(WEEKDAY(A2)={7, 1}), AK2, " & _
"IF(AP2=""ONPEAK"", AL2, " & _
"IF(AP2=""OFFPEAK"", AM2, """")"
'revert formula to result values
.Range("AT2:AT" & lr) = .Range("AT2:AT" & lr).Value2
End With
End Sub
插入到整个范围列中的所有公式都是您要在该列的第二行中键入的公式。这与将公式放入第二行然后填充相同。删除公式后保留其结果值。 Range.Value2 property因为这些值的性质是严格的数字而.Value2是最新的值属性。
tl; dr:简而言之,如果您可以为第二行设计工作公式,那么此方法将有利于您的开发。