+ Reply to Thread
Results 1 to 7 of 7

Nested Loops

  1. #1
    Registered User
    Join Date
    01-13-2014
    Location
    a
    MS-Off Ver
    Excel 2010
    Posts
    5

    Nested Loops

    Hi,

    Just wondering if someone could help me with this issue.



    i = 15
    j = 15

    Do While Cells(i, 1) <> ""
    Month = Left(Cells(i, 5), 6)

    Do Until Month = (Cells(j, 31))
    If Month = Cells(j, 31) Then Exit Do
    Else
    j = j + 1
    Loop
    Cells(i, 6) = Cells(j, 31)
    i = i + 1
    Loop
    The first error I get is about the Else (else without if), I take it that I'm not using the "exit do"

    If I try to code that differently I then find I get another error saying "loop without do" so again I take it that I'm not coding the nested loop correctly.

    Can someone help point me in the right direction?


    The overall aim of the code is to go through a column, compare the first six characters to another column; find a match, and copy the corresponding value in the row back in beside the original list.

  2. #2
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Nested Loops

    A couple of things....indenting really helps "seeing" how things work....I threw in the End IF and Month is an actual Function in VBA so I changed it to MyMonth....AND...declaring your variable is always a good idea...HTH

    Please Login or Register  to view this content.
    Ernest

    Please consider adding a * if I helped

    Nothing drives me crazy - I'm always close enough to walk....

  3. #3
    Forum Expert Ron Coderre's Avatar
    Join Date
    03-22-2005
    Location
    Boston, Massachusetts
    MS-Off Ver
    2013, 2016, O365
    Posts
    6,996

    Re: Nested Loops

    Can you post some sample data...including the results you want to see?
    Ron
    Former Microsoft MVP - Excel (2006 - 2015)
    Click here to see the Forum Rules

  4. #4
    Registered User
    Join Date
    01-13-2014
    Location
    a
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Nested Loops

    Thanks for the replies

    I've tried a different approach, I'm getting some output but it's not correct.

    Please Login or Register  to view this content.

    This is the input (I'm taking the first five characters):

    Jul14Aug
    Jul14Aug
    Jul14
    Jul14
    Jul14-Aug
    Jun14-Dec


    And comparing it to these two columns:


    Jan14 25
    Feb14 32
    Mar14 123
    Apr14 32
    May14 23
    Jun14 233
    Jul14 231
    Aug14 123



    The idea is to take find the corresponding month, and then copy the number beside the corresponding month, and place it beside the original input.

    So this input:
    Please Login or Register  to view this content.
    Would produce this output:


    Please Login or Register  to view this content.

  5. #5
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Nested Loops

    Here is something that might get you closer...I reset the counter j so when you go through the loop it starts over...also, you are trying to retrieve column 31, not 29 (I think)...Ron, from Post #3 brings up a good point in that posting an actual excel workbook kindof takes the guess work out....Also, your matching criteria for the 1st 5 characters isn't going to catch the 14-Jul matches...you'll have to determine if there is a "-" in the the first 5 characters and then remove it....check INSTR().....

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    01-13-2014
    Location
    a
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Nested Loops

    Ah, yes. Resetting j. That's what the issue was. Thanks, really appreciate it!

  7. #7
    Forum Expert judgeh59's Avatar
    Join Date
    02-07-2013
    Location
    Boise, Idaho
    MS-Off Ver
    Excel 2016
    Posts
    2,310

    Re: Nested Loops

    glad I could help....In what looks like a small VBA module dimensioning your variables isn't a real "big" thing, but as you grow the size of your modules, the dimensioning becomes more important. Faster running and smaller executing code. Again, I'm glad I could help, thanks for the points. Have a great day

+ 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. Nested Loops
    By christian2012 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 03-10-2013, 08:19 PM
  2. Nested loops VBA
    By zurich in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-06-2012, 03:21 PM
  3. Nested Do Loops
    By ross88guy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-08-2010, 09:10 AM
  4. VBA - Nested loops
    By roheba in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-09-2010, 01:42 PM
  5. Nested Loops
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-11-2009, 04:14 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