VBA For Each Loop

  • Trying to code a VBA script that will loop through Col A, B, C, D, E, F, and G, look for the value and only return the First value found in Col H. If no values found, then return "IN-SCOPE" in Col H.


    [TABLE="width: 500"]

    [tr]


    [td]

    Col A

    [/td]


    [td]

    Col B

    [/td]


    [td]

    Col C

    [/td]


    [td]

    Col D

    [/td]


    [td]

    Col E

    [/td]


    [td]

    Col F

    [/td]


    [td]

    Col G

    [/td]


    [td]

    Col H

    [/td]


    [/tr]


    [tr]


    [td]

    Retired

    [/td]


    [td][/td]


    [td][/td]


    [td][/td]


    [td][/td]


    [td][/td]


    [td][/td]


    [td]

    Retired

    [/td]


    [/tr]


    [tr]


    [td]

    Retired

    [/td]


    [td][/td]


    [td]

    PSFIN

    [/td]


    [td][/td]


    [td]

    PKU

    [/td]


    [td][/td]


    [td][/td]


    [td]

    Retired

    [/td]


    [/tr]


    [tr]


    [td][/td]


    [td][/td]


    [td]

    PSFIN

    [/td]


    [td]

    FIRE

    [/td]


    [td][/td]


    [td]

    EX

    [/td]


    [td]

    LAB

    [/td]


    [td]

    PSFIN

    [/td]


    [/tr]


    [tr]


    [td][/td]


    [td][/td]


    [td][/td]


    [td][/td]


    [td][/td]


    [td][/td]


    [td][/td]


    [td]

    IN-SCOPE

    [/td]


    [/tr]


    [/TABLE]

  • Re: VBA For Each Loop


    Try this


    Assumes Row 1 is a header row (including a header for column H) and data starts in cell B1


    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: VBA For Each Loop


    Please attach a sample of your workbook

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: VBA For Each Loop


    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: VBA For Each Loop


    You're welcome

    We now have a reputation system in place. If my reply helped please "Like" the reply by clicking the "Like" icon at bottom right of my reply.

  • Re: VBA For Each Loop


    You could also achieve this via the following array formula*:


    {=IFERROR(INDEX(A2:G2,MATCH(TRUE,A2:G2<>"",0)),"IN-SCOPE")}


    Robert


    * Array formulas must be confirmed with CTRL+SHIFT+ENTER after typing or modifying them. When done correctly, Excel will automatically add braces, {}, around the formula. Do not simply type the braces yourself.

  • Re: VBA For Each Loop


    Non Array


    =IFERROR(INDEX(A2:G2,0,MATCH("*",A2:G2,0)),"IN-SCOPE")


    VBA

    Code
    Sub test()
        With Cells().CurrentRegion
            .Columns("h").Offset(1).Resize(.Rows.Count - 1).FormulaR1C1 = _
            "=iferror(index(rc1:rc7,0,match(""*"",rc1:rc7,0)),""IN=SCOPE"")"
        End With
    End Sub

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!