+ Reply to Thread
Results 1 to 5 of 5

How to make a loop run for variable criteria.

  1. #1
    Forum Contributor ANDREAAS's Avatar
    Join Date
    05-06-2013
    Location
    Port Elizabeth, South Africa
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    368

    How to make a loop run for variable criteria.

    Good Day Boys and Girls

    I have the code below that searches for area names in one worksheet and when it finds the name, copies the row to another specified worksheet.

    Problem is that there are hundreds of areas to search for, so i need a loop within the loop to search for each of the areas instead of me having to type out an if statement for each of the areas.

    Please Login or Register  to view this content.
    I have a sheet called "Ref." where all the areas is listed, so instead of repeating this
    Please Login or Register  to view this content.
    and this....
    Please Login or Register  to view this content.
    I'd like for the code to run through sheets("Ref.").Range("B9:B1000") and search for each of the areas listed there in the source sheet.


    Any help will be greatly appreciated.
    Last edited by ANDREAAS; 07-09-2019 at 05:17 AM.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to make a loop run for variable criteria.

    Please upload a workbook or a representative cut down copy, anonymised if necessary. It is always easier to advise if we can see your request in its context.

    Show a before and after situation with manually calculated results, explaining which information is data and which is results, and if it's not blindingly obvious how you have arrived at your results some explanatory notes as well.

    To upload a file click the Go Advanced button at the foot of your post, look underneath the post area for the Manage Attachments section and take it from there.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor ANDREAAS's Avatar
    Join Date
    05-06-2013
    Location
    Port Elizabeth, South Africa
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    368

    Re: How to make a loop run for variable criteria.

    Hi Richard

    See attacched as requested. i typed in red what the desired result should be. My code above works, only problem is that i currently would have to retype that bottom part over and over for each area.
    Attached Files Attached Files

  4. #4
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to make a loop run for variable criteria.

    To be perfetly honest I believe you are just storing up problems for yourself.

    You are making the same mistake that I see time and time again. You are mixing up the two elements of data capture and final reporting. The two require quite different treatments.

    A lot of people start by designing the form that they expect to see as the final report or which at first glance seems the best way of capturing data, and then wonder why it's so difficult to subsequently analyse and summarise or extract information from it. Yours exhibits all those features.


    You should always capture data in a simple two dimensional table and worry about reporting information from it afterwards. Without exception doing this you will always be able to easily obtain management information. Rarely is this the case if you start the other way round.
    You will also throw open the whole wonderful world of the powerful Pivot table functionality.

    So before you get too far with this I'd create a single sheet database that contains the following columns
    Date
    Area *
    Office Name *
    name for whatever is represented by columns C:K (15-23) - * Values would be one of 15 - 23
    Hours
    Number of Guards
    Grading *
    Shift Time


    The * values could be Validation drop down cells from which you could pick values. The way I normally arrange these things is to have a single data entry row above the database in which the new values are entered, then a button which runs a macro that adds the new record to the database.

  5. #5
    Forum Contributor ANDREAAS's Avatar
    Join Date
    05-06-2013
    Location
    Port Elizabeth, South Africa
    MS-Off Ver
    Excel 2007 / 2010
    Posts
    368

    Re: How to make a loop run for variable criteria.

    thank you Richard. much appreciated. Will change the approach as suggested.

+ 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] Can i make the column I want to loop a variable? (i,VARIABLE) instead of (i,2)
    By trenzalore888 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-31-2017, 12:48 PM
  2. [SOLVED] correctly 'wording' a for/next loop using a variable counter and variable filter
    By DBoyd121 in forum Excel Formulas & Functions
    Replies: 6
    Last Post: 07-14-2016, 06:13 PM
  3. [SOLVED] Loop rows and make a Range null if criteria met in defined Column
    By KEDENNIS in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-03-2016, 09:55 AM
  4. [SOLVED] VB Loop different variables and stop variable to find a new variable..
    By feroguz in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-14-2014, 04:54 PM
  5. Replies: 8
    Last Post: 07-09-2014, 10:42 PM
  6. Replies: 6
    Last Post: 12-21-2012, 08:03 AM
  7. Why did an inner loop variable start overwriting the outer loop range suddenly?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2012, 03:24 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