+ Reply to Thread
Results 1 to 8 of 8

Looping through a range and inserting a formula where condition met

  1. #1
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Looping through a range and inserting a formula where condition met

    I am having trouble looping through a range and inserting a formula where a condition is met.

    My range is "h9:i"

    My current code inserts a formula in column i when there is a value in the adjacent row in column h:

    Please Login or Register  to view this content.
    However, because there are so many rows (12,000+) it is taking a considerable length of time to run.

    Some of the cells in column i will already contain the relevant formula, so to speed this up I want to insert the formula in column i only when the cell value (in column i) is not null, as opposed to inserting it for all cells within the range.

    I know how to loop through the range, but the problem is the 'H9' cell reference in the formula in the code above will need to change depending on what row it is being inserted into- so if it is going into "i31" this will need to be "h31".

    This is the bit I am stuck on.

    Hope that makes sense...any ideas?

    Strud

  2. #2
    Forum Expert millz's Avatar
    Join Date
    08-14-2013
    Location
    Singapore
    MS-Off Ver
    Excel, Access 2016
    Posts
    1,694

    Re: Looping through a range and inserting a formula where condition met

    Not sure if I understood you correctly, you want to put formula in column I when cell is blank or not blank?

    Please Login or Register  to view this content.
    多么想要告诉你 我好喜欢你

  3. #3
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: Looping through a range and inserting a formula where condition met

    yeah it was to insert where they are blank.

    So your edit works perfectly, speeds it up massively.

    Thanks for your help!

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Looping through a range and inserting a formula where condition met

    Your code isn't looping. It just puts the formula in every cell in the range. If you change it to a loop, it will take longer. Maybe qualify the range with .SpecialCells(xlCellTypeBlanks) if some of the cells already have values or a formula. I suspect the problem may be calculation time based on the named ranges.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  5. #5
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: Looping through a range and inserting a formula where condition met

    Yeah I know it wasn't looping, I thought looping would be the way to solve it, but .SpecialCells(xlCellTypeBlanks) works a treat

    Cheers!

  6. #6
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Looping through a range and inserting a formula where condition met

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

  7. #7
    Forum Contributor
    Join Date
    04-19-2013
    Location
    Yorkshire, England
    MS-Off Ver
    Excel 2010
    Posts
    297

    Re: Looping through a range and inserting a formula where condition met

    done.

    cheers.

  8. #8
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,446

    Re: Looping through a range and inserting a formula where condition met

    You're welcome. Thanks for the rep.

+ 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. [SOLVED] Looping Subject to a Condition
    By andrewc in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 09-11-2013, 10:10 AM
  2. Inserting formula into cell/range refering to a range
    By CobertRannon in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-30-2013, 01:28 PM
  3. Inserting a formula into a range of cells using VBA
    By NBVC in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-13-2007, 11:11 AM
  4. [SOLVED] Looping and inserting negaive totals
    By Jim May in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-25-2006, 11:15 AM
  5. looping for a condition
    By jhahes in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-30-2005, 07:05 PM

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