merge data from multiple columns to single - vba help

nt_beans

New Member
Joined
Nov 10, 2015
Messages
26
hello,
I ve a spreadsheet in following format and looking to merge columns and make it to single.

Current:
UserLocationVendorCBOTCMENYMEXCOMEX
Brian HoyerNew YorkCQG11
Sydney ChenLos AngelesCQG111
Ram NivasDallasCQG1

<tbody>
</tbody>

Desire Output

if cbot or cme or nymex or comex has '1' then i want data in following format:

UserLocationExchangeVendor
Brian HoyerNew YorkCBOTCQG
Brian HoyerNew YorkNYMEXCQG
Sydney ChenLos AngelesCMECQG
Sydney ChenLos AngelesNYMEXCQG
Sydney ChenLos AngelesCOMEXCQG
Ramn NivasDallasCBOTCQG

<tbody>
</tbody>

Thanks in advance for your help
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
This assumes you start in A1 on Sheet1, modify to suit.

Code:
Sub summarise()

Dim rownum As Long
Dim rownum2 As Long


Application.ScreenUpdating = False


On Error Resume Next
With ThisWorkbook
    .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Summary"
End With


Sheets("Summary").Cells(1, 1).Value = "User"
Sheets("Summary").Cells(1, 2).Value = "Location"
Sheets("Summary").Cells(1, 3).Value = "Vendor"
Sheets("Summary").Cells(1, 4).Value = "Exchange"


rownum = 2
rownum2 = 2


Do Until Sheets("Sheet1").Cells(rownum, 1).Value = ""
If Sheets("Sheet1").Cells(rownum, 4).Value = 1 Then
Sheets("Sheet1").Rows(rownum).Copy Sheets("Summary").Rows(rownum2)
Sheets("Summary").Cells(rownum2, 4).Value = "CBOT"
rownum2 = rownum2 + 1
End If
If Sheets("Sheet1").Cells(rownum, 5).Value = 1 Then
Sheets("Sheet1").Rows(rownum).Copy Sheets("Summary").Rows(rownum2)
Sheets("Summary").Cells(rownum2, 4).Value = "CME"
rownum2 = rownum2 + 1
End If
If Sheets("Sheet1").Cells(rownum, 6).Value = 1 Then
Sheets("Sheet1").Rows(rownum).Copy Sheets("Summary").Rows(rownum2)
Sheets("Summary").Cells(rownum2, 4).Value = "NYMEX"
rownum2 = rownum2 + 1
End If
If Sheets("Sheet1").Cells(rownum, 7).Value = 1 Then
Sheets("Sheet1").Rows(rownum).Copy Sheets("Summary").Rows(rownum2)
Sheets("Summary").Cells(rownum2, 4).Value = "COMEX"
rownum2 = rownum2 + 1
End If
rownum = rownum + 1
Loop
Sheets("Summary").Columns("E:G").ClearContents
Sheets("Summary").Columns("C").Insert
Sheets("Summary").Columns("E").Cut Sheets("Summary").Columns("C")
Sheets("Summary").Cells.AutoFit


Application.ScreenUpdating = True


End Sub
 
Upvote 0
This assumes you start in A1 on Sheet1, modify to suit.

Code:
Sub summarise()

Dim rownum As Long
Dim rownum2 As Long


Application.ScreenUpdating = False


On Error Resume Next
With ThisWorkbook
    .Sheets.Add(After:=.Sheets(.Sheets.Count)).Name = "Summary"
End With


Sheets("Summary").Cells(1, 1).Value = "User"
Sheets("Summary").Cells(1, 2).Value = "Location"
Sheets("Summary").Cells(1, 3).Value = "Vendor"
Sheets("Summary").Cells(1, 4).Value = "Exchange"


rownum = 2
rownum2 = 2


Do Until Sheets("Sheet1").Cells(rownum, 1).Value = ""
If Sheets("Sheet1").Cells(rownum, 4).Value = 1 Then
Sheets("Sheet1").Rows(rownum).Copy Sheets("Summary").Rows(rownum2)
Sheets("Summary").Cells(rownum2, 4).Value = "CBOT"
rownum2 = rownum2 + 1
End If
If Sheets("Sheet1").Cells(rownum, 5).Value = 1 Then
Sheets("Sheet1").Rows(rownum).Copy Sheets("Summary").Rows(rownum2)
Sheets("Summary").Cells(rownum2, 4).Value = "CME"
rownum2 = rownum2 + 1
End If
If Sheets("Sheet1").Cells(rownum, 6).Value = 1 Then
Sheets("Sheet1").Rows(rownum).Copy Sheets("Summary").Rows(rownum2)
Sheets("Summary").Cells(rownum2, 4).Value = "NYMEX"
rownum2 = rownum2 + 1
End If
If Sheets("Sheet1").Cells(rownum, 7).Value = 1 Then
Sheets("Sheet1").Rows(rownum).Copy Sheets("Summary").Rows(rownum2)
Sheets("Summary").Cells(rownum2, 4).Value = "COMEX"
rownum2 = rownum2 + 1
End If
rownum = rownum + 1
Loop
Sheets("Summary").Columns("E:G").ClearContents
Sheets("Summary").Columns("C").Insert
Sheets("Summary").Columns("E").Cut Sheets("Summary").Columns("C")
Sheets("Summary").Cells.AutoFit


Application.ScreenUpdating = True


End Sub

This works great..thanks for your help
 
Upvote 0

Forum statistics

Threads
1,214,617
Messages
6,120,541
Members
448,970
Latest member
kennimack

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