Match Concatenate and Adjust Row in Macro Excel

nurj

New Member
Joined
Feb 8, 2020
Messages
13
Office Version
  1. 2016
Platform
  1. Windows
Hi all...

I have series of data in Sheet1 and Sheet2. I want to match row from sheet1 to sheet2 with the condition. If the result of concatenate in Sheet1 and Sheet2 is match, then the data in that row is adjusted. I am stuck at adjusting the rows

Here is my data and the output

Book1
ABCDEFGHIJKL
1ABCDE111FGHBCG
2ABCDE111FGHBCG
3SDWER111FVADWV
4SDWER111FVADWV
5TREBN111ASQRES
6DFQLK111QVBFQV
7VBRTU111AHLBRH
8VBRTU111AHLBRH
9LGEBX111KRPGER
10ACVET111HMACVM
Sheet1

Book1
ABCDEFGHIJKL
1ABCDE111FGHBCG
2SDWER111FVADWV
3TREBN111ASQRES
4DFQLK111QVBFQV
5VBRTU111AHLBRH
6LGEBX111KRPGER
7BFQWM111MQDFQQ
8ACVET111HMACVM
Sheet2

Book1
ABCDEFGHIJKLMNOPQRSTUVWXYZ
1ABCDE111FGHBCGABCDE111FGHBCG
2ABCDE111FGHBCG
3SDWER111FVADWVSDWER111FVADWV
4SDWER111FVADWV
5TREBN111ASQRESTREBN111ASQRES
6DFQLK111QVBFQVDFQLK111QVBFQV
7VBRTU111AHLBRHVBRTU111AHLBRH
8VBRTU111AHLBRH
9LGEBX111KRPGERLGEBX111KRPGER
10BFQWM111MQDFQQ
11ACVET111HMACVMACVET111HMACVM
Compare


Thank you for you help...
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
I was not sure whether the row in sheet1 or sheet2 or both was being amended based on concatenation matching up
Code below writes a value to column AA in the matched row in both sheets - so take your pick
The code assumes that concatenated values are already in column L in both sheets and that the actual data starts in row 2 in both sheets

Put code in a module on its own
VBA Code:
Option Explicit
'make available to more than one procedure

Private w1 As Worksheet, w2 As Worksheet, rng1 As Range, rng2 As Range, row1 As Long, row2 As Long
Sub AlterRows()
    Dim cel As Range, row1 As Long, row2 As Long
    SetRanges
    For Each cel In rng1
        row2 = GetMatch(cel.Text)
        If row2 > 0 Then
            row1 = cel.Row
            w1.Cells(row1, "AA") = "amended1"
            w2.Cells(row2, "AA") = "amended2"
        End If
    Next cel
End Sub

Private Function GetMatch(concat As String) As Long
    On Error Resume Next
    If concat <> "" Then GetMatch = rng2.Find(concat, lookat:=xlWhole).Row
End Function

Private Sub SetRanges()
    Set w1 = Sheets("Sheet1")
    Set w2 = Sheets("Sheet2")
    Set rng1 = w1.Range("L2", w1.Range("L" & Rows.Count).End(xlUp))
    Set rng2 = w2.Range("L2", w2.Range("L" & Rows.Count).End(xlUp))
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,981
Messages
6,122,566
Members
449,089
Latest member
Motoracer88

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