+ Reply to Thread
Results 1 to 12 of 12

Loops within loops?

  1. #1
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Loops within loops?

    Hi all,

    Hope all is well in your macro worlds and beyond!

    Trying to accomplish a loop within a loop for LOOKUP data from 12 worksheets into a summary sheet.

    The macro is meant to do the following:
    1. Check if column B in the summary sheet contains a value - if so:
    a) Input into column G in the summary sheet a VLOOKUP to that data sheet to pull in a value (or 0.00 IFERROR).
    b) Do as (1a) for each of the 12 data sheets (columns G to R in total).
    c) Cycle through each code in column B completing (1a) and (1b).
    2. Otherwise cycle through to the next code in column B.

    I get an object defined error on my FormulaR1C1 - I think I have not correctly written it so the macro can use the k variable for each data worksheet number (1-12).

    I have posted the formula and worksheet:

    Please Login or Register  to view this content.


    I would greatly apprecaite any input on correcting / improving my code if willing!


    Many thanks for any help in advance!


    Coeus.
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loops within loops?

    'Formula to look up the 7th column in each monthly data sheet.
    Do you mean the return values comes from column G if column B of summary sheet and column A of the rest of sheets match.
    In other words, do you just want to return a single value? What would happen if there are duplicate codes on sheets?

  3. #3
    Forum Expert mikerickson's Avatar
    Join Date
    03-30-2007
    Location
    Davis CA
    MS-Off Ver
    Excel 2011
    Posts
    6,229

    Re: Loops within loops?

    Try
    "=IFERROR(VLOOKUP(RC[" & (-j+1) & "], 'Data - Month & k'!C[" & (-j) & "]:C, 7, FALSE),0)"
    _
    ...How to Cross-post politely...
    ..Wrap code by selecting the code and clicking the # or read this. Thank you.

  4. #4
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Loops within loops?

    Hi AB33. There will never be duplicate codes on the sheet - the data comes from a database where each code is a primary key.

    Yeah your explanation matches what is the desired outcome!


    Thanks for taking a look :D

  5. #5
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Loops within loops?

    Hi mikerickson,

    I have input that but then the workbook loops through and tries to open another workbook!

    Had to CTRL ALT DELETE to close the Excel programme :-(

  6. #6
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loops within loops?

    Coeus,
    If the return values comes from column G, Am I right this is copied in to summary G14 and down or is it G14 across?
    You mentioned "columns G to R in total)".

  7. #7
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Loops within loops?

    Hi Coeus,
    try it
    Please Login or Register  to view this content.

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loops within loops?

    Try the attached.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    01-22-2013
    Location
    England
    MS-Off Ver
    Excel 2010
    Posts
    55

    Re: Loops within loops?

    Many many thanks to all that have input!

    I have gone with nilem's approach as looks the simplest and got the result!

    If I can ask a few questions on your code so I understand?


    Dim i&, j& - do these need to be defined? In my original code I just noted 'For i = 1 to 1000'?

    With Sheets("Summary") - I never use the 'With' function mainly out of ignorance - is this better than directly defining the sheet as in my original code ' If Sheets("Summary").Cells(i, 2) <> 0 Then'?

    .Cells(Rows.Count, 2).End(xlUp).Row - now this looks like it removes my limit on i being 1000. 1000 was more than likely ever to be required - yours more efficiently limits to the end of the row if I understand which is much more efficient! If you could clarify a bit for me on this:

    - Rows.Count - this counts to the final row i.e. ignores and blanks and keeps going until no data exists below a point in the column?

    - .End(xlUp).Row - why is this necessary? I would imagine the 'Rows.Count' defines the very last row in the column so .Cells(Rows.Count, 2) would be sufficient?

    " & j & " - this is why my VLOOKUP was not working - I have to seperate any variable within a code using & VARIABLE &.

    Lastly you removed my k variable and instead noted j + 6 - genius!


    Again many thanks learning so much!


    Coeus.

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loops within loops?

    Dim i&, j& - do these need to be defined? In my original code I just noted 'For i = 1 to 1000'?
    No necessary! In VBA, unlike some other languages, do not need to define your variables, but for efficiency and tracing of errors, it is a good practice to declare your variables with option explicit.
    'For i = 1 to 1000'?
    you are looping 1000 times. For speed purpose, you should only loop up to the last non empty row with data.

    With Sheets("Summary")
    Again, you can work with out WITH END WITH, but for efficiency running of code and to avoid repeating the same statement again and again, it is a good practice to use if you understand it.

    .Cells(Rows.Count, 2).End(xlUp).Row - now this looks like it removes my limit on i being 1000. 1000
    this avoids using a fixed loop 1 to 1000. Your data may only gone down to ten rows, yet you are looping 1000 times, so use the last non empty row with data.


    " & j & " - this is why my VLOOKUP was not working
    if you have a string with your code, you should put them in quotation mark

    j + 6
    means increase the column by for each loop.
    I hope this helps a bit.

  11. #11
    Forum Expert nilem's Avatar
    Join Date
    10-22-2011
    Location
    Ufa, Russia
    MS-Off Ver
    2013
    Posts
    3,377

    Re: Loops within loops?

    @AB33 Thanks AB, it's great

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: Loops within loops?

    Nilem,
    It is a beer time!
    Any Russian beer you can recommend?

+ 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. Do until loops
    By tryer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-17-2011, 11:03 AM
  2. Loops
    By TheGainmaker in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-17-2011, 12:04 PM
  3. VBA Loops
    By mistadarcy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-16-2011, 01:57 PM
  4. [SOLVED] Loops...
    By Willabo in forum Excel General
    Replies: 2
    Last Post: 06-14-2006, 11:15 AM
  5. [SOLVED] For next loops
    By Kate in forum Excel General
    Replies: 5
    Last Post: 05-22-2006, 08:15 AM

Tags for this Thread

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