Link to home
Start Free TrialLog in
Avatar of kristibigo
kristibigoFlag for United States of America

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Martin Liss
Martin Liss
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kristibigo

ASKER

Beautiful! Thank you!
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
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?