Automate Excel to shift data so the values in two rows match?

EmilyExcel

New Member
Joined
Jun 27, 2014
Messages
2
Hello! Please help me with this quandary for my internship. I would greatly appreciate it :) Here is my problem:
</SPAN>
I want to match ID numbers in two columns by shifting the ID from Source 2 (and shifting its unique attributes along with it) so that it lines up in the proper row and matches the ID from Source 1. Both sources include ID numbers that the other one doesn’t have. I don’t want to delete those rows, but just leave the Source 2 cells that are lacking the information blank. Ideally the blank cells would be automatically highlighted. Here is what I am trying to do:</SPAN>

Before:</SPAN>
Source 1 unique attributes</SPAN>
ID from Source 1</SPAN>
ID from Source 2</SPAN>
Source 2 unique attributes</SPAN>
AA</SPAN>
1</SPAN>
2</SPAN>
BA</SPAN>
AB</SPAN>
3</SPAN>
3</SPAN>
BB</SPAN>
AC</SPAN>
5</SPAN>
4</SPAN>
BC</SPAN>
AD</SPAN>
6</SPAN>
5</SPAN>
BD</SPAN>
AE</SPAN>
8</SPAN>
6</SPAN>
BE</SPAN>
AF</SPAN>
10</SPAN>
11</SPAN>
BF</SPAN>
AG</SPAN>
12</SPAN>
12</SPAN>
BG</SPAN>

<TBODY>
</TBODY>

After:</SPAN>
Source 1 unique attributes</SPAN>
ID from Source 1</SPAN>
ID from Source 2</SPAN>
Source 2 unique attributes</SPAN>
AA</SPAN>
1</SPAN>


AB</SPAN>
3</SPAN>
3</SPAN>
BB</SPAN>
AC</SPAN>
5</SPAN>
5</SPAN>
BD</SPAN>
AD</SPAN>
6</SPAN>
6</SPAN>
BE</SPAN>
AE</SPAN>
8</SPAN>


AF</SPAN>
10</SPAN>


AG</SPAN>
12</SPAN>
12</SPAN>
BG</SPAN>

<TBODY>
</TBODY>

Is there a command, formula, or macro you can suggest to automate this work? Otherwise I have to manually go through it. I look at “ID from Source 2” and if that doesn’t match the “ID from Source 1,” I delete “ID from Source 2” along with its “Source 2 unique attributes.” I then shift all of the Source 2 cells up the rows and check again if the new ID matches with the ID from Source 1. If not, delete and shift the cells up again. If it is obvious I simply do not have the necessary ID from Source 2, I leave it blank and continue down the row. I need to be careful to not just keep deleting Source 2 IDs that match IDs from Source 1 in rows farther down.</SPAN>

Thank you for your help :rolleyes: -Emily</SPAN>
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
try this

Code:
Sub AlignData()

Dim LR As Long, i As Long
Dim IFind As Variant, IFind2 As Variant
'this compares two columns (B, C) to align matching data to same row
Application.ScreenUpdating = False
LR = Range("A" & Rows.Count).End(xlUp).Row
i = 2

Do
        IFind = Cells(i, 2)
        IFind2 = Cells(i, 3)
        IFind3 = Cells(i + 1, 2)
    If IFind < IFind2 And Not IFind2 >= IFind3 Then
        Range(Cells(i, 3), Cells(i, 4)).Clear
    End If
    If IFind > IFind2 Then
        Range(Cells(i, 3), Cells(i, 4)).Delete Shift:=xlUp
    End If
    If IFind3 < IFind2 And IFind3 <> Empty Then
        Range(Cells(i, 3), Cells(i, 4)).Insert Shift:=xlDown
    End If
    i = i + 1
Loop Until IsEmpty(Cells(i, 1))
      
MsgBox "Done"
Application.ScreenUpdating = True
End Sub

note: be sure to save your file and or make a copy of the data
 
Upvote 0
Dear Mr. Texas,

Thanks a lot!! This is looking really good :) I ran your code and it did nearly everything we need it to do. The only prob is that it does not delete entries for "ID from Source 2" and "Source 2 unique attributes" when the corresponding ID from 1 doesn't exist in the workbook, and it doesn't insert blank cells in "ID from Source 2" and "Source 2 unique attributes" and move the data in those columns down to try and match the IDs up again in the next row. You can see below..."11" and "BF" (from source 2, in the 3rd and 4th columns) should simply be deleted and "12" and "BG" (again, from source 2, in the 3rd and 4th columns) should be moved down to the last row to line up with the ID from Source 1 entitled "12".
After running code:


Source 1 unique attributes -
- ID from Source 1 - -
ID from Source 2-
Source 2 unique attributes -
AA 1
AB 3
3
BB
AC 5
5 BD
AD 6
6 BE
AE 8
11
BF
AF 10
12
BG
AG

Are you able to figure out how
to fix those probs?? I tried
myself but I am not much
of a programmer, hahah

Any advice you can give
would be greatly
appreciated. I feel
like we are so close
12

<tbody>
</tbody>
to cracking this bugger!!



<tbody>
</tbody>
Thanks again :) -Em
 
Upvote 0

Forum statistics

Threads
1,214,788
Messages
6,121,575
Members
449,039
Latest member
Arbind kumar

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