I have a macro I use daily and want to add a few things to it. I had a ton of help with what I have now (code below) and tried to add the stuff myself, but it turned into a disaster. So back to the drawing board. Any help is greatly appreciated!
What I've been trying to do is add titles under/beside the formulas:
.HorizontalAlignment = xlCenter
.HorizontalAlignment = xlCenter
*New-7 & New-8 merge w/ K,L,N,O,P (M is not visible on the worksheet.)
Example:
[TABLE="width: 500"]
<tbody>[TR]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[TD]K[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]New-7[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]New-1[/TD]
[TD]New-2[/TD]
[TD]New-3[/TD]
[TD]New-4[/TD]
[TD]New-5[/TD]
[TD]New-6[/TD]
[TD]New-8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]New-7[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]New-1[/TD]
[TD]New-2[/TD]
[TD]New-3[/TD]
[TD]New-4[/TD]
[TD]New-5[/TD]
[TD]New-6[/TD]
[TD]New-8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
What I've been trying to do is add titles under/beside the formulas:
- New-1 =“Annual”
.Font.Bold = True - New-2 =“kWh”
- New-3 =“peak kW”
- New-4 =“TDSP”
- New-5 =“Avg. TDSP chrg”
- New-6 =“load factor”
- *New-7 =“(Power factor charges only >250kW & <95% PF)”
.Font.Italic = True
.HorizontalAlignment = xlCenter
- *New-8 =“(higher load factor = lower delivery charges)”
.Font.Italic = True
.HorizontalAlignment = xlCenter
*New-7 & New-8 merge w/ K,L,N,O,P (M is not visible on the worksheet.)
Example:
[TABLE="width: 500"]
<tbody>[TR]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[TD]K[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]New-7[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]New-1[/TD]
[TD]New-2[/TD]
[TD]New-3[/TD]
[TD]New-4[/TD]
[TD]New-5[/TD]
[TD]New-6[/TD]
[TD]New-8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD]##[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]New-7[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]New-1[/TD]
[TD]New-2[/TD]
[TD]New-3[/TD]
[TD]New-4[/TD]
[TD]New-5[/TD]
[TD]New-6[/TD]
[TD]New-8[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD]Formula[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Code:
Sub InsertTotals()
Dim lStartSample As Long, lEndSample As Long
Dim lSampleRows As Long, lEnd As Long
Const GAP = 3
Dim sFormula As String
sFormula = "="
lStartSample = 2
lEnd = Range("F" & Rows.Count).End(xlUp).Row
Do While lStartSample <= lEnd
lEndSample = Range("F" & lStartSample).End(xlDown).Row + 1
lSampleRows = lEndSample - lStartSample
Rows(lEndSample).Font.Bold = True
Range("F" & lEndSample).FormulaR1C1 = "=Sum(R[-" & lSampleRows & "]C:R[-1]C)"
Range("G" & lEndSample).FormulaR1C1 = "=Max(R[-" & lSampleRows & "]C:R[-1]C)"
With Range("H" & lEndSample)
.Style = "Currency"
.NumberFormat = "$#,###"
.FormulaR1C1 = "=sum(R[-" & lSampleRows & "]C:R[-1]C)"
End With
With Range("I" & lEndSample)
.Style = "Currency"
.NumberFormat = "$#,###.####"
.FormulaR1C1 = "=IF(RC[-3]=0,"""",RC[-1]/RC[-3])"
End With
With Range("J" & lEndSample)
.Style = "Percent"
.FormulaR1C1 = "=IF(RC[-3]=0,"""",RC[-4]/(RC[-3]*8760))"
End With
sFormula = sFormula & "R" & lEndSample & "C+"
lStartSample = lEndSample + GAP
With Range("F" & lEndSample).Resize(, 5)
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Borders(xlEdgeBottom).Weight = xlMedium
.Interior.Color = RGB(0, 255, 127)
End With
With Range("F" & lEndSample).Resize(, 5)
.HorizontalAlignment = xlCenter
End With
Loop
Rows(lEndSample + 2).Font.Bold = True
sFormula = Left(sFormula, Len(sFormula) - 1)
Range("H" & lEndSample + 2).Style = "Currency"
Range("H" & lEndSample + 2).NumberFormat = "$#,###"
Range("I" & lEndSample + 2).Style = "Currency"
Range("I" & lEndSample + 2).NumberFormat = "$#,###.####"
Range("J" & lEndSample + 2).Style = "Percent"
Range("F" & lEndSample + 2).FormulaR1C1 = sFormula
Range("G" & lEndSample + 2).FormulaR1C1 = sFormula
Range("H" & lEndSample + 2).FormulaR1C1 = sFormula
Range("I" & lEndSample + 2).FormulaR1C1 = "=IF(RC[-3]=0,"""",RC[-1]/RC[-3])"
Range("J" & lEndSample + 2).FormulaR1C1 = "=IF(RC[-3]=0,"""",RC[-4]/(RC[-3]*8760))"
With Range("F" & lEndSample + 2).Resize(, 5)
.Borders(xlEdgeLeft).Weight = xlMedium
.Borders(xlEdgeRight).Weight = xlMedium
.Borders(xlEdgeTop).Weight = xlMedium
.Bord
ers(xlEdgeBottom).Weight = xlMedium
.Borders(xlInsideVertical).Weight = xlMedium
.Interior.Color = RGB(0, 255, 127)
End With
With Range("F" & lEndSample + 2).Resize(, 5)
.HorizontalAlignment = xlCenter
End With
End Sub