Excle macro

Last Edited By Krjb Donovan
Last Updated: Mar 05, 2014 09:35 PM GMT

Question

I have a large data. I need to match column a to column b and copy the value in column c when value in column a matches with the value in column b. When columns a and b do not match, I need to shift the cells in columns b and c down with with their contents and again match columns a and b, and the process repeats until all values in columns a and b are matched.

Example of My data: Before running the macro a b c 1 1 35 2 2 50 3 3 60 4 5 21 5 6 45 6 7 80 7 8 90 8 9 40 9

After running the macro, the data should look like this a b c 1 1 35 2 2 50 3 3 60 4 5 5 21 6 6 45 7 7 80 8 8 90 9 9 40

Your kind help is very much appreciated.

Dorji

Answer

Dorji

This works on the sample data you gave. I believe it will work for expanded data if I understand the pattern you described.


Sub test()

Dim n As Integer

n = 1

Do While Cells(n, 1).Value <> ""

   If Cells(n, 2).Value <> Cells(n, 1).Value Then
       Cells(n, 2).Resize(1, 2).Insert Shift:=xlDown
       n = n + 1
   End If
   
   n = n + 1

Loop

End Sub

I hoe this helps.

Advertisement

©2021 eLuminary LLC. All rights reserved.