Tweak to VBA code to include last line

neilp

Well-known Member
Joined
Jul 5, 2004
Messages
533
Office Version
  1. 365
Platform
  1. 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?
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
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Wk 5 v3.xlsm
ABCDEFG
22EO25144BGYBIDENS GIANT YELLOW1442£ 26.04£ 52.08Yes
23R25BGYBIDENS GIANT YELLOW Royalty2£ 5.15£ 10.30
24EO25144BBEBIDENS BLAZING EMBERS1446£ 28.08£ 168.48Yes
25R25BBEBIDENS BLAZING EMBERS Royalty6£ 7.19£ 43.16
26EO25144BBFBIDENS BLAZING FIRE1448£ 28.08£ 224.64Yes
27R25BBFBIDENS BLAZING FIRE Royalty8£ 7.19£ 57.54
28EO25144BBSBIDENS BLAZING STAR1441£ 28.08£ 28.08Yes
29R25BBSBIDENS BLAZING STAR Royalty1£ 7.19£ 7.19
30EO25144BPPBIDENS PRETTY IN PINK1442£ 28.08£ 56.16Yes
31BIDENS PRETTY IN PINK Royalty2
32
Sheet1
Cell Formulas
RangeFormula
E22:E30E22=VLOOKUP(B22,EOCODES,3,FALSE)
F22:F30F22=E22*D22
A22:A30A22=VLOOKUP(B22,EOCODES,2,FALSE)
Named Ranges
NameRefers ToCells
codes='Product Sales Prices'!$C$2:$D$350A22:A30, E22:E30
EOCODES='Product Sales Prices'!$B$2:$D$500A22:A30, E22:E30
 
Upvote 0
Maybe try using column B instead of column G to find the last row of data, since column G does not always have an entry, i.e.
instead of:
VBA Code:
    lastRow = ws.Cells(ws.Rows.Count, "G").End(xlUp).Row
try:
VBA Code:
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
 
Upvote 0
Solution
Hi Joe.
thanks for your input
That still brings up the same issue - no foumulas dragged into the last line
thanks
Neil
 
Upvote 0
Please show me the following:
1. What your data looks like to start, before running anything.
2. What it currently looks like after the code runs.
3. What it SHOULD look like, after the code runs.
 
Upvote 0
Hi Joe,
your suggestion got me to relook at the code. I decided to try and get it to recalculate the last row after it had done all the inserting of rows.
I added
VBA Code:
lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
after the bulk of the code and it is working perfectly now.

thanks for the help

Neil
 
Upvote 0

Forum statistics

Threads
1,226,111
Messages
6,189,007
Members
453,520
Latest member
packrat68

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top