+ Reply to Thread
Results 1 to 5 of 5

Looping problem

  1. #1
    Forum Contributor
    Join Date
    08-04-2013
    Location
    Bucharest
    MS-Off Ver
    Office Professional Plus 2019
    Posts
    304

    Looping problem

    Hi I'm having trouble getting a macro to loop through a list of names

    Please Login or Register  to view this content.
    I have BO2:BO6, a list of 5 names, I want the macro take the first name, copy that name to BK1, then run the import macro (which will generate some numbers in sheet2). copy those numbers (4 cells) in the cell right next to the corresponding name (BP column).. then loop through the next name, do the same thing, and so on until the end of the list.

    I have made several mistakes, as it doesn't loop, but rather imports only the first or the last name in the list and then stops.
    Thanks for your time
    Last edited by bibu; 07-19-2014 at 05:53 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    13,001

    Re: Looping problem

    You are using "ActiveSheet" in your code; you should use the actual sheet name instead.
    You are initializing lr & lr1 to the last row on some sheet, but there is no way from the code to tell what sheet it is, or should be.
    It would be better for readabiliity and debugging to get the name of the sheet that is active when the code starts.

    Dim Source as Worksheet
    Set Source =Activesheet
    Then

    lr=Source.Cells(..... etc.

    Notice that in the top your loop you say:
    ActiveSheet.Range("BK1") = cell.Value

    but at the bottom you say, Sheets("Sheet2").Activate. So, when the loop increments to the next cell, it looks on Sheet2. Is that what you want?
    Ben Van Johnson

  3. #3
    Forum Contributor
    Join Date
    08-04-2013
    Location
    Bucharest
    MS-Off Ver
    Office Professional Plus 2019
    Posts
    304

    Re: Looping problem

    Please Login or Register  to view this content.
    Thanks for your reply
    I have tried to not use activesheet.
    Yes the list of names is in Sheet2,so that is where I want the loop to increment.
    What happens now when I am debugging is it's only using the last name in the list and stops. And when I restart the macro it still uses the last name.
    I have attached a screenshot with the results after running the macro 9 times (once it imported a different name) not sure why.
    https://www.dropbox.com/s/2v9zogd97t...720_013915.jpg

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    Win10/MSO2016
    Posts
    13,001

    Re: Looping problem

    lr1 is set outside the loop, say, lr1 =10, then ws1.Range("BP" & lr1 + 1).PasteSpecial Paste:=xlPasteValues, will reference "BP11". Note that lr1 remains at 10, it does not increment. So the values are always pasted into row 11.

    Please Login or Register  to view this content.

  5. #5
    Forum Contributor
    Join Date
    08-04-2013
    Location
    Bucharest
    MS-Off Ver
    Office Professional Plus 2019
    Posts
    304

    Re: Looping problem

    Sorry for the delay in marking this as solved! Many thanks!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. HOW TO: Pause loop, fix error on a popup UserForm, continue loop
    By AndyMachin in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 06-19-2014, 04:37 PM
  2. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  3. Why did an inner loop variable start overwriting the outer loop range suddenly?
    By 111StepsAhead in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-16-2012, 03:24 PM
  4. Macro Loop Broken. Detects Max but doesn't continue loop
    By herchenbach in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-21-2011, 12:17 PM
  5. Loop a column on Sheet1 and loop a row on Sheet2 to find a match
    By johnnywinter in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 02-13-2009, 02:09 PM

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