I use the below setup to fill formulas on an excel data worksheet. The first 5 rows are full of buttons and other reference cells, and row 6 is the header row for the data. Occasionally, and without a clear pattern, this macro will miscount the rows and start putting formulas in row 6 instead of row 7, which overwrites the header row.
Any ideas on how to fix or prevent this?
Any ideas on how to fix or prevent this?
Code:
Sub fillFormula3()
Dim Lr As Long
Dim ws1 As Worksheet: Set ws1 = ThisWorkbook.Sheets("Main Data")
Lr = ws1.Range("A" & Rows.Count).End(xlUp).Row
ws1.Range("E7:E" & Lr).Formula = "=IF(D7="""",D$4,D7)"
ws1.Range("F7:F" & Lr).Formula = "=IF(G7="""",G$4,G7)"
ws1.Range("M7:M" & Lr).Formula = "=SUM(I7:L7)"
ws1.Range("N7:N" & Lr).Formula = "=IF(M7="""","""",(H7-M7))"
ws1.Range("A7:B" & Lr).Interior.Color = RGB(248, 203, 176) 'light pink
ws1.Range("M7:N" & Lr).Interior.Color = RGB(248, 203, 176) 'light pink
With ws1.Range("A7:N" & Lr).Borders.LineStyle = xlContinuous
.ColorIndex = 11
End With
With ws1
With Application.ErrorCheckingOptions
.BackgroundChecking = False
.EvaluateToError = False
.InconsistentFormula = False
End With
End With
End Sub