+ Reply to Thread
Results 1 to 5 of 5

Creating a loop or calling a sub for each value in a range...

Hybrid View

  1. #1
    Registered User
    Join Date
    06-28-2006
    Posts
    28

    Question Creating a loop or calling a sub for each value in a range...

    Hi,

    I am having problems adding attachements, so I can't upload a spreadsheet - I will try explain this the best I can!

    I have a spreadsheet with different tabs on. On the first tab called "Front" I have a list of identifiers in a range from N16 to N26. The amount of identifiers changed constantly. Sometimes there maybe 1, 4 or 0 etc.

    These identifiers relate to data on another tab "Portfolio", that I want to copy into a further different tab "Index" and run a vlookup

    So on the sheet "Front" within the range N16 to N26, I want to be able to run the following code (Macro 3) for any of the identifiers within that range, where ActiveCell is in this case the first cell in the range N16:

        ActiveCell.Copy
        Sheets("Index").Select
        Range("B4").Select
        Selection.End(xlDown).Offset(1, 0).PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        ActiveCell.Offset(0, 1).FormulaR1C1 = "=VLOOKUP(RC[-1],Portfolio!C[-1]:C[16],2,FALSE)"
        ActiveCell.Offset(0, 2).FormulaR1C1 = "=VLOOKUP(RC[-2],Portfolio!C[-2]:C[15],3,FALSE)"
        ActiveCell.Offset(0, 8).FormulaR1C1 = "=VLOOKUP(RC[-8],Portfolio!C[-8]:C[8],9,FALSE)"
        ActiveCell.Offset(0, 9).FormulaR1C1 = "=VLOOKUP(RC[-9],Portfolio!C[-9]:C[8],10,FALSE)"
        ActiveCell.Offset(0, 13).FormulaR1C1 = "=VLOOKUP(RC[-13],Portfolio!C[-13]:C[3],14,FALSE)"
        ActiveCell.Offset(0, 17).FormulaR1C1 = "=VLOOKUP(RC[-17],Portfolio!C[-17]:C[3],18,FALSE)"
    I am assuming there is some kind of loop that can do this that will run or exit loop if there are no identifiers, but I don't have any experience with loops. Either that or call this sub for each cell in a range.

    Many thanks for taking the time to look - I will try again later to edit post and load a spreadsheet.

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Creating a loop or calling a sub for each value in a range...

    Have you tried zipping your file before attaching?
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    06-28-2006
    Posts
    28

    Re: Creating a loop or calling a sub for each value in a range...

    Aha! That has worked - thanks for the advice.
    Attached Files Attached Files

  4. #4
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200

    Re: Creating a loop or calling a sub for each value in a range...

    Why can't you do the VLOOKUP without the copying?

  5. #5
    Registered User
    Join Date
    06-28-2006
    Posts
    28

    Re: Creating a loop or calling a sub for each value in a range...

    I need the identifier on the Index tab becuase that code is used on the Positions tab. So if you take a look at the Postions tab in column A, that takes the data from the "Index" tab, which includes the Identifier.

    The number of rows on the Index tab changes, hence why there is the formula in column A in Positions.

+ 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