I've written the following code that adds a new line to a range of rows within a section in a spreadsheet. As there are several of these sections, sBudgetLine is passed in as the start value of the range (Section name) which is added with an extra 1 to miss the headers of the section (date, description and price).
All works fine, however when the row is added occasionally the formatting that I have hard coded doesn't work and adds an extra line further down the page as well as the new row which is in the right place.
Sub AddNewAllocToSpendLine(sBudgetLine As String, Optional sSheetName As String = c_Alloc2SpendSheetName)
Dim c As Range
Dim N As Long
Dim lastRow As Long
Dim formula As Range
Dim rng As Range
With Worksheets(sSheetName)
Set c = .Columns(1).Find(sBudgetLine, LookIn:=xlValues)
If Not (c Is Nothing) Then
lastRow = .Cells(c.Row, 2).End(xlDown).Row
Worksheets(sSheetName).Activate
Worksheets(sSheetName).Range("B" & lastRow + 1, "E" & lastRow + 1).Activate
Selection.EntireRow.Insert Shift:=xlDown
Selection.Range("B" & ActiveCell.Row, "E" & ActiveCell.Row).Interior.Color = RGB(255, 255, 255)
With Selection.Range("B" & ActiveCell.Row, "D" & ActiveCell.Row).Borders
.LineStyle = xlContinuous
.Weight = xlThin
.ColorIndex = 1
End With
Selection.Cells(1, 4).formula = "=IF(" & "D" & ActiveCell.Row & "="""","""",IF(" & "D" & ActiveCell.Row & ">14999.99,""Minimum 3 formal competitive tenders - inform PSU"",IF(" & "D" & ActiveCell.Row & ">2499.99,""Minimum 3 written quotes based on written specification"",IF(" & "D" & ActiveCell.Row & ">999.99,""Minimum 3 written quotes"",IF(" & "D" & ActiveCell.Row & ">499.99,""Minimum 3 oral quotes"",IF(" & "D" & ActiveCell.Row & ">249.99,""One written or verbal quote"",""One written or verbal quote""))))))"
End If
End With
End Sub
I have tried all sorts and can't figure whats wrong, could this be a vba glitch? If this is the case, is there a better way of providing the formatting with the added row?