Solved

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

Posted on 2014-12-19
4
234 Views
Last Modified: 2014-12-19
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.
0
Comment
Question by:kristibigo
  • 3
4 Comments
 
LVL 46

Accepted Solution

by:
Martin Liss earned 500 total points
ID: 40509653
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
 

Author Closing Comment

by:kristibigo
ID: 40509837
Beautiful! Thank you!
0
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40509946
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
 
LVL 46

Expert Comment

by:Martin Liss
ID: 40509967
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
I recently resolved a client's Office 2013 installation problem and wanted to offer an observation that may help you with troubleshooting similar issues. The client ordered three Dell Optiplex system units with the Windows 7 downgrade option inst…
An overview on how to enroll an hourly employee into the employee database and how to give them access into the clock in terminal.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now