Consulting

Results 1 to 11 of 11

Thread: Help with Loop

  1. #1

    Help with Loop

    Hello,
    I am new to VBA and am trying to enter an array into a loop. The initial array formula I used, and entered into the cells directly, was this:

    =IFERROR(INDEX(Color!$A:$A, SMALL(IF(ISBLANK(Color!$A:$A)+ISERROR(Color!$A:$A)++IF(Color!$B:$B<>A$1,1,0 ), "", ROW(Color!$A:$A)-MIN(ROW(Color!$A:$A))+1), ROW(1:1))),"")

    I would then copy and paste this formula down the column until all the rows with data would be filled, and then I would copy it across the columns (to the right) so all the columns would contain the formula. Obviously, a better way to do this would be to create a loop using VBA, correct? I am very new to VBA and tried to create a code that would do this but I cannot get it to work. This is what I have so far:

    [VBA]Sub Color()
    Worksheets("Dates").Activate
    Range("A2").Select
    Do
    Selection.FormulaArray = _
    "=IFERROR(INDEX(Color!$A:$A,SMALL(IF(ISBLANK(Color!$A:$A)+ISERROR(Color!$A: $A)++IF(Color!$B:$B<>A$1,1,0), """", ROW(Color!$A:$A)-MIN(ROW(Color!$A:$A))+1), ROWS(ActiveCell.Row - 1))),"" "")"
    ActiveCell.Offset(1, 0).Select
    Loop Until IsEmpty(ActiveCell.Offset(1, 0))
    End Sub[/VBA]

    Any suggestions??
    Thanks,
    Amy
    Last edited by Aussiebear; 04-13-2013 at 05:14 PM. Reason: Added the correct tags to the supplied code

  2. #2
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Not tested but i just fixed your formula, give it a go[VBA]Sub Color()

    Worksheets("Dates").Activate
    Range("A2").Select

    Do
    Selection.Formula = _
    "=IFERROR(INDEX(Color!$A:$A,SMALL(IF(ISBLANK(Color!$A:$A)+ISERROR(Color!$A: $A)++IF(Color!$B:$B<>A$1,1,0), """", ROW(Color!$A:$A)-MIN(ROW(Color!$A:$A))+1), ROWS(" & ActiveCell.Row & " - 1))),"" "")"
    ActiveCell.Offset(1, 0).Select
    Loop Until IsEmpty(ActiveCell.Offset(1, 0))

    End Sub[/VBA]
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  3. #3
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Abbab, I think this follows on from what were trying to do:

    [VBA]Sub Color()
    Worksheets("Dates").Activate
    Range("A2").Select
    Do
    Selection.FormulaArray = _
    "=IFERROR(INDEX(Color!$A:$A,SMALL(IF(ISBLANK(Color!$A:$A)+ISERROR(Color!$A: $A)+IF(Color!$B:$B<>A$1,1,0), """", ROW(Color!$A:$A)-MIN(ROW(Color!$A:$A))+1), ROW(ActiveCell)-1)),"" "")"
    ActiveCell.Offset(1, 0).Select
    Loop Until IsEmpty(ActiveCell.Offset(1, 0))
    End Sub[/VBA]

    The ROW and ROWS functions use a range as input rather than a number.
    sassora

  4. #4
    Thank you for the suggestions. I could not get Simon's to work, and with Sassora's the formula was placed in the cell but no value calculated. I have also been trying something like this to no avail as well:

    Option Explicit
    Sub ColorLoop()


    Dim xrow As Integer
    Dim xcol As Integer

    xrow = 1
    xcol = 1

    Do Until Cells(xrow, 1) = ""

    Cells(xrow + 1, xcol).Formula.Array = "=IFERROR(INDEX(Color!$A$2:$A$500, SMALL(IF(ISBLANK(Color!$A$2:$A$500)+ISERROR(Color!$A$2:$A$500)++IF(Color!$B $2:$B$500<>A$1,1,0), "", ROW(Color!$A$2:$A$500)-MIN(ROW(Color!$A$2:$A$500))+1), ROW())),"") "

    xrow = xrow + 1

    Loop

    End Sub

    Any suggestions??
    Thanks all!

  5. #5
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Can you post the sheet?
    sassora

  6. #6

    Response

    Sure, I have attached it! Please let me know if you need me to clarify anything on it!
    Thanks!
    Amy
    Attached Files Attached Files

  7. #7
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    What does the formula aim to work out? can you give more background?

    If you are looking to find the average difference in time between colours, a simple formula should suffice along the lines of: "((A$52-A$2+1)-I2)/I2".
    There is no need to sum all of the differences and then add them up.
    Last edited by sassora; 04-20-2013 at 06:32 AM.
    sassora

  8. #8
    Hi Sassora,
    Thank you for that formula! I tried it and it seems to work, it is just off a little bit. For example, when I calculate the "black" averages between dates I get 20.67, and the formula produces a result of 20 even, I calculate 21 for the "orange" average and the formula produces a result of 20, etc. Can you explain the discrepancies? Also, since this data will be added to continually, is there a way to set the range so it is not A$52, but continues down the column?
    Thanks so much!
    Amy

  9. #9
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    What range are you looking at?

    Can you attach a worksheet with calculations?
    Last edited by sassora; 05-03-2013 at 03:16 PM.

  10. #10
    Hi Sassora,
    Please see attached, the first sheet.
    Thanks!
    Amy
    Attached Files Attached Files

  11. #11
    VBAX Tutor
    Joined
    Jan 2008
    Posts
    262
    Location
    Where are the calculations that give 20.67? and 21?
    sassora

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •