VBA for LastRow to copy formula down fails

Tarver

Board Regular
Joined
Nov 15, 2012
Messages
109
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
I'm trying to insert a formula and copy it down to the last row of the data it's working on.

Code:
Sub InsertAndCopyFormula()
    Sheets("PasteData").Select
    Range("L2:L" & LastRow).Formula = "=MONTH(I2)"
End Sub

When I execute this code, I get the error "Run-time error '1004': Method 'Range' of object '_Global' failed.

Is there something wrong with my syntax here, or something else I'm missing?

Thanks in advance for your help!
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
You haven't assigned a value to LastRow so you're trying to refer to L0 which doesn't exist.
 
Upvote 0
So if I assign a value to LastRow like this, it still gives me an error:

Code:
Sub InsertAndCopyFormula()
    Dim LastRow As Long
    Sheets("PasteData").Select
    LastRow = ActiveSheet.UsedRange.Rows.Count
    Range("L2:L" & LastRow).Formula = "=MONTH(I2)"
End Sub

I've also seen online that perhaps the LastRow should be set a different way:
Code:
LastRow = Range("A" & Rows.Count).End(xlUp).Row

Both of these methods fail.
 
Upvote 0
To find the last row. This finds the last row in column A use a column you should use a column that will always have data
Code:
LastRow = Cells(Rows.Count, "A").End(xlUp).Row
 
Upvote 0
You're code works for me, what error do you get & what line is higlighted
 
Upvote 0

Forum statistics

Threads
1,214,415
Messages
6,119,377
Members
448,888
Latest member
Arle8907

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