Condense multiples into first occurrence only

nguerra

New Member
Joined
Oct 1, 2013
Messages
46
Is there a way to take the first occurrence in every value change in a column and re-write it with another value in the same row? For example:

Column 1Column 2Column 6Column 7
appleaapplea
appleapeara
appleaorangeb
pearagrapea
pearabananab
orangebappleb
grapeaorangea
grapea
bananab
appleb
appleb
orangea

<colgroup><col style="width:54pt" span="6" width="72"> </colgroup><tbody>
</tbody>
 
Thank you Zax, Seems to be the same problem that Hiker95 had. I'm pasting the out put below(section of real data). A&B are the original, F&G are your output, and J&K is whats needed.

PartSub PartPartSub PartPartSub Part
NS1050F1013PATNS1050F1013PATNS1050F1013PAT
NS11086B510QNS11086B510QNS11086B510Q
NS11086B510QNS11086B510DFCNS1134F1169DFC
NS11086B510QNS11086B510QNS1144F0848DFC
NS11086B510DFCNS11086B510PATNS1145F0950Q
NS11086B510QNS1134F1169DFCNS1146F0931DFC
NS11086B510QNS1134F1169PATNS1203F0196PAT
NS11086B510PATNS1144F0848DFCNS12076A583Q
NS1134F1169DFCNS1144F0848PAT
NS1134F1169PATNS1145F0950Q
NS1144F0848DFCNS1145F0950DFC
NS1144F0848PATNS1145F0950PAT
NS1145F0950QNS1146F0931DFC
NS1145F0950DFCNS1146F0931Q
NS1145F0950PATNS1146F0931DFC
NS1146F0931DFCNS1146F0931PAT
NS1146F0931QNS1203F0196PAT
NS1146F0931DFCNS12076A583Q
NS1146F0931PATNS12076A583DFC
NS1203F0196PATNS12076A583Q
NS12076A583QNS12076A583PAT
NS12076A583DFCNS1233F0408MC

<colgroup><col><col><col span="3"><col><col><col span="2"><col><col></colgroup><tbody>
</tbody>
 
Upvote 0

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Sorry Zax, I posted your results, trying everyone's but I'm still getting duplicates from everyone.
 
Upvote 0
Thank you Zax, Seems to be the same problem that Hiker95 had. I'm pasting the out put below(section of real data). A&B are the original, F&G are your output, and J&K is whats needed.

PartSub PartPartSub PartPartSub Part
NS1050F1013PATNS1050F1013PATNS1050F1013PAT
NS11086B510QNS11086B510QNS11086B510Q
NS11086B510QNS11086B510DFCNS1134F1169DFC
NS11086B510QNS11086B510QNS1144F0848DFC
NS11086B510DFCNS11086B510PATNS1145F0950Q
NS11086B510QNS1134F1169DFCNS1146F0931DFC
NS11086B510QNS1134F1169PATNS1203F0196PAT
NS11086B510PATNS1144F0848DFCNS12076A583Q
NS1134F1169DFCNS1144F0848PAT
NS1134F1169PATNS1145F0950Q
NS1144F0848DFCNS1145F0950DFC
NS1144F0848PATNS1145F0950PAT
NS1145F0950QNS1146F0931DFC
NS1145F0950DFCNS1146F0931Q
NS1145F0950PATNS1146F0931DFC
NS1146F0931DFCNS1146F0931PAT
NS1146F0931QNS1203F0196PAT
NS1146F0931DFCNS12076A583Q
NS1146F0931PATNS12076A583DFC
NS1203F0196PATNS12076A583Q
NS12076A583QNS12076A583PAT
NS12076A583DFCNS1233F0408MC

<colgroup><col><col><col span="3"><col><col><col span="2"><col><col></colgroup><tbody>
</tbody>
So you want the out put to be in J & K? and which range will the code examine?
 
Upvote 0
Output can go anywhere and the original code is in columns A&B. When I ran your code, I got the output in columns F&G. When I looked at column F, I still saw more than one instance. The output should look like J&K but again, it can go into any column.
 
Upvote 0
Output can go anywhere and the original code is in columns A&B. When I ran your code, I got the output in columns F&G. When I looked at column F, I still saw more than one instance. The output should look like J&K but again, it can go into any column.

I tried it, how's that possible? try this, it will clear columns F&G then insert the summary:
Code:
Sub CopyOneInstance()
Dim Cell As Range
Range("F2:G" & Cells(Rows.Count, 6).End(xlUp).Row).Clear
For Each Cell In Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
    If Cell.Value <> Cell.Offset(1, 0).Value Or Cell.Offset(0, 1).Value <> Cell.Offset(1, 1).Value Then
        Range(Cells(Cell.Row, Cell.Column), Cells(Cell.Row, 2)).Copy Range(Cells(Cells(Rows.Count, 6).End(xlUp).Row + 1, 6), _
        Cells(Cells(Rows.Count, 6).End(xlUp).Row + 1, 7))
    End If
