how to do this in excel

banme

New Member
Joined
Sep 23, 2016
Messages
33
i have sheet with data . i want to combine column A values with Column B values with this condition ..

IF sheet one have these values in Column A and Column B

HTML:
|----------------------------|--------------------------|
|                Cell A                |        Cell B                     |
|----------------------------|--------------------------|
|             Name 1                |         Last Name 1           |
|----------------------------|--------------------------|
|             Name 2                |         Last Name 2           |
|----------------------------|--------------------------|
|             Name 3                |         Last Name 3           |
|----------------------------|--------------------------|
|             Name 4                |         Last Name 4           |
|----------------------------|--------------------------|
|             Name 5                |         Last Name 5           |
|----------------------------|--------------------------|

then need values in sheet 2 like this

HTML:
|----------------------------------------|
|                Cell A                                 |   
|----------------------------------------|
|             Name 1 -  Last Name 1            |
|----------------------------------------|
|             Name 1 -  Last Name 2            |
|----------------------------------------|
|             Name 1 -  Last Name 3            |
|----------------------------------------|
|             Name 1 -  Last Name 4            |
|----------------------------------------|
|             Name 1 -  Last Name 5            |
|----------------------------------------|

|----------------------------------------|
|                Cell B                                 |   
|----------------------------------------|
|             Name 2 -  Last Name 1            |
|----------------------------------------|
|             Name 2 -  Last Name 2            |
|----------------------------------------|
|             Name 2 -  Last Name 3            |
|----------------------------------------|
|             Name 2 -  Last Name 4            |
|----------------------------------------|
|             Name 2 -  Last Name 5            |
|----------------------------------------|

|----------------------------------------|
|                Cell C                                 |   
|----------------------------------------|
|             Name 3 -  Last Name 1            |
|----------------------------------------|
|             Name 3 -  Last Name 2            |
|----------------------------------------|
|             Name 3 -  Last Name 3            |
|----------------------------------------|
|             Name 3 -  Last Name 4            |
|----------------------------------------|
|             Name 3 -  Last Name 5            |
|----------------------------------------| and continue TO SAME NAME 5

i tried to find some other ways to do THIS . but not able to find any online tool to do this so i think excel is only option for this to do this . so can you please help me in this .
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
For Cell A, in sheet 2, use this formula and drag it down;

=Sheet1!$A$1&"-"&Sheet1!B1


For Cell B, in sheet 2, use this formula and drag it down;

=Sheet1!$A$2&"-"&Sheet1!B1


For Cell C, in sheet 2, use this formula and drag it down;

=Sheet1!$A$3&"-"&Sheet1!B1


and so on.....

Here, operator "&" is used to concatenate or combine values of Column A, Dash(-), and ColumnB
 
Last edited:
Upvote 0
Hi banme,


PFB for the required macro for your query.


Code:
Sub ColumnMerger()


'
'
'Code by Ramandeep Singh
'
'


'Declaring variables
Dim LastRowColumnA, LastRowColumnB, i, j As Long
Dim RangeA, RangeB As Range
Dim ColumnAValue, ColumnBValue As Variant


With Sheets("Sheet1")
    
'Getting the last row number for column A and B
LastRowColumnA = .Range("A1").End(xlDown).Row
LastRowColumnB = .Range("B1").End(xlDown).Row
    
'Checking whether no record or single record exists in column A
If LastRowColumnA = 1048576 Then
MsgBox "Macro cannot run as Column A contain no record or single record"
Exit Sub
End If
    
'Checking whether no record or single record exists in column B
If LastRowColumnB = 1048576 Then
MsgBox "Macro cannot run as Column B contain no record or single record"
Exit Sub
End If
    
'Defining the range of column A and B
Set RangeA = .Range(.Range("A1"), .Range("A1").End(xlDown))
Set RangeB = .Range(.Range("B1"), .Range("B1").End(xlDown))


End With


'Inserting new sheet as the last sheet
Sheets.Add after:=Sheets(Sheets.Count)


i = 1


'Looping through all the values in column A
For Each ColumnAValue In RangeA
j = 1
'Looping through all the values in column B
For Each ColumnBValue In RangeB
        
'Generating the required string
Cells(j, i) = ColumnAValue & " - " & ColumnBValue
j = j + 1
Next
i = i + 1
Next


'Auto adjusting the size of the columns
Columns.AutoFit


End Sub

Regards
Ramandeep Singh
 
Upvote 0

Forum statistics

Threads
1,214,648
Messages
6,120,726
Members
448,987
Latest member
marion_davis

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top