Copy formula using VB code

The formula is there on the H column in the spreadheet.I am pasting data into the spreadsheet from another sheet using macro.We are pasting data from A to G columns.The formula on H will do a vlookup based on value in A column.
Is there a way to automate the formula to all the cells

For example if i pasted data on  A to G in 20 rows .I need the formula on H to be from H1 to H20 automatically.

Please let me know.

Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Is there already a formula in H1? If so then

    Range("H2:H" & Range("G1048576").End(xlUp).Row).Formula = Range("H1").Formula


    Range("H1:H" & Range("G1048576").End(xlUp).Row).Formula = "=A1"

where you would change the formula to suit.
Chaitu235Author Commented:
Hi Can you please tell me where i should write this code..on worksheet_activate?
Chaitu235Author Commented:
I think you got my question wrong...

The code i am looking for is ..

I have formula on H3 column...The data on A through G columns will be results from a query .If query retured 20 rows ..

Data will be there from A3 through  G on 20 rows.   I want the formula on H3 to be copied to next 20 cells on H column.Please help me on that
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Martin LissOlder than dirtCommented:
Can you attach a workbook that has the query results in it?
Chaitu235Author Commented:
Martin please find the Spreadsheet attached.Please let me know if you have any questions.
Martin LissOlder than dirtCommented:
To do it manually
1.    Select cell G1
2.    Grab the little black dot in the lower right-hand corner and drag it down to the last cell
3.    Release the mouse

To do it via VBA use this macro
Sub CopyFormula()
Dim lngLastRow As Long
Dim lngRow As Long

With ActiveSheet
    lngLastRow = .Range("A1048576").End(xlUp).Row
    .Range("G1").AutoFill Destination:=.Range("G1:G" & lngLastRow), Type:=xlFillDefault
End With
End Sub

Open in new window

Just in case you need them, here are instructions on how to install and use the macro.

1.    In Excel, Press Alt+F11 to open the Visual Basic Editor (VBE)
2.    Right-click on your workbook name in the "Project-VBAProject" pane (at the top left corner of the editor window). If you don’t see an existing module then select Insert -> Module from the context menu. Otherwise just select the module.
3.    Copy the macro (you can use the ‘Select All’ button if you like) and paste it into the right-hand pane of the VBA editor ("Module1" window)
4.    Press Alt+F11 again to go back to Excel
5.    Optionally, press Alt+F8 to open the "Macro" dialog window. Select the macro, click ‘Options…’,  hold down the Shift key and type the letter A (or any other letter) and click ‘OK’. Then anytime you want to run the macro press Ctrl+Shift+A.
When you close the workbook you will need to save it as an xlsm or xls file if it’s not already one of those.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Martin LissOlder than dirtCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.