Solved

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

Posted on 2014-12-19
4
238 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 47

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 47

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 47

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

735 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