+ Reply to Thread
Results 1 to 3 of 3

VBA to insert formulas only in selected cells

  1. #1
    Registered User
    Join Date
    07-29-2021
    Location
    Australia
    MS-Off Ver
    Office 365
    Posts
    2

    Question VBA to insert formulas only in selected cells

    Hi all,

    I was just seeking some advice on how to write a macro that will insert a formula only if a condition is met.

    The goal here is I have a column of dates and want to run an xlookup formula to pull in data only if the date is within the past 30 days.

    I want to macro to look for example at the date in A1, and if that date is less than 30 days before todays date, then run an xlookup and write the value to B1.
    I am hoping to set this up to look through up to 1000cells (A1 to A1001). Below is the code I attempted to write but does not work seem to work at all as I am quite new to this.

    -----
    Please Login or Register  to view this content.
    ----


    Any suggestions on how to do this would be greatly appreciated.
    Last edited by davesexcel; 07-29-2021 at 09:59 AM.

  2. #2
    Forum Contributor
    Join Date
    11-05-2019
    Location
    Tiki Island, TX
    MS-Off Ver
    Office 365
    Posts
    168

    Re: VBA to insert formulas only in selected cells

    I'm not sure why you are writing a macro to accomplish this goal. This seems like you could just use the formula in the spreadsheet to accomplish the same goal. Having said that, in your macro the formula closes out the xlookup but does not close out the if function. It should look more like:
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    07-29-2021
    Location
    Australia
    MS-Off Ver
    Office 365
    Posts
    2

    Re: VBA to insert formulas only in selected cells

    The reasoning for the macro is if the range is not within the past 30 days then I want nothing to change in the existing cell.
    The xlookup is looking at another table that only contains only the last 30 days of data. If I just had the formula in each cell then the when the data source updates to no longer include old data then the xlookup would break.

    Thanks for the pickup on the way the macro is written, I will try that out.

+ 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. Is there a Macro that can insert multiple photos to a selected range of cells.
    By ebridge in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-04-2016, 04:12 PM
  2. Insert cells, fill formulas down, set formulas for dynamic range
    By Snickers65 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-03-2013, 01:43 PM
  3. Code to insert rows between selected cells
    By DeepakS in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 08-07-2013, 08:18 AM
  4. Copying formulas down cells:proper cells are selected?
    By pgreenway in forum Excel General
    Replies: 2
    Last Post: 05-06-2011, 10:03 AM
  5. Macro to insert formulas to certain cells respective of cell selected
    By rain4u in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 04-21-2011, 10:51 PM
  6. Insert text next to selected cells
    By yunesm in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 11-19-2010, 12:01 PM
  7. Insert a line in chart and enter selected cells sum
    By BLRITCHIE in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-15-2009, 08:28 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