VBA script - Compare - Loops

NeewBie

New Member
Joined
Aug 7, 2012
Messages
40
Hi,

Can someone help me to write a script that compares the value in columns A with columns B. If the value match in both columns then write the new data in new worksheet.

I also winder where I can find an VBA Index there all functions are included like If, Else, For, Len etc. Where it is explained.

Thank you!
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
I am not sure this is what you want because you did not explain what "new data" means or where it would be located. The procedure below assumes the new data would be found on the row that matched the column B data and would be from column B thru the last column with data. If that is incorrect, then provide the correct details. For the index to terms and conditions, the VBA help file is pretty good, once you learn how to use it. Just type a keyword in the searchy box and then use the menu of topics to narrow your seach to what you are looking for.

Code:
Sub match()
Dim sh As Worksheet, lr As Long, rng As Range, Brng As Range
Set sh = Sheets(1) 'Edit sheet name
lr = sh.Cells(Rows.Count, 1).End(xlUp).Row
Set rng = sh.Range("A2:A" & lr)
Set Brng = sh.Range("B2:B" & sh.Cells(Rows.Count, 2).End(xlUp).Row)
For Each c In rng
For Each i In Brng
If c.Value = i.Value Then
col = sh.Cells(i.Row, Columns.Count).End(xlToLeft).Column
With sh
.Range(.Cells(i.Row, 2), .Cells(i.Row, col)).Copy
End With
Sheets(2).Range("A2").Insert
End If
Next
Next
End Sub
Code:
 
Upvote 0
Hi JLGWhiz!

Than kyou for youre answer. The problem is that id do not works? Let say that I have this value:

Column A Column B
10 10
12 9
15 15
32 11
17 17

In this case I want that the script will compare all values in the column A and B. When it finds same value somwhere in the columns then I want it to write in worksheet 2.

Cheers m8!
 
Upvote 0
Hi JLGWhiz!

Than kyou for youre answer. The problem is that id do not works? Let say that I have this value:

Column A Column B
10 10
12 9
15 15
32 11
17 17

In this case I want that the script will compare all values in the column A and B. When it finds same value somwhere in the columns then I want it to write in worksheet 2.

Cheers m8!

Hi, Can someone please still help me?
 
Upvote 0

Forum statistics

Threads
1,215,005
Messages
6,122,661
Members
449,091
Latest member
peppernaut

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