Next
Application.CutCopyMode = False
End Sub
I tried the code and it works every time! :confused:
ZAX
 
Upvote 0
nguerra,

It is always best to display your actual raw data, and, the results that you are looking for. This way we can usually find a solution on the first go.

Sample raw data in columns A, and, B:


Excel 2007
ABCDEFG
1PartSub Part
2NS1050F1013PAT
3NS11086B510Q
4NS11086B510Q
5NS11086B510Q
6NS11086B510DFC
7NS11086B510Q
8NS11086B510Q
9NS11086B510PAT
10NS1134F1169DFC
11NS1134F1169PAT
12NS1144F0848DFC
13NS1144F0848PAT
14NS1145F0950Q
15NS1145F0950DFC
16NS1145F0950PAT
17NS1146F0931DFC
18NS1146F0931Q
19NS1146F0931DFC
20NS1146F0931PAT
21NS1203F0196PAT
22NS12076A583Q
23NS12076A583DFC
24
Sheet1


After the new macro in columns F, and, G:


Excel 2007
ABCDEFG
1PartSub PartPartSub Part
2NS1050F1013PATNS1050F1013PAT
3NS11086B510QNS11086B510Q
4NS11086B510QNS1134F1169DFC
5NS11086B510QNS1144F0848DFC
6NS11086B510DFCNS1145F0950Q
7NS11086B510QNS1146F0931DFC
8NS11086B510QNS1203F0196PAT
9NS11086B510PATNS12076A583Q
10NS1134F1169DFC
11NS1134F1169PAT
12NS1144F0848DFC
13NS1144F0848PAT
14NS1145F0950Q
15NS1145F0950DFC
16NS1145F0950PAT
17NS1146F0931DFC
18NS1146F0931Q
19NS1146F0931DFC
20NS1146F0931PAT
21NS1203F0196PAT
22NS12076A583Q
23NS12076A583DFC
24
Sheet1


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub GetFirstPairs()
' hiker95, 02/19/2014, ME758897
Dim r As Long, lr As Long, n As Long, nr As Long
Application.ScreenUpdating = False
Columns("F:G").ClearContents
Cells(1, 6).Resize(, 2).Value = Cells(1, 1).Resize(, 2).Value
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
  nr = Range("F" & Rows.Count).End(xlUp).Offset(1).Row
  n = Application.CountIf(Columns(1), Cells(r, 1).Value)
  If n = 1 Then
    Cells(nr, 6).Resize(, 2).Value = Cells(r, 1).Resize(, 2).Value
  ElseIf n > 1 Then
    Cells(nr, 6).Resize(, 2).Value = Cells(r, 1).Resize(, 2).Value
  End If
  r = r + n - 1
Next r
Columns("F:G").AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetFirstPairs macro.
 
Upvote 0
Maybe...

Code:
Sub FirstOnly()
    Dim lastRow As Long, i As Long
    
    lastRow = Cells(Rows.Count, 1).End(xlUp).Row
    
    With CreateObject("Scripting.Dictionary")
        .comparemode = vbTextCompare
        
        For i = 1 To lastRow
            If Not .exists(Cells(i, 1).Value) Then .Add Cells(i, 1).Value, Cells(i, 2).Value
        Next i
        
        Range("F1").Resize(.Count).Value = Application.Transpose(.keys)
        Range("G1").Resize(.Count).Value = Application.Transpose(.items)
    End With
End Sub

M.
 
Upvote 0
nguerra,

It is always best to display your actual raw data, and, the results that you are looking for. This way we can usually find a solution on the first go.

Sample raw data in columns A, and, B:

Excel 2007
ABCDEFG
1PartSub Part
2NS1050F1013PAT
3NS11086B510Q
4NS11086B510Q
5NS11086B510Q
6NS11086B510DFC
7NS11086B510Q
8NS11086B510Q
9NS11086B510PAT
10NS1134F1169DFC
11NS1134F1169PAT
12NS1144F0848DFC
13NS1144F0848PAT
14NS1145F0950Q
15NS1145F0950DFC
16NS1145F0950PAT
17NS1146F0931DFC
18NS1146F0931Q
19NS1146F0931DFC
20NS1146F0931PAT
21NS1203F0196PAT
22NS12076A583Q
23NS12076A583DFC
24

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



After the new macro in columns F, and, G:

