+ Reply to Thread
Results 1 to 4 of 4

Multiple condtion For loop, dynamic named range

  1. #1
    Registered User
    Join Date
    11-29-2012
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    2

    Multiple condtion For loop, dynamic named range

    Hi,

    This is my first play with VBA so apologise if the question is very easy and but I've been running into errors I cant resolve for the last couple of days.
    (if someone can suggest a good debugging tutorial would also be appreciated).

    I have a spreadsheet of building data energy use (15min intervals for many days, I've attached just two days). I'm trying to extract the time ranges where use is high/low or ramping up and down.

    All I think I need is a for loop, which loops through a named range and returns the value of the adjacent time column for the first cell which exceeds a bound (in this case this is the variable "mid"), following this I want to continue looping through the range and returns the time value where the value of the range drops below the variable "mid".

    My first problem is in this implementation loop, it gives an application/object definition error, which so far I can't resolve.

    I've attached the spreadsheet with my current VBA code

    Thanks for your time
    Attached Files Attached Files
    Last edited by emburl; 12-04-2012 at 06:41 PM. Reason: The code was confusing without comments being colured

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: Multiple condtion For loop, dynamic named range

    Hi

    A couple of things.

    rng is already a range, so the for loop should be

    Please Login or Register  to view this content.
    2) mid is a key word, so use something else - midd perhaps

    3) I've dummied up something that will put a comment in column M when the transition point is met. Hopefully it will give you some ideas.

    Please Login or Register  to view this content.
    rylo

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

    Re: Multiple condtion For loop, dynamic named range

    This fixes the syntax errors. I don't know if it produces the results you expect.

    Please Login or Register  to view this content.
    You were referencing Workbooks("CottonKirkSmall")
    The workbook's name is Workbooks("CottonKirkSmall.xlsm")
    The workbook reference is not really necessary unless you are switching between workbooks.

    You created a loop For Each c In Range(Rng)
    Changed to For Each c In Rng

    Debugging VBA

  4. #4
    Registered User
    Join Date
    11-29-2012
    Location
    Wellington, New Zealand
    MS-Off Ver
    Excel 2007
    Posts
    2

    Re: Multiple condtion For loop, dynamic named range

    Thanks rylo and AlphaFrog,

    The dual conditions for transition points is perfect. Because I'm working with multiple days in the same file, I think stuck with the named range and offset method. To do this I used rylo's logic for the if conditions, and used the c.Row property to return what cell satisfied the conditions.


    Please Login or Register  to view this content.
    There's undoubtedly more efficient ways to do this, and if anyone wants to suggest those feel free.

    Thank you
    Last edited by emburl; 12-04-2012 at 11:29 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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