neilp
Well-known Member
- Joined
- Jul 5, 2004
- Messages
- 533
- Office Version
- 365
- Platform
- Windows
What am I doing wrong.
I'm using the Macro below to : find the word Yes in column G, then insert a row beneath that row and fill in column B with the words in the line above (+ the word royalty) . it then copied the number that is in column D and finally copies the formulas, so that they fill in columns A, E and F.
This works great until it gets to the last line. although it copies the number in column D, it does not fill in the formulas and stops at the line above it.
any ideas?
I'm using the Macro below to : find the word Yes in column G, then insert a row beneath that row and fill in column B with the words in the line above (+ the word royalty) . it then copied the number that is in column D and finally copies the formulas, so that they fill in columns A, E and F.
This works great until it gets to the last line. although it copies the number in column D, it does not fill in the formulas and stops at the line above it.
any ideas?
VBA Code:
Sub InsertRowAndDragFormulas()
Dim ws As Worksheet
Dim lastRow As Long
Dim i As Long
Dim cellText As String
Dim startRow As Long
Set ws = ThisWorkbook.Sheets("Sheet1")
startRow = 13
lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
For i = lastRow To startRow Step -1
If ws.Cells(i, "G").Value = "Yes" Then
cellText = ws.Cells(i, "B").Value
ws.Rows(i + 1).Insert Shift:=xlDown
ws.Cells(i + 1, "D").Value = ws.Cells(i, "D").Value
ws.Cells(i + 1, "B").Value = cellText & " Royalty"
End If
Next i
Dim formulaColumns As Variant
formulaColumns = Array("A", "E", "F")
For i = LBound(formulaColumns) To UBound(formulaColumns)
Dim col As String
col = formulaColumns(i)
Dim lastFormulaRow As Long
lastFormulaRow = ws.Cells(ws.Rows.Count, col).End(xlUp).Row
ws.Range(ws.Cells(startRow, col), ws.Cells(lastFormulaRow, col)).FillDown
Next i
MsgBox "Macro completed successfully!", vbInformation
End Sub