+ Reply to Thread
Results 1 to 7 of 7

Recursive loop through the rows and columns

Hybrid View

maximusu Recursive loop through the... 10-11-2018, 04:34 PM
¯\\_(ツ)_/¯ Re: Recursive loop through... 10-11-2018, 05:09 PM
maximusu Re: Recursive loop through... 10-11-2018, 05:18 PM
MarvinP Re: Recursive loop through... 10-11-2018, 05:20 PM
maximusu Re: Recursive loop through... 10-11-2018, 05:26 PM
MarvinP Re: Recursive loop through... 10-11-2018, 05:34 PM
maximusu Re: Recursive loop through... 10-11-2018, 05:44 PM
  1. #1
    Registered User
    Join Date
    09-22-2018
    Location
    russia
    MS-Off Ver
    2010
    Posts
    4

    Recursive loop through the rows and columns

    Hello everybody.
    Guys help me figure out the task please
    I have the following problem.

    I need to combine values from all columns and rows.
    At the beginning, we take the first value of the first column and append the first value of the second column to it
    Then we add all the values of the third column to the result.
    Then we take first value of the first column, append the second value of the second column and append all values from 3-rd column again.
    And so on.

    The example clearly shows what is required.

    The number of columns is variable
    The number of lines is different everywhere.

    I understand that this can be solved by a recursive function. I tried to write it, but I did not succeed at all. Full trash..
    Please help
    Attached Files Attached Files
    Last edited by maximusu; 10-11-2018 at 04:36 PM.

  2. #2
    Forum Contributor
    Join Date
    09-19-2018
    Location
    USA
    MS-Off Ver
    2016
    Posts
    208

    Re: Recursive loop through the rows and columns

    Your "results" don't seem to match up to what you're asking for. They appear duplicated, e.g.

    A1-B1-C1
    A1-B1-C2
    A1-B1-C3
    A1-B1-C4
    A1-B1-C1
    A1-B1-C2
    A1-B1-C3
    A1-B1-C4


    then comes
    A1-B2-C1
    A1-B2-C2
    A1-B2-C3
    A1-B2-C4
    A1-B2-C1
    A1-B2-C2
    A1-B2-C3
    A1-B2-C4
    ...

  3. #3
    Registered User
    Join Date
    09-22-2018
    Location
    russia
    MS-Off Ver
    2010
    Posts
    4

    Re: Recursive loop through the rows and columns

    Oh, sorry. It's a handmade result
    Attached Files Attached Files

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,229

    Re: Recursive loop through the rows and columns

    Hi maximusa and welcome to the forum,

    Here is the code that will work on your 3 column problem. This should give you a head start on doing one with an unknown number of columns.

    Sub maximusa()
        Dim LastCRow As Double
        Dim LastBRow As Double
        Dim LastARow As Double
        Dim RowACtr As Double
        Dim RowBCtr As Double
        Dim RowCCtr As Double
        Dim LastERow As Double
    
        LastARow = Cells(Rows.Count, "A").End(xlUp).Row
        LastBRow = Cells(Rows.Count, "B").End(xlUp).Row
        LastCRow = Cells(Rows.Count, "C").End(xlUp).Row
    
        Columns("E:E").ClearContents
        Cells(1, "E") = "Results"
    
        For RowACtr = 1 To LastARow
            For RowBCtr = 1 To LastBRow
                For RowCCtr = 1 To LastCRow
                    LastERow = Cells(Rows.Count, "E").End(xlUp).Row
                        Cells(LastERow + 1, "E") = Cells(RowACtr, "A") & "-" & _
                        Cells(RowBCtr, "B") & "-" & Cells(RowCCtr, "C")
                Next RowCCtr
            Next RowBCtr
        Next RowACtr
    
    End Sub

    VBA recursive loop for Maximusa.xlsm
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Registered User
    Join Date
    09-22-2018
    Location
    russia
    MS-Off Ver
    2010
    Posts
    4

    Re: Recursive loop through the rows and columns

    Thanks, MarvinP

    But this is an easy way, but, as usual, everyone wants a beautiful solution.
    Like recursion
    In extreme cases, I have to use inner loops.

  6. #6
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,229

    Re: Recursive loop through the rows and columns

    Hey miximusu

    Before you can solve harder problems, like recursion, you should see how to do it the easy way. Then take the defined variables and make them variable. This will lead you to an answer. How many columns might you have in your problem? Did my answer work for your test problem?

    Instead of Columns A,B,C how would you make a variable number of columns? Perhaps using Cells(1,Columns.Count).End(xlToLeft).Column ? Are you getting an idea from my example? Instead of having a fixed number of For..Next Loops you might use a For..Each type of Loop.

    Does this get you closer to what you want?

  7. #7
    Registered User
    Join Date
    09-22-2018
    Location
    russia
    MS-Off Ver
    2010
    Posts
    4

    Re: Recursive loop through the rows and columns

    Yes... but i found another case.
    Look at this. No bad.

    Any way thanks a lot, MarvinP
    Attached Files Attached Files

+ 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 Loop and recursive Dir(): How to separate the first match from all other matches in all
    By greenythebeast in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-07-2016, 03:14 PM
  2. Loop Across Columns...If Blank Cell, Loop Down Rows
    By flindy87 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-25-2014, 07:05 PM
  3. Loop Across Columns If Blank Cell Loop Down Rows
    By flindy87 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-05-2014, 10:22 AM
  4. Deal With Recursive (Endless Loop)
    By Oxco in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-30-2013, 08:14 PM
  5. Replies: 1
    Last Post: 11-08-2012, 04:53 PM
  6. Calling a recursive Sub in a 'for' loop in another Sub
    By excelworker_1 in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 06-15-2012, 10:57 AM
  7. Recursive Loop -- How to Stop It?
    By LarryP in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-28-2006, 03:50 PM

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