+ Reply to Thread
Results 1 to 7 of 7

Only run code in macro if condition is met?

  1. #1
    Registered User
    Join Date
    03-29-2013
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    78

    Only run code in macro if condition is met?

    Hi all,


    So have a macro that is copying a worksheet (a Purchase Order template) so that I can automate making copies of said template and changing the PO no. in the new copy. See below...




    Please Login or Register  to view this content.


    That is all working fine and pretty straightforward up to that point.


    However what I would now like to do is add some code to this macro so that it will only action the copy and create new PO code for those PO numbers listed on a sheet I already have called 'Control Sheet' that lists the actual PO sheets required.

    That sheet looks like this:

    Header in A1 'Sheet Name' and in B1 'Print?
    and then down column A = A2: 'PO 1' A3: 'PO 2' etc. etc.
    and down column B = B2: 'PRINT', B3: 'PRINT' etc. etc.

    A B
    Row 1 Sheet Name Print?
    Row 2 PO 1 PRINT
    Row 3 PO 2 PRINT
    Row 4 PO 3 PRINT
    Row 5 PO 4 PRINT
    Row 6 PO 5 PRINT





    Any help will be gratefully received.


    Thank you very much in advance!



    Oliver
    Last edited by OLLY-7; 02-18-2018 at 01:41 PM.

  2. #2
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Only run code in macro if condition is met?

    Post Deleted - Tags Added.
    Last edited by xladept; 02-18-2018 at 02:01 PM.
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  3. #3
    Registered User
    Join Date
    03-29-2013
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    78

    Re: Only run code in macro if condition is met?

    Apologies and thank you for the notification.


    Duly corrected.

    Hope all good now.


    Regards

  4. #4
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Only run code in macro if condition is met?

    Change the name of your PO 1 sheet to PO Template. The code will copy PO Template and rename the copy as PO1 (just like the other PO Names) if it is in your Control Sheet list.

    Please Login or Register  to view this content.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  5. #5
    Registered User
    Join Date
    03-29-2013
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    78

    Re: Only run code in macro if condition is met?

    Thank you very much AlphaFrog!

    That works perfectly!

    Thank you!


    I did also have a macro that was deleting all sheets from 'PO 2' to 'PO 15' (when I was using PO 1 as my 'template').

    However that doesn't appear to be working any more - I guess because now there are likely to be less than 15 PO sheets at any one time.

    Could you kindly suggest another macro to delete any sheets starting with 'PO' / or that are listed on the control sheet?

    Or perhaps an extension to the code you provided above so that whenever it is run, it auto deletes any pre-existing PO sheets?


    Thank you very much again and kinds regards,


    Oliver

  6. #6
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,643

    Re: Only run code in macro if condition is met?

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    03-29-2013
    Location
    London
    MS-Off Ver
    Excel 2016
    Posts
    78

    Re: Only run code in macro if condition is met?

    Thank you.


    Again - works perfectly.

    Thanks again and best regards,


    Oliver

+ 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. Macro code to use rank fuction the values by condition
    By anay_zinje in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-22-2015, 02:15 PM
  2. [SOLVED] VBA code, Pop window displays if macro doesn't find requested condition
    By Tapyr in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-19-2015, 01:30 PM
  3. Replies: 0
    Last Post: 04-22-2015, 12:25 PM
  4. Add If condition to a code recorded by Macro recording function in Excel
    By Emma_B in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-29-2014, 01:36 AM
  5. Macro code using IF case with variable inputs for logical test condition
    By m2krishnan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-16-2013, 10:59 PM
  6. [SOLVED] Customize code for a macro that copies three rows to a new workbook based on a condition
    By philips in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 01-04-2013, 05:55 AM
  7. Running or bypassing code in a macro based on a certain condition...
    By kenab1 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-17-2010, 04:03 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