Copying formula until the last cell with VBA Macro

mohammadimran

New Member
Joined
May 30, 2018
Messages
10
Dear experts,

I have been trying to run this macro however the formula copies only until row number 31 in column. Can you please guide me what is wrong with it?
I am trying to paste formula in column A and then copy it until the last cell. The macro runs just fine but formula only updates until A31. Please help

Sub Column_Adjustment()




Dim Lastrow As Long

Columns("A:C").Cut
Columns("M:M").Insert Shift:=xlToRight
Columns("A:A").Insert Shift:=xlToRight
Range("A1").Value = "Vendor Name"
Range("A2").Formula = "=IFERROR(INDEX('C:\Users\username\Documents\[Vendors List.xlsx]Sheet1'!$C:$C,MATCH(M2,'C:\Users\username\Documents\[Vendors List.xlsx]Sheet1'!$A:$A,0)),0)"

Lastrow = Range("A:A").End(xlUp).Row

Range("A2").Copy
Range(Cells(3 & Lastrow, 1), Cells(Lastrow, 1)).PasteSpecial (xlPasteFormulas)


Application.ScreenUpdating = True


End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try:
Code:
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
 
Upvote 0
what is the value of Lastrow in that case? Also, try changing this line:

Range(Cells(3 & Lastrow, 1), Cells(Lastrow, 1)).PasteSpecial (xlPasteFormulas)

to this:

Range(Cells(3 , 1), Cells(Lastrow, 1)).PasteSpecial (xlPasteFormulas)
 
Upvote 0
I have a total of 1601 rows.
changing the range cell 3 and lastrow to only 3, 1 has now limited the formula to copy only until row 3. Now its not even until row 32.
 
Upvote 0
What is the value for Lastrow you get using the line I posted in post #2 ? Your code inserts a new col A with nothing in it but the formula in A2. What column do you want to use to determine the last row? When you decide change the range in red font in the code below and see if it works for you.
Rich (BB code):
Sub Column_Adjustment()
Dim Lastrow As Long
Columns("A:C").Cut
Columns("M:M").Insert Shift:=xlToRight
Columns("A:A").Insert Shift:=xlToRight
Range("A1").Value = "Vendor Name"
Range("A2").Formula = "=IFERROR(INDEX('C:\Users\username\Documents\[Vendors List.xlsx]Sheet1'!$C:$C,MATCH(M2,'C:\Users\username\Documents\[Vendors List.xlsx]Sheet1'!$A:$A,0)),0)"
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Range("A2:A" & Lastrow).FillDown
Application.ScreenUpdating = True
End Sub
 
Upvote 0
I am not sure about the value of lastrow.
I am trying to cut the first 3 columns and insert them to column M
Now I have inserted column A and named it as Vendor Name
Now I have vendor numbers available in column M and the vendor names in another file against those number (therefore putting index-match formula in A2). Now I want to drag this formula down until the last cell value according to whichever cell is the last cell in column M (Vendor number).
 
Upvote 0
Then replace the red A in post #6 with an M and give the code a try.
 
Upvote 0
mohammadimran, what number does the code below give you?

Code:
Sub WhatRow()
MsgBox Columns("M:M").Find("*", , xlValues, , xlByRows, xlPrevious).Row
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,214
Messages
6,170,772
Members
452,353
Latest member
strainu

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