+ Reply to Thread
Results 1 to 7 of 7

Insert or copy formulas using VBA

  1. #1
    Registered User
    Join Date
    05-14-2014
    MS-Off Ver
    Excel 2013
    Posts
    14

    Insert or copy formulas using VBA

    Hello everyone,
    I am a novice at VBA and macros, but I've gone far using this great forum and a lot of trial and error until now.
    I'm currently stumped on how to insert a formula using a VBA routine. I have a userform that collects information and a SaveButtonClick() that transfers and the userform data to several cells in column's 1,5 thru 16 in a "data" sheet. However, cells in columns 2,3 & 4 in the data sheet required a vlookup formula and I hoped that adding the formula to the "transfer" syntax in the routine would work. I'm not sure if this method is the best, or if a copy/paste syntax would be more appropriate to insert formulas. Also, the vlookup's lookup_value has to come from the formula's active row (the row it was just copied to), specifically, column 1 or A.

    Warm wishes and many thanks on any help you can give!

    ____________________
    Please Login or Register  to view this content.
    Last edited by Leith Ross; 05-16-2014 at 08:18 PM. Reason: Added Code Tags

  2. #2
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Insert or copy formulas using VBA

    Hi izzorac,

    Welcome to the forum!!

    Try this - just note it's obviously untested without your workbook:

    Please Login or Register  to view this content.
    Regards,

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  3. #3
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Insert or copy formulas using VBA

    I think Trebor may have one too many right parentheses in the formula ')'.

    Try:
    Please Login or Register  to view this content.
    In addition to make the formula look cleaner you may want to declare your lookup table as a 'Named Range'. This can be done manually (Excel 2003):
    Insert > Name > Define or in newer versions of Excel see http://www.dummies.com/how-to/conten...xcel-2007.html

    It can also be done in VBA such as (the named range is any name of your choice):
    Please Login or Register  to view this content.
    Lewis

  4. #4
    Registered User
    Join Date
    05-14-2014
    MS-Off Ver
    Excel 2013
    Posts
    14

    Re: Insert or copy formulas using VBA

    Hi Robert and Trevor,
    Thanks for the code fix! It works perfectly with only one parentheses in the formula. I have also used the range declarations you suggested and it certainly cleans up the code. However, after running it didn't return the correct VLOOKUP result and I realized that VLOOKUP needs to have the 1st column in target range sorted a to Z, or numerically. I will have to add a sort function for the "Master Store List" Range that runs prior to the Vlookup... unless one of you know of another VBA call function that looks for a specific value without having the target range sorted in any particular manner?
    Thanks again!
    Cesar

  5. #5
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Insert or copy formulas using VBA

    Try
    Please Login or Register  to view this content.
    4th parameter as 'False', indicates you are looking for an EXACT MATCH and the range DOES NOT have to be sorted. See http://office.microsoft.com/en-us/ex...005209335.aspx

    Lewis

  6. #6
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: Insert or copy formulas using VBA

    Yes, Lewis is correct you don't need your data to be sorted for VLOOKUP to work. VLOOKUP will only return the first match it finds in a list where's there's duplicates though, is this the issue?

  7. #7
    Registered User
    Join Date
    05-14-2014
    MS-Off Ver
    Excel 2013
    Posts
    14

    Re: Insert or copy formulas using VBA

    Lewis and Trebor,
    I really appreciate the help on the coding. I've made your suggested changes and they work great! Funny how I've used Vlookup for years as a worksheet function and never entertained the "false" argument-- see how old dogs can learn new tricks.
    Take care!
    Cesar

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Insert rows and copy formulas
    By amlal in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-30-2012, 02:53 PM
  2. Insert a new row and copy all formulas down
    By treva26 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-17-2007, 06:12 PM
  3. insert row, copy formulas, have code example (I think).
    By gobbolino in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-14-2007, 12:10 AM
  4. [SOLVED] Macro to insert copy and insert formulas only to next blank row
    By bob in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-30-2006, 07:10 AM
  5. Copy/Insert rows with formulas
    By GregR in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-26-2005, 06:06 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1