+ Reply to Thread
Results 1 to 4 of 4

Trying to get a loop started

  1. #1
    Registered User
    Join Date
    12-05-2008
    Location
    Missouri
    Posts
    30

    Trying to get a loop started

    I'm trying to get a loop started to perform tasks and then go to the next line if it has info in it and do the same thing, until it gets to a blank line, and can't seem to get it right. It's been a while since i've written macros, so i'm sure i'm doing something completely wrong. Any help would be much appreciated. So far I have:
    Please Login or Register  to view this content.
    Last edited by abertrand; 12-19-2008 at 11:21 AM. Reason: Added code tags with note to OP how do this next time

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Post

    Quote Originally Posted by abertrand View Post
    I'm trying to get a loop started to perform tasks and then go to the next line if it has info in it and do the same thing, until it gets to a blank line, and can't seem to get it right. It's been a while since i've written macros, so i'm sure i'm doing something completely wrong. Any help would be much appreciated. So far I have:

    Sub Data()
    '
    ' Data Macro
    ' Macro recorded 12/4/2008 by abertrand
    '
    ' Keyboard Shortcut: Ctrl+d
    '
    ActiveCell = A4

    Do While ISBLANK(ActiveCell) = False

    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-1],'HF Database.xls'!C1:C12,2,FALSE)"
    ActiveCell.Offset(0, 2).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-3],'HF Database.xls'!C1:C12,3,FALSE)"
    ActiveCell.Offset(0, 2).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-5],'HF Database.xls'!C1:C12,4,FALSE)"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-6],'HF Database.xls'!C1:C12,5,FALSE)"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-7],'HF Database.xls'!C1:C12,6,FALSE)"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-8],'HF Database.xls'!C1:C12,7,FALSE)"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-9],'HF Database.xls'!C1:C12,8,FALSE)"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-10],'HF Database.xls'!C1:C12,9,FALSE)"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-11],'HF Database.xls'!C1:C12,10,FALSE)"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-12],'HF Database.xls'!C1:C12,11,FALSE)"
    ActiveCell.Offset(0, 1).Range("A1").Select
    ActiveCell.FormulaR1C1 = "=VLOOKUP(RC[-13],'HF Database.xls'!C1:C12,12,FALSE)"
    ActiveCell.Offset(1, -13).Range("A1").Select

    Loop

    End Sub
    Hi, and welcome to the forum.

    Please take a moment to review the forum rules which require code to be entered here between code tags. On this occasion and as it's your first post I'll let this go.

    I think you may be confusing what a loop is used for. In the above code it appears that within your loop you're also moving the active cell, when presumably you're wanting the loop itself to iterate a number of times times. Moreover you appear to be selecting A1 over and over again.

    Why not use a counter within your loop which can be used as an element in an Offset instruction. It's not quite clear what you're trying to do since the RC[-1] type instruction doesn't seem to increase by 1 each time. I'm assuming that's an error and it should. In which case something like

    Please Login or Register  to view this content.
    That's untested but hopefully the simple structure will give you the idea. Better still upload a sample workbook.

    Regards

    HTH

  3. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    Hello abertrand,

    I added th code tags to your post. Here is how to do it next time...

    How to wrap your Code
    On the Message window Toolbar you will see the # icon. This will automatically wrap the text you selected with the proper Code tags to create a Code Window in your post. You can do this manually by placing the tag [code]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/code] at the end.

    As a new member please take so time to familiarize yourself with the Do's and Don'ts here in the Forum, just click on the link below...

    Forum Rules

    Sincerely,
    Leith Ross

  4. #4
    Registered User
    Join Date
    12-05-2008
    Location
    Missouri
    Posts
    30
    I understand what a loop is used for, and the VLOOKUPS I want to perform are correct, not all the cells I need populated in are right after each other.

    What I am trying to do is pull a number from the cell(cell1) beginning of the row and VLOOKUP for the numbers I need, which is pretty basic.

    What I am trying to do with the LOOP is make it then go to the cell(cell2) below cell 1 (the first cell), and if that cell has information in it populate the corresponding cells using the VLOOKUP. This should be done until it gets to a blank cell.

+ 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