Excel 2007
ABCDEFG
1PartSub PartPartSub Part
2NS1050F1013PATNS1050F1013PAT
3NS11086B510QNS11086B510Q
4NS11086B510QNS1134F1169DFC
5NS11086B510QNS1144F0848DFC
6NS11086B510DFCNS1145F0950Q
7NS11086B510QNS1146F0931DFC
8NS11086B510QNS1203F0196PAT
9NS11086B510PATNS12076A583Q
10NS1134F1169DFC
11NS1134F1169PAT
12NS1144F0848DFC
13NS1144F0848PAT
14NS1145F0950Q
15NS1145F0950DFC
16NS1145F0950PAT
17NS1146F0931DFC
18NS1146F0931Q
19NS1146F0931DFC
20NS1146F0931PAT
21NS1203F0196PAT
22NS12076A583Q
23NS12076A583DFC
24

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1



Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

Code:
Option Explicit
Sub GetFirstPairs()
' hiker95, 02/19/2014, ME758897
Dim r As Long, lr As Long, n As Long, nr As Long
Application.ScreenUpdating = False
Columns("F:G").ClearContents
Cells(1, 6).Resize(, 2).Value = Cells(1, 1).Resize(, 2).Value
lr = Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To lr
  nr = Range("F" & Rows.Count).End(xlUp).Offset(1).Row
  n = Application.CountIf(Columns(1), Cells(r, 1).Value)
  If n = 1 Then
    Cells(nr, 6).Resize(, 2).Value = Cells(r, 1).Resize(, 2).Value
  ElseIf n > 1 Then
    Cells(nr, 6).Resize(, 2).Value = Cells(r, 1).Resize(, 2).Value
  End If
  r = r + n - 1
Next r
Columns("F:G").AutoFit
Application.ScreenUpdating = True
End Sub

Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension .xlsm

Then run the GetFirstPairs macro.

Hiker95,

This worked great and executes very fast. Looks like the values are very close to what I was expecting. If i wanted to add an additional column (c) and have that column be copied over with the other two, what would need to change in the vba code?
 
Upvote 0
nguerra,

Please do not quote entire replies from your helper. When quoting follow these guidelines:
1. Quote ONLY if it is needed to add clarity or context for your reply. If so, then
2. Quote ONLY the specific part of the post that is relevant - - not the entire post.

This will keep thread clutter to a minimum and make the discussion easier to follow.

And, when you respond to your helper, please use their site ID/username/handle.


Thanks for the feedback.

You are very welcome. Glad I could help.


Looks like the values are very close to what I was expecting. If i wanted to add an additional column (c) and have that column be copied over with the other two, what would need to change in the vba code?

In order to continue please supply screenshots of before and after:

To post your data, you can download and install one of the following two programs:
Excel Jeanie
MrExcel HTML Maker

Or, when using Internet Explorer, just put borders around your data in Excel and copy those cells into your post.
See reply #2 the BLUE text in the following link:
http://www.mrexcel.com/forum/about-board/444901-how-create-table-like-aladin.html#post2198045


If you are not able to give us screenshots:
You can upload your workbook to Box Net,
sensitive data changed
mark the workbook for sharing
and provide us with a link to your workbook.
 
Upvote 0
Marco, Yes your code below works better now

Sub FirstOnly()

Dim lastRow As Long, i As Long

lastRow = Cells(Rows.Count, 1).End(xlUp).Row

With CreateObject("Scripting.Dictionary")
.comparemode = vbTextCompare

For i = 1 To lastRow
If Not .exists(Cells(i, 1).Value) Then .Add Cells(i, 1).Value, Cells(i, 2).Value
Next i

Range("F1").Resize(.Count).Value = Application.Transpose(.keys)
Range("G1").Resize(.Count).Value = Application.Transpose(.items)

End With

End Sub



I found out though that I need to add another column and I'd like to have that column copied over just like the second column was. So now you would check the first cell and if different from the previous, copy over that cell and the next two cells.

abcdefgh
NS1050F1013PATKeyNS1050F1013PATKey
NS11086B510QKeyNS11086B510QKey
NS11086B510QKeyNS1134F1169DFCKey
NS11086B510QKey
NS11086B510DFCKey
NS11086B510QKey
NS11086B510QKey
NS11086B510PATKey
NS1134F1169DFCKey
NS1134F1169PATKey
NS1144F0848DFCKey
NS1144F0848PATKey
NS1145F0950QKey
NS1145F0950DFCKey
NS1145F0950PATKey
NS1146F0931DFCKey
NS1146F0931QKey
NS1146F0931DFCKey
NS1146F0931PATKey
NS1203F0196PATKey
NS12076A583QKey
NS12076A583DFCKey
NS12076A583QKey
NS12076A583PATKey
NS1233F0408MCKey
NS1233F0408MCKey
NS1233F0408QKey
NS1233F0408DFCKey
NS1233F0408PATKey
NS103562775PATKey
NS1119F0564QKey
NS1119F0564DFCKey
NS1119F0564QKey
NS1119F0564QKey
NS1119F0564PATKey
NS1147F1664PATKey
NS1150F0976QKey
NS1150F0976DFCKey

<tbody>
</tbody>
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,107
Messages
6,123,127
Members
449,097
Latest member
mlckr

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