SOLVED

How do I merge 2 columns

%3CLINGO-SUB%20id%3D%22lingo-sub-2725499%22%20slang%3D%22en-US%22%3EHow%20do%20I%20merge%202%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2725499%22%20slang%3D%22en-US%22%3E%3CP%3EOk%20so%20basically%20its%20a%20bit%20more%20complicated%20than%20that%2C%20I%20want%20to%20merge%20a%20phone%20number%20and%20home%20phone%20number%20into%20one%20column%20%2C%20but%20I%20want%20the%20home%20phone%20number%20to%20go%20to%20the%20next%20line%20in%20the%20merged%20column%20which%20also%20needs%20a%20foreign%20key%20to%20be%20repeated%20if%20the%20employee%20has%202%20numbers%20entered.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2725499%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EBI%20%26amp%3B%20Data%20Analysis%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EExcel%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EFormulas%20and%20Functions%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20Scripts%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2725599%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20merge%202%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2725599%22%20slang%3D%22en-US%22%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20didn't%20understand%20the%20ID%23%3C%2FP%3E%3CP%3EYou%20can%20use%20the%20following%20formula%20and%20then%20click%20on%20%22Wrap%20Text%22%20option%3A%3DTEXTJOIN(CHAR(10)%3B%3BB2%3AC2)%26nbsp%3B%3C%2FP%3E%3CDIV%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%26nbsp%3B%3CDIV%20class%3D%22mceNonEditable%20lia-copypaste-placeholder%22%3E%3CIMG%20border%3D%220%22%20%2F%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FDIV%3E%3C%2FDIV%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-excel-formula%22%3E%3CCODE%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1148076%22%20target%3D%22_blank%22%3E%40Dirgh690%3C%2FA%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2725639%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20merge%202%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2725639%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1148076%22%20target%3D%22_blank%22%3E%40Dirgh690%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3ERun%20the%20following%20macro%3A%3C%2FP%3E%0A%3CPRE%20class%3D%22lia-code-sample%20language-visual-basic%22%3E%3CCODE%3ESub%20MergePhone()%0A%20%20%20%20Dim%20rng%20As%20Range%0A%20%20%20%20Application.ScreenUpdating%20%3D%20False%0A%20%20%20%20Set%20rng%20%3D%20Range(%22C%3AC%22).Find(What%3A%3D%22*%22%2C%20SearchDirection%3A%3DxlPrevious)%0A%20%20%20%20Do%0A%20%20%20%20%20%20%20%20rng.Offset(1).EntireRow.Insert%0A%20%20%20%20%20%20%20%20rng.Offset(1%2C%20-2).Value%20%3D%20rng.Offset(0%2C%20-2).Value%0A%20%20%20%20%20%20%20%20rng.Offset(1%2C%20-1).Value%20%3D%20%22'%22%20%26amp%3B%20rng.Value%0A%20%20%20%20%20%20%20%20Set%20rng%20%3D%20Range(%22C%3AC%22).Find(What%3A%3D%22*%22%2C%20After%3A%3Drng%2C%20SearchDirection%3A%3DxlPrevious)%0A%20%20%20%20%20%20%20%20If%20rng.Row%20%3D%201%20Then%20Exit%20Do%0A%20%20%20%20Loop%0A%20%20%20%20Range(%22C%3AC%22).ClearContents%0A%20%20%20%20Application.ScreenUpdating%20%3D%20True%0AEnd%20Sub%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2725652%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20merge%202%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2725652%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1148076%22%20target%3D%22_blank%22%3E%40Dirgh690%3C%2FA%3E%26nbsp%3BYou%20don't%20specify%20what%20%22a%20foreign%20key%22%20entails.%20But%20perhaps%20you%20mean%20that%20%2204%22%20should%20be%20in%20the%20beginning%20of%20every%20phone%20number.%20If%20so%2C%20try%20this%20in%20D2%2C%20set%20the%20cell%20format%20to%20wrap%20text%20and%20copy%20it%20down%3A%3C%2FP%3E%3CP%3E%3CSTRONG%3E%3DTEXT(B2%2C%220000000000%22)%26amp%3BCHAR(10)%26amp%3BIF(ISBLANK(C2)%2C%22%22%2CTEXT(LEFT(B2%2C1)%2C%2200%22))%26amp%3BC2%3C%2FSTRONG%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20image-alt%3D%22Screenshot%202021-09-06%20at%2019.10.59.png%22%20style%3D%22width%3A%20299px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F308354iB792A3E6687A4657%2Fimage-size%2Flarge%3Fv%3Dv2%26amp%3Bpx%3D999%22%20role%3D%22button%22%20title%3D%22Screenshot%202021-09-06%20at%2019.10.59.png%22%20alt%3D%22Screenshot%202021-09-06%20at%2019.10.59.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2725696%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20merge%202%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2725696%22%20slang%3D%22en-US%22%3EThank%20You%20Soo%20Much!!%20This%20is%20exactly%20what%20I%20needed!!%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2728072%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20merge%202%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2728072%22%20slang%3D%22en-US%22%3EHey%20Hans%2C%20If%20I%20want%20to%20merge%206%20columns%20instead%20of%20just%202%20what%20would%20i%20need%20to%20change%3F%3F%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2728135%22%20slang%3D%22en-US%22%3ERe%3A%20How%20do%20I%20merge%202%20columns%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2728135%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1148076%22%20target%3D%22_blank%22%3E%40Deerg65%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EYou've%20never%20said%20%22Why%22%20you%20are%20doing%20these%20merges.%20In%20general%2C%20merging%20data%20like%20that%20is%20something%20people%20regret%20later%2C%20since%20Phone%23%20becomes%20ambiguous%20once%20done....whereas%20%22Cell%22%20and%20%22Home%22%20are%20clear.%20I%20can't%20imagine%20why%20you'd%20want%20to%20merge%20six%2C%20unless%20you're%20talking%20the%20whole%20home%20address%2C%20and%20there%20too%20I'd%20want%20to%20ask%2C%20%22Why%3F!!%22%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIf%20you're%20doing%20this%20so%20as%20to%20create%20a%20printout%20of%20each%20person's%20contact%20info%2C%20or%20a%20nice%20address%20list%2C%20you'd%20be%20far%20better%20served%2C%20over%20the%20long%20term%2C%20by%20keeping%20each%20data%20element%20intact%20as%20separate%20cells%20and%20using%20MailMerge%20in%20Word%20(with%20the%20Excel%20database%20as%20the%20source)%20to%20create%20the%20printouts.%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

