Making Value common in corresponding Cell

kshitij_dch

Active Member
Joined
Apr 1, 2012
Messages
362
Office Version
  1. 365
  2. 2016
  3. 2007
Platform
  1. Windows
I have 2 columns

Column A and Column B
Column B is sorted and It have common Value group together

Column A has unique values corresponding to column B

I want a formula or a macro which will find a group of common value in B column and Make first value common for group in column B for an example :

It is like :-

ID'sLoan Number
123ABC
456ABC
789ABC
4545BCA
5656BCA
6767BCA

I want like -:

ID'sLoan Number
123ABC
123ABC
123ABC
4545BCA
4545BCA
4545BCA
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
VBA Code:
Sub test()
  Dim lRow As Long, f As Long
  lRow = Cells(Rows.Count, 1).End(xlUp).Row

  For i = 2 to lRow
    If Cells(i, 2).Value <> Cells(i-1, 2).Value Then
      For j = 1 to lRow
        If Cells(i + j, 2).Value = Cells(i, 2).Value Then
          Cells(i + j, 1).Value = Cells(i, 1).Value
        Else
          Exit For
        End If
      Next
    End If
  Next
End Sub
 
Upvote 0
VBA Code:
Sub test()
            Dim lr As Long
            
            lr = Cells.Find("*", Cells(1, 1), xlFormulas, xlWhole, xlByRows, xlPrevious, False).Row
            Dim k As Integer
            
            For k = 2 To lr
                If Range("B" & k) = Range("B" & k + 1) Then
                    Range("A" & k + 1) = Range("A" & k)
                End If
            Next k

End Sub
 
Upvote 0
A non-looping alternative that you could test:
VBA Code:
Sub CommonValue()
  With Range("A2", Range("A" & Rows.Count).End(xlUp))
    .Value = Evaluate("if(" & .Offset(, 1).Address & "=" & .Offset(-1, 1).Address & ",""""," & .Address & ")")
    .SpecialCells(xlBlanks).FormulaR1C1 = "=R[-1]C"
    .Value = .Value
  End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,827
Messages
6,181,194
Members
453,021
Latest member
pingpong7117

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