In Excel 2007, how to write VBA code to loop through rows to create names dynamically

I am creating names of tables and each table is named based on a hierarchy.  The following example are made up names, but will serve the purpose of my example. As I hope you can tell, I am taking the first word from the first column and creating every possible variation from the words of following columns.  The first column (Col A) is the top hierarchy where it becomes the first part of the name, the second column (Col B) is the next highest becoming the second part of the name, and so on. What can be written via VBA to loop through each word of each column to create a new name?  The "Results" column is a sample of what I want to achieve.

Col A      Col B      Col C      Col D            Results
ABC      CAT      RED      ONE            ABC_CAT_RED_ONE
DEF      DOG      ORG      TWO            ABC_CAT_RED_TWO
GHI      PIG      YLW      THR            ABC_CAT_RED_THR
JKL      BAT      GRN      FOR            ABC_CAT_RED_FOR
MNO      BOY      BLU      FIV            ABC_CAT_RED_FIV
      MAN      PUR      SIX            ABC_CAT_RED_SIX
      BUG      BLK      SEV            ABC_CAT_RED_SEV
            WHT      EGT            ABC_CAT_RED_EGT
            BRW      NIN            ABC_CAT_RED_NIN
            PNK      TEN            ABC_CAT_RED_TEN
            LAV                  ABC_CAT_ORG_ONE
            BEI                  ABC_CAT_ORG_TWO
                              ABC_CAT_ORG_THR
                              ABC_CAT_ORG_FOR
                              ABC_CAT_ORG_FIV
                              ABC_CAT_ORG_SIX
                              ABC_CAT_ORG_SEV
                              ABC_CAT_ORG_EGT
                              ABC_CAT_ORG_NIN
                              ABC_CAT_ORG_TEN
                              etc.Example-of-Hierarchy-Naming.xlsxI am creating names of tables and each table is named based on a hierarchy.  The following example are made up names, but will serve the purpose of my example. As I hope you can tell, I am taking the first word from the first column and creating every possible variation from the words of following columns.  The first column (Col A) is the top hierarchy where it becomes the first part of the name, the second column (Col B) is the next highest becoming the second part of the name, and so on. What can be written via VBA to loop through each word of each column to create a new name?  The "Results" column is a sample of what I want to achieve.

Col A      Col B      Col C      Col D            Results
ABC      CAT      RED      ONE            ABC_CAT_RED_ONE
DEF      DOG      ORG      TWO            ABC_CAT_RED_TWO
GHI      PIG      YLW      THR            ABC_CAT_RED_THR
JKL      BAT      GRN      FOR            ABC_CAT_RED_FOR
MNO      BOY      BLU      FIV            ABC_CAT_RED_FIV
      MAN      PUR      SIX            ABC_CAT_RED_SIX
      BUG      BLK      SEV            ABC_CAT_RED_SEV
            WHT      EGT            ABC_CAT_RED_EGT
            BRW      NIN            ABC_CAT_RED_NIN
            PNK      TEN            ABC_CAT_RED_TEN
            LAV                  ABC_CAT_ORG_ONE
            BEI                  ABC_CAT_ORG_TWO
                              ABC_CAT_ORG_THR
                              ABC_CAT_ORG_FOR
                              ABC_CAT_ORG_FIV
                              ABC_CAT_ORG_SIX
                              ABC_CAT_ORG_SEV
                              ABC_CAT_ORG_EGT
                              ABC_CAT_ORG_NIN
                              ABC_CAT_ORG_TEN
                              etc.
kristibigoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Sub Hierarchy()
Dim lngLastRowA As Long
Dim lngLastRowB As Long
Dim lngLastRowC As Long
Dim lngLastRowD As Long
Dim lngRowA As Long
Dim lngRowB As Long
Dim lngRowC As Long
Dim lngRowD As Long
Dim lngNextRow As Long

lngLastRowA = Range("A1048576").End(xlUp).Row
lngLastRowB = Range("B1048576").End(xlUp).Row
lngLastRowC = Range("C1048576").End(xlUp).Row
lngLastRowD = Range("D1048576").End(xlUp).Row

For lngRowA = 1 To lngLastRowA
    For lngRowB = 1 To lngLastRowB
        For lngRowC = 1 To lngLastRowC
            For lngRowD = 1 To lngLastRowD
                lngNextRow = lngNextRow + 1
                Cells(lngNextRow, 6) = Cells(lngRowA, 1) & "_" & Cells(lngRowB, 2) & "_" & Cells(lngRowC, 3) & "_" & Cells(lngRowD, 4)
            Next
        Next
    Next
Next
End Sub

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kristibigoAuthor Commented:
Beautiful! Thank you!
0
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2014
0
Martin LissOlder than dirtCommented:
OK I see what you are talking about. To me however it seems strange to select the record by way of the person's initials. What would you do if there were a Karen Angela Babet and also a Karl Andrew Brown?

Would you be open to a different approach?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Office Productivity

From novice to tech pro — start learning today.