+ Reply to Thread
Results 1 to 4 of 4

Repeat code for several rows with If statement

  1. #1
    Registered User
    Join Date
    03-28-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    3

    Repeat code for several rows with If statement

    Hi,

    I am a vba newbie and am trying to replicate the below code for 25 rows (rows 7-31).
    the code does a vlookup for certain cells dependant on data validation in another cell.
    I am not familiar with loops in vba and was hoping someone could help me with that.
    Any help would be greatly appreciated.

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Range("F7") = "Yes" Then
    If (Target.Address = "$H$7") Then
    If Range("G7") = "" Then
    MsgBox ("Column G cannot be blank if you selected yes.")
    Range("F7").Select
    Else
    Range("H7").Select
    ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-1],'[abc.xlsx]0210 - IDDAs'!R1C3:R65536C11,3,FALSE)"
    Range("I7").Select
    ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-2],'[abc.xlsx]0210 - IDDAs'!R1C3:R65536C11,5,FALSE)"
    Range("J7").Select
    ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-3],'[abc.xlsx]0210 - IDDAs'!R1C3:R65536C11,6,FALSE)"
    Range("K7").Select
    ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-4],'[abc.xlsx]0210 - IDDAs'!R1C3:R65536C11,8,FALSE)"
    Range("L7").Select
    ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-5],'[abc.xlsx]0210 - IDDAs'!R1C3:R65536C11,9,FALSE)"
    End If
    Else
    End If
    Else

    If (Target.Address = "$K$7") Then
    Range("K7").Select
    ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-1],'[abc.xlsx]0210 - IDDAs'!R1C8:R65536C11,3,FALSE)"
    Range("L7").Select
    ActiveCell.FormulaR1C1 = _
    "=VLOOKUP(RC[-2],'[abc.xlsx]0210 - IDDAs'!R1C8:R65536C11,4,FALSE)"
    Else
    End If

    End If

    End Sub

  2. #2
    Valued Forum Contributor JeanRage's Avatar
    Join Date
    03-02-2009
    Location
    Nice, France
    MS-Off Ver
    Excel 2003
    Posts
    705

    Thumbs down Re: Repeat code for several rows with If statement

    Hi,

    Among many possibilities ...
    Please Login or Register  to view this content.
    HTH

  3. #3
    Registered User
    Join Date
    03-28-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Repeat code for several rows with If statement

    Quote Originally Posted by JeanRage View Post
    Hi,

    Among many possibilities ...
    Please Login or Register  to view this content.
    HTH
    Thanks so much JeanRage.
    I tried it and it doesnt work.
    I guess I am not doing something else correctly.
    Can I use the same syntax for target.address code?
    e.g. If (target.Address = "$H" & i) Then blah blah
    or is there a better way to do this?

  4. #4
    Registered User
    Join Date
    03-28-2010
    Location
    USA
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: Repeat code for several rows with If statement

    Never mind! I used (Target.row=i and target.column = 5) instead of target.address and it worked!
    Thanks a million again!
    You got me on the right track!

+ 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