Ok so basically its a bit more complicated than that, I want to merge a phone number and home phone number into one column , but I want the home phone number to go to the next line in the merged column which also needs a foreign key to be repeated if the employee has 2 numbers entered. 

 

Edit: I Managed to do it with a little help

7 Replies

@Deerg65

 

I didn't understand the ID#

You can use the following formula and then click on "Wrap Text" option:

=TEXTJOIN(CHAR(10),,B2:C2) 

 

 

best response confirmed by Deerg65 (Occasional Contributor)
Solution

@Deerg65 

Run the following macro:

Sub MergePhone()
    Dim rng As Range
    Application.ScreenUpdating = False
    Set rng = Range("C:C").Find(What:="*", SearchDirection:=xlPrevious)
    Do
        rng.Offset(1).EntireRow.Insert
        rng.Offset(1, -2).Value = rng.Offset(0, -2).Value
        rng.Offset(1, -1).Value = "'" & rng.Value
        Set rng = Range("C:C").Find(What:="*", After:=rng, SearchDirection:=xlPrevious)
        If rng.Row = 1 Then Exit Do
    Loop
    Range("C:C").ClearContents
    Application.ScreenUpdating = True
End Sub

@Deerg65 You don't specify what "a foreign key" entails. But perhaps you mean that "04" should be in the beginning of every phone number. If so, try this in D2, set the cell format to wrap text and copy it down:

=TEXT(B2,"0000000000")&CHAR(10)&IF(ISBLANK(C2),"",TEXT(LEFT(B2,1),"00"))&C2

 

Screenshot 2021-09-06 at 19.10.59.png

 

Thank You Soo Much!! This is exactly what I needed!!
Hey Hans, If I want to merge 6 columns instead of just 2 what would i need to change??

@Deerg65 

 

You've never said "Why" you are doing these merges. In general, merging data like that is something people regret later, since Phone# becomes ambiguous once done....whereas "Cell" and "Home" are clear. I can't imagine why you'd want to merge six, unless you're talking the whole home address, and there too I'd want to ask, "Why?!!"

 

If you're doing this so as to create a printout of each person's contact info, or a nice address list, you'd be far better served, over the long term, by keeping each data element intact as separate cells and using MailMerge in Word (with the Excel database as the source) to create the printouts.

@Deerg65 

See your new discussion How can I merge 6 columns?? 

I posted a macro there.