Syntax Error - Insert Column Header (Good), Insert Formula in Row 2 (Good), Drag to Last Row (ERROR)

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
Hello, any ideas how I can get the following code to work? I am adding a column called "Value less VAT", then adding in the formula in row 2, then attempting to drag it down to the last row. Everything works like a charm, except the dragging down portion. See details below.

Code #1 = my code that is resulting in a syntax error
Code #2 = code that works properly, EXCEPT I need the row # to be dynamic (i.e. instead of being P2:AF2 in all rows, I need it to be P2:AF2 in row 2, then P3:AF3 in row 3, etc...)

Code:
Sub Loop_Example()
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim LastColumn As Integer
    Dim LastRow As Integer
    
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    'Select "Inventory Transaction Summary" worksheet
    With Sheets("Inventory Transaction Summary -")
        'Improves speed of macro
        .Select
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        .DisplayPageBreaks = False
        
    'Insert "Value less VAT" column heading after last column
    LastColumn = ActiveSheet.UsedRange.Columns.Count
    LastRow = ActiveSheet.UsedRange.Rows.Count
    ActiveSheet.Cells(1, LastColumn + 1) = "Value Less VAT"
    
    For n = LastRow To 2 Step -1
    ActiveSheet.Cells(n, LastColumn + 1) = "="P" & n * "AF" & n"
    
    Next n
    
    End With
    
    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With
End Sub




Code #2:

Code:
Sub Loop_Example()
    Dim CalcMode As Long
    Dim ViewMode As Long
    Dim LastColumn As Integer
    Dim LastRow As Integer
    
    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
    End With
    'Select "Inventory Transaction Summary" worksheet
    With Sheets("Inventory Transaction Summary -")
        'Improves speed of macro
        .Select
        ViewMode = ActiveWindow.View
        ActiveWindow.View = xlNormalView
        .DisplayPageBreaks = False
        
    'Insert "Value less VAT" column heading after last column
    LastColumn = ActiveSheet.UsedRange.Columns.Count
    LastRow = ActiveSheet.UsedRange.Rows.Count
    ActiveSheet.Cells(1, LastColumn + 1) = "Value Less VAT"
    
    For n = LastRow To 2 Step -1
    ActiveSheet.Cells(n, LastColumn + 1) = "=P2*AF2"
    
    Next n
    
    End With
    
    ActiveWindow.View = ViewMode
    With Application
        .ScreenUpdating = True
        .Calculation = CalcMode
    End With
End Sub
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
8,388
Office Version
  1. 365
Platform
  1. Windows
Replace that loop with:

Code:
Range(Cells(2, Lastcolumn + 1), Cells(LastRow, Lastcolumn + 1)).FormulaR1C1 = "=RC16*RC32"
 

Watch MrExcel Video

Forum statistics

Threads
1,129,272
Messages
5,635,217
Members
416,847
Latest member
inaramos

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
Top