compare columns in two different sheets.

Gangster

New Member
Joined
Apr 15, 2014
Messages
23
HELLO GURUS,
im very new to VBA macro. i have a task to be done. i have two sheets in same workbook.
In each sheet i have a 20*8 matrix table with data.
i need to compare column B and C from sheet1 to all the values in column B and C in sheet.
for ex: sheet1
B(2,2) and C(2,3) to any rows in column B and C.
if they match i need to retrieve data from column say D to H from sheet1 and place it in sheet2.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Gangster,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
Gangster,

1. can u plz insert comment lines..so that i can understand the working of the code. as im just a beginner.

Thanks for the Private Message reminder.


I hope all the 'text lines above each section of code will help you to understand the macro code.


Code:
'  the following line is the macro name
Sub UpdateSheet2Sheet3()



' the following line is a way for me to keep track of the macros
'          date
'                      MrExcel thread number
' hiker95, 04/18/2014, ME771379



' it is a good practice to delcare all your variables
'   if there was a problem in your code, then someone with experience
'   could help to repair a problem
' the following 3 are worksheet variables
Dim w1 As Worksheet, w2 As Worksheet, w3 As Worksheet



' c is a range object used to loop vertically in Sheet1 = w1, in column N
'               nrng is a range object to search vertically in Sheet2 = w2, in column N
Dim c As Range, nrng As Range



'  lr1 is used to find the last row in Sheet1 = w1
'                lr2 is used to find the last row in Sheet2 = w2
'                             lr3 is used to find the last row in Sheet3 = w3
'                                          nr is used to find the next available blank row in Sheet3 = w3
Dim lr1 As Long, lr2 As Long, lr3 As Long, nr As Long



' turn off screen updating, and, screen flickering when the macro is running
Application.ScreenUpdating = False



' set the worksheet variables to their respective worksheets
Set w1 = Sheets("Sheet1")
Set w2 = Sheets("Sheet2")
Set w3 = Sheets("Sheet3")



' In Sheet3 = w3
With w3



' find the last used row in Sheet3, column A
  lr3 = .Cells(Rows.Count, "A").End(xlUp).Row



' if the last used row is greater than the title row
'   then clear range A2:G lr3
  If lr3 > 1 Then .Range("A2:G" & lr3).ClearContents
End With



' In Sheet2 = w2
With w2



' find the last used row in Sheet2, column B
  lr2 = .Cells(Rows.Count, "B").End(xlUp).Row
  
  
  
' Concatenate/join in each row in column N, my work areas, in R1C1 notation
'   Column B and Column C data for searching
  With .Range("N2:N" & lr2)
    .FormulaR1C1 = "=RC[-12]&RC[-11]"
    
    
    
'   change the R1C1 notation to a value
    .Value = .Value
  End With
End With



' In Sheet1 = w1
With w1



' find the last used row in Sheet1, column B
  lr1 = .Cells(Rows.Count, "B").End(xlUp).Row
  
  
  
' Concatenate/join in each row in column N, my work areas, in R1C1 notation
'   Column B and Column C data for searching
  With .Range("N2:N" & lr1)
    .FormulaR1C1 = "=RC[-12]&RC[-11]"
    
    
    
'   change the R1C1 notation to a value
    .Value = .Value
  End With
  
  
  
' loop thru range N2:N16
  For Each c In .Range("N2:N" & lr1)
  
  
  
'   if the cell is not empty/blank
    If c <> "" Then
    
    
    
    
'     search what is in column N, cell N2
'                in w2 column N
      Set nrng = w2.Columns("N:N").Find(c, LookAt:=xlWhole)
      
      
      
'     if the search finds a match in w2 column N
      If Not nrng Is Nothing Then
      
      
      
'       copy w1 range H c.Row thru M c.Row
'                                            to w2 range H nrng.Row
        .Range("H" & c.Row).Resize(, 6).Copy w2.Range("H" & nrng.Row)
        Application.CutCopyMode = False
      End If
    End If
  Next c
End With



' clear the work areas in w1 and w2 in column N
w1.Range("N2:N" & lr1).ClearContents
w2.Range("N2:N" & lr2).ClearContents



' in w2 = Sheet2
With w2



' loop thru the cells in row 1 thru the lr2
  For r = 2 To lr2
  
  
  
'   If column H row is blank
    If .Cells(r, 8) = "" Then
    
    
    
'     find the next available blank row in Sheet3 = w3
'                                  in column A
      nr = w3.Cells(w3.Rows.Count, "A").End(xlUp).Row + 1
      
      
      
'     copy range A thru G of the r row to
'                                     the next available row
'                                     in Sheet3 = w3
'                                               in column A
      .Range("A" & r & ":G" & r).Copy w3.Range("A" & nr)
      
      
      
'     remove whatever is in the copy buffer
      Application.CutCopyMode = False
    End If
  Next r
  
  
  
' autofit all the columns in Sheet3 = w3
  .Columns("H:M").AutoFit
  
  
  
' Activate/Select Sheet3 = w3
  .Activate
End With



' turn Screen Updating back on
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Gangster,

Thanks for the feedback.

You are very welcome. Glad I could help.

And, come back anytime.
 
Upvote 0
hi hiker95,

i need your help..!!!!!



I have a workbook with for ex 30*5 matrix with data. i need to group these data in sheet2 with condition if data in column D occurs greater then or equal to 5. Rest/lesser then can be ignore..
NOTE : with space of two columns left after every grouping.



After grouping, i need to check whether there is a match between column c,d in (sheet2)workbook1 with coulmn a,b in workbook2.

if it matches i need to copy contents from coulmn B,C,D from workbook2 and paste into column E,F,G in sheet2/workbook1.

i have attached a sample workbook...


8huhjpcwe1xwpe0vwlms

https://app.box.com/s/8huhjpcwe1xwpe0vwlms

1q2kv9mhd8mmiagj0gb7

https://app.box.com/s/1q2kv9mhd8mmiagj0gb7


thanks in advance...
 
Upvote 0
Gangster,

Sorry, but, I have not worked with coding a macro that uses two or more workbooks in a very long time.

Click on the Reply to Thread button, and just put the word BUMP in the thread. Then, click on the Post Quick Reply button, and someone else will assist you.
 
Upvote 0
Hi hiker95,
Thanks for the reply..
i have just changed the logic..With your help we ll get the desired output.
---------------------
i need to sort data if they repeat for more then 5 times.. else delete the data...
and insert a line after each sort..
im attaching a sample workbook.
where sheet1 contains the data.
sheet2 contains the desired output.
gut71s997ujitudy528l

https://app.box.com/s/gut71s997ujitudy528l

Thanks in advance....
 
Upvote 0
Gangster,

Thanks for the new workbook.

1. Your raw data is in Sheet1?

2. You want the raw data in Sheet1, to be replaced with your current requirements, replaceing/over-writing the raw data?

3. What is Sheet3 for?
 
Upvote 0

Forum statistics

Threads
1,215,377
Messages
6,124,598
Members
449,174
Latest member
chandan4057

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