When I step through the code below, the results on the worksheet are as they should be (the total label is in J whatever and the total is in K whatever) but when I run through the macro without any code breaks, the worksheet header rows (in this case range A1:K7) are shifted over to range C1:M7, the total label that should be in J whatever is in L9, and the total that the macro should be placing at the end of K whatever is in M9. What is missing from the code to prevent this?
<code>
Private Sub TotalDist(sWS As String)
' Put a total in the last row of the worksheet
Dim lStart As Long
Sheets(sWS).Select
With Worksheets(sWS)
lStart = GetLastRow(sWS)
.Range("K8:E" & lStart).Select
lStart = lStart + 1
.Range("K" & lStart).Activate
ActiveCell.Formula = "=SUM(K8:K" & lStart - 1 & ")"
Range("K" & lStart).Select
Selection.Style = "Comma"
Selection.Font.Bold = True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("J" & lStart).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Font.Bold = True
ActiveCell.FormulaR1C1 = "Total"
Range("J" & lStart).Select
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
' Reset Focus
.Range("A8").Select
End With
End Sub
</code>
Thanks in advance for any help.
<code>
Private Sub TotalDist(sWS As String)
' Put a total in the last row of the worksheet
Dim lStart As Long
Sheets(sWS).Select
With Worksheets(sWS)
lStart = GetLastRow(sWS)
.Range("K8:E" & lStart).Select
lStart = lStart + 1
.Range("K" & lStart).Activate
ActiveCell.Formula = "=SUM(K8:K" & lStart - 1 & ")"
Range("K" & lStart).Select
Selection.Style = "Comma"
Selection.Font.Bold = True
Selection.Borders(xlDiagonalDown).LineStyle = xlNone
Selection.Borders(xlDiagonalUp).LineStyle = xlNone
Selection.Borders(xlEdgeLeft).LineStyle = xlNone
With Selection.Borders(xlEdgeTop)
.LineStyle = xlContinuous
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThin
End With
With Selection.Borders(xlEdgeBottom)
.LineStyle = xlDouble
.ColorIndex = 0
.TintAndShade = 0
.Weight = xlThick
End With
Selection.Borders(xlEdgeRight).LineStyle = xlNone
Selection.Borders(xlInsideVertical).LineStyle = xlNone
Selection.Borders(xlInsideHorizontal).LineStyle = xlNone
Range("J" & lStart).Select
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Font.Bold = True
ActiveCell.FormulaR1C1 = "Total"
Range("J" & lStart).Select
With Selection
.HorizontalAlignment = xlRight
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
' Reset Focus
.Range("A8").Select
End With
End Sub
</code>
Thanks in advance for any help.