VBA - Compare 2 columns for partial matches, and return only last 3 characters of matches

TropicalMagic

New Member
Joined
Jun 19, 2021
Messages
47
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I’m trying to –
  1. Compare Column G from Workbook 1, starting from row 2, to only the first 10 characters in Column A, starting from row 9, from Workbook 2 for matching values
  2. If there are matching values, copy only the last 3 characters in Column A from Workbook 2 to Column F from Workbook 1
However, the macro is continuously running without ending. I'm not sure if there is any error present.

Here is my code:

VBA Code:
Dim Range1 As Range, Range2 As Range, Rng1 As Range, Rng2 As Range
Dim i As Long

Set Range1 = Workbook1.Sheet(1).Range("G9:G" & LASTROW1)
Set Range2 = Workbook2.Sheet(1).Range("A2:A" & LASTROW2)

For i = 9 to LASTROW1
For Each Rng1 in Range1
For Each Rng2 In Range2
If Rng1.Value = Left(Rng2.Value, 10) Then
    Right(Rng2.Value, 3).Copy
    Workbook1.Sheet(1).Range("F" & i).PasteSpecial
End If

Next
Next
Next

Many thanks in advance!
 
Hello. Thank you for this beautiful symbol as usual. How can you add another column condition so that the comparison is made, for example, in column A and column C?
The method used is to search only in one column. To search in 2 or more columns another method must be used.
You would have to explain exactly what you want to search for in A and what you want to search for in C, depending on that a solution would be created.
If you have a similar problem, I suggest you create a new thread.
 
Upvote 0

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Forum statistics

Threads
1,215,097
Messages
6,123,076
Members
449,094
Latest member
mystic19

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