+ Reply to Thread
Results 1 to 6 of 6

Combining 2 columns in different spreadsheets and creating all combinations

Hybrid View

  1. #1
    Registered User
    Join Date
    09-09-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    3

    Combining 2 columns in different spreadsheets and creating all combinations

    I have a bit of a dilemma. So i have 2 spreadsheet and i created temporary sheets as the ones I am designing contain sensitive information. I've been a bit out of the loop when it comes to vba so here i am seeking help


    So i created a spreadsheet which contains a list of all presidents and i also created a 2nd spreadsheet which contains all states. As an example we have

    PRESIDENTS
    Barack Obama
    George Bush
    Bill Clinton
    George Bush
    Ronald Reagan

    Next we have states
    Alabama
    Texas
    Oklahoma
    Utah

    What the results that I am looking for is combining the sheets or creating some code by which i could produce the following results.

    AlabamaBarack Obama
    AlabamaGeorge Bush
    AlabamaBill Clinton
    AlabamaGeorge Bush
    AlabamaRonald Reagan
    TexasBarack Obama
    TexasGeorge Bush
    TexasBill Clinton
    TexasGeorge Bush
    TexsaRonald Reagan
    ETC
    ETC
    ETC

    Please help as I am stuck
    Attached Files Attached Files

  2. #2
    Valued Forum Contributor wenqq3's Avatar
    Join Date
    04-01-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    868

    Re: Combining 2 columns in different spreadsheets and creating all combinations

    Put the "STATES" and "PRESIDENTS" in same workbook, then it will create a new sheets call "RESULT"

    Sub RepeatName()
    
    Dim state_name, president_name As String
    Dim state_lstCol, president_lstCol As Long
    Application.ScreenUpdating = False
    
    Sheets("STATES").Select
    state_lstCol = Range("A" & Rows.count).End(xlUp).Row
    Sheets("PRESIDENTS").Select
    president_lstCol = Range("A" & Rows.count).End(xlUp).Row
    
    Sheets.Add
    ActiveSheet.Name = "RESULT"
    For i = 2 To state_lstCol
        Sheets("STATES").Select
        state_name = Range("A" & i).Value
        
            For j = 2 To president_lstCol
                Sheets("PRESIDENTS").Select
                president_name = Range("B" & j).Value
                
                Sheets("RESULT").Select
                ActiveCell.Formula = state_name & president_name
                ActiveCell.Offset(1, 0).Select
            Next j
    Next i
    Application.ScreenUpdating = True
    End Sub
    -If the problem is solved, please mark your thread as Solved: Click Thread Tools above your first post, select "Mark your thread as Solved".

    -Always upload a workbook before start your question
    To attach a file, push the button with the paperclip (or scroll down to the Manage Attachments button), browse to the required file, and then push the Upload button.

    +++ If my answer(s) helped you, please add me reputation by click on * +++

  3. #3
    Registered User
    Join Date
    09-09-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Combining 2 columns in different spreadsheets and creating all combinations

    What if i wanted to not combine them but have them in separate columns?

    So it would be

    ALABAMA BARACK
    ALABAMA BUSH
    ALABAMA CLINTON

    etc etc etc

    Once again thank you for your assitance.

  4. #4
    Valued Forum Contributor wenqq3's Avatar
    Join Date
    04-01-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    868

    Re: Combining 2 columns in different spreadsheets and creating all combinations

    =INDEX(STATES!$A$2:$A$57,ROUNDUP(ROW(A1)/(COUNTA(PRESIDENTS!B:B)-1),0))&INDEX(PRESIDENTS!$B$2:$B$44,MOD(ROW(A1)+ROUNDUP(ROW(A1)/(COUNTA(PRESIDENTS!B:B)-1),0)-1,COUNTA(PRESIDENTS!B:B)))
    If you want use function, here you are.
    Need put "STATES" and "PRESIDENTS" in same workbook.

  5. #5
    Registered User
    Join Date
    09-09-2013
    Location
    United States
    MS-Off Ver
    Excel 2013
    Posts
    3

    Re: Combining 2 columns in different spreadsheets and creating all combinations

    Is there a way that if i hit the row limit in excel 2010 to continue adding them into a new worksheet?

  6. #6
    Valued Forum Contributor wenqq3's Avatar
    Join Date
    04-01-2013
    Location
    Malaysia
    MS-Off Ver
    Excel 2003
    Posts
    868

    Re: Combining 2 columns in different spreadsheets and creating all combinations

    Is there a way that if i hit the row limit in excel 2010 to continue adding them into a new worksheet?
    Excel 2010 Worksheet size 1,048,576 rows by 16,384 columns

    Sub RepeatName()
    
    Dim state_name, president_name As String
    Dim state_lstCol, president_lstCol As Long
    Dim i, j, k As Integer
    k = 1
    Application.ScreenUpdating = False
    
    Sheets("STATES").Select
    state_lstCol = Range("A" & Rows.count).End(xlUp).Row
    Sheets("PRESIDENTS").Select
    president_lstCol = Range("B" & Rows.count).End(xlUp).Row
    
    Sheets.Add
    ActiveSheet.Name = "RESULT" & k
    For i = 2 To state_lstCol
        Sheets("STATES").Select
        state_name = Range("A" & i).Value
        
            For j = 2 To president_lstCol
                Sheets("PRESIDENTS").Select
                president_name = Range("B" & j).Value
                
                Sheets("RESULT" & k).Select
                ActiveCell.Formula = state_name
                ActiveCell.Offset(0, 1).Formula = president_name
                ActiveCell.Offset(1, 0).Select
                If ActiveCell.Row = 1048576 Then
                        Sheets.Add
                        k = k + 1
                        ActiveSheet.Name = "RESULT" & k
                End If
            Next j
    Next i
    Application.ScreenUpdating = True
    End Sub
    What if i wanted to not combine them but have them in separate columns?
     ActiveCell.Formula = state_name
     ActiveCell.Offset(0, 1).Formula = president_name

+ 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. [SOLVED] Creating formula for combining several columns
    By lmm in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-26-2013, 08:33 PM
  2. [SOLVED] combining numbers into combinations
    By David in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 02:05 AM
  3. combining numbers into combinations
    By David in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-06-2005, 01:05 AM
  4. combining numbers into combinations
    By David in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-05-2005, 10:05 PM
  5. [SOLVED] combining numbers into combinations
    By David in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 05-29-2005, 11:05 PM

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