+ Reply to Thread
Results 1 to 12 of 12

For EACH cell in specific range meeting specific criteria, call a sub

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    For EACH cell in specific range meeting specific criteria, call a sub

    Looking to have a macro call a subroutine every time it finds a cell meeting specific criteria.

    Code in plain english would look like this:

    For EACH cell in range A1:BZ500 meeting the following criteria:

    Cell value is a date

    AND

    Cell's date is at least a week or more in the future

    AND

    Cell background (Fill) = RGB color code: (R:191 G:191 B:191)



    DO the following:


    Call repeatingsub

  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: For EACH cell in specific range meeting specific criteria, call a sub

    Hi,

    Are you sure you understand the implications of looping through 39000 cells or was that a typo?
    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
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    Re: For EACH cell in specific range meeting specific criteria, call a sub

    Quote Originally Posted by Richard Buttrey View Post
    Hi,

    Are you sure you understand the implications of looping through 39000 cells or was that a typo?
    Well is there a way to define a range as the last row and column used on the VISIBLE worksheet?

    The entire worksheet has 39k cells, however only a few thousands are actually visible (based on applied filters).

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,808

    Re: For EACH cell in specific range meeting specific criteria, call a sub

       Dim C As Range
       
       For Each C In Range("A1:BZ500")
       
          If IsDate(C.Value) And C.Value > Date + 7 And C.Interior.Color = RGB(191, 191, 191) Then
             repeatingsub
          End If
          
       Next C
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    Re: For EACH cell in specific range meeting specific criteria, call a sub

    Quote Originally Posted by 6StringJazzer View Post
       Dim C As Range
       
       For Each C In Range("A1:BZ500")
       
          If IsDate(C.Value) And C.Value > Date + 7 And C.Interior.Color = RGB(191, 191, 191) Then
             repeatingsub
          End If
          
       Next C

    I tried the following:

     Sub test()
     
     
     
     Dim C As Range
       
       For Each C In Range("a1:aw250")
       
          If IsDate(C.Value) And C.Value > Date + 7 And C.Interior.Color = RGB(191, 191, 191) Then
             
             Dim testmsg As String
             testmsg = ActiveCell.Value
             
            
             MsgBox testmsg
             
          End If
          
       Next C
    End Sub
    And it hangs on the same cell (with a date from over a year ago, which does not meet the criteria of being a week or more in the future) , displaying its value in a message box over and over without moving on to the next cell. What am I doing wrong?

  6. #6
    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: For EACH cell in specific range meeting specific criteria, call a sub

    What does the repeating sub actually do?

    Knowing that might determine whether you need a looping procedure at all.

  7. #7
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    Re: For EACH cell in specific range meeting specific criteria, call a sub

    Quote Originally Posted by Richard Buttrey View Post
    What does the repeating sub actually do?

    Knowing that might determine whether you need a looping procedure at all.
    I haven't written it yet, but basically it's going to use each identified cell as a reference point, capturing it's column and row as seperate variables, and using them for a few other FOR EACH and DO UNTIL routines.

    The end result is populating a bunch of outlook appointments on a calendar.


    So basically once it finds a cell meeting the criteria it would save variables:

    graydatecol
    graydaterow

    and then do a bunch of stuff with those variables. when a new gray date cell is found, the variables reset but the same sub is run (making a new outlook appointment).

  8. #8
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,808

    Re: For EACH cell in specific range meeting specific criteria, call a sub

    The For loop is cycling through each cell in the Range. But it does not make that cell active. Your MsgBox call uses ActiveCell (which I discourage in most cases), and ActiveCell never changes. The code is not getting hung up on one cell, just the message is.

    Try this:

     Sub test()
     
     
     
     Dim C As Range
       
       For Each C In Range("a1:aw250")
       
          If IsDate(C.Value) And C.Value > Date + 7 And C.Interior.Color = RGB(191, 191, 191) Then
             
              MsgBox "Value of cell " & C.Address & ": " & C.Value
             
          End If
          
       Next C
    End Sub
    Last edited by 6StringJazzer; 07-02-2014 at 10:24 AM.

  9. #9
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    Re: For EACH cell in specific range meeting specific criteria, call a sub

    Quote Originally Posted by 6StringJazzer View Post
    The For loop is cycling through each cell in the Range. But it does not make that cell active. Your MsgBox call uses ActiveCell (which I discourage in most cases), and ActiveCell never changes. The code is not getting hung up on one cell, just the message is.

    Try this:

     Sub test()
     
     
     
     Dim C As Range
       
       For Each C In Range("a1:aw250")
       
          If IsDate(C.Value) And C.Value > Date + 7 And C.Interior.Color = RGB(191, 191, 191) Then
             
              MsgBox "Value of cell " & C.Address & ": " & C.Value
             
          End If
          
       Next C
    End Sub

    The msgbox pulling the value of the activecell was a neccesary test since the sub that will actually be called runs entirely off activecell commands. So in order for the next sub to work, I need to be able to reference whatever the active c is as "activecell".

    for example, I will be doing such things as:

    "Once C is found, save the column number it's in as variable X and the row number as variable Y"

    Then I would run other tasks based on it's row and column (basically pulling other sets of data from the same row/column)

    Will this script allow that to happen?
    Last edited by ks100; 07-02-2014 at 01:35 PM.

  10. #10
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2404
    Posts
    24,808

    Re: For EACH cell in specific range meeting specific criteria, call a sub

    First, it is not necessary to quote every post when responding. Usually a quote is not necessary at all; include a quote if you are not responding to the most recent post in the thread, or if you want to highlight something specific that you are responding to.

    Quote Originally Posted by ks100 View Post
    ...the sub that will actually be called runs entirely off activecell commands.
    This is not a best practice. (This is what is known as "common environment coupling".) It is undesirable for exactly the reason you see here. The fact that your sub depends on ActiveCell is undocumented and unenforceable. The repeating sub has no control over or visibility to what cell is active at any given time. Future changes in either sub that affect ActiveCell have the potential to introduce bugs that are very difficult to diagnose. I recommend that you add a single parameter to your sub to give the range of the cell to act on, and pass in C when you call it.

    If you absolutely must use ActiveCell then you have to activate the cell before calling:

    C.Activate
    repeatingsub

  11. #11
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    Re: For EACH cell in specific range meeting specific criteria, call a sub

    I would love to know a more efficient/stable way. I'll post a sample workbook shortly.

  12. #12
    Forum Contributor
    Join Date
    09-04-2013
    Location
    USA
    MS-Off Ver
    Excel 2010
    Posts
    298

    Re: For EACH cell in specific range meeting specific criteria, call a sub

    The image below is a very simplified sample of the spreadsheet I am working with and what I want to accomplish. I can figure out most of the code myself, but I am wondering what the best way to accomplish all this is, as the only way I know how to do it is with activecell.

    I am not expecting the code to be written for me, but I was hoping someone could point me in the right direction as far as showing me what function to use to accomplish the capturing of task, person and department names, and a brief example of how it works.



    example72.jpg

+ 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] Formula to return specific text based on multiple cells meeting a single criteria
    By missydanni in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 08-13-2013, 09:48 AM
  2. [SOLVED] how to stop Command button If specific number is entered in a specific cell range
    By sspreyer in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-26-2013, 05:10 AM
  3. Syntax to call a specific workbook and specific worksheet using VBA?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-21-2013, 05:32 PM
  4. [SOLVED] How to select records meeting specific criteria?
    By billj in forum Excel General
    Replies: 6
    Last Post: 02-20-2013, 06:17 AM
  5. Using formulas to extract data meeting 2 specific criteria
    By Levie in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 11-01-2012, 09:27 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