compare two ranges in different workbooks and copy data to a 3rd workbook

Reply

Join Date: Feb 2008
Posts: 41
Reputation: asadalim1 is an unknown quantity at this point 
Solved Threads: 0
asadalim1 asadalim1 is offline Offline
Light Poster

compare two ranges in different workbooks and copy data to a 3rd workbook

 
0
  #1
Jun 5th, 2009
Hi
tryna get this peice of code to work but i encountered a RUN TIME ERROR 6 OVER FLOW .

Here is how it goes:

I have two spreadsheets in different workbooks ( workbook 1: sheet 1 and workbook2: sheet1), here i need to compare column 5 in Book1 and Column 5 for all cells, say X is the value we are looking for..

X occurs once in book1 and might occur more than once in book2..so if a match occurs ( that is once the code checks that there is X occuring in both books in columns 5) it should copy all rows in book 2 where X occurs to a new workbook 3 in sheet 1 and also it shoud copy entire row data where X occurs in book 1 sheet 1






Visual Basic 4 / 5 / 6 Syntax (Toggle Plain Text)
  1. Sub wrkting()
  2. Dim b1 As Workbook
  3. Dim b2 As Workbook
  4. Dim b3 As Workbook
  5.  
  6. Dim W1 As Worksheet
  7. Dim W2 As Worksheet
  8. Dim W3 As Worksheet
  9.  
  10. Set b1 = Workbooks("1")
  11. Set b2 = Workbooks("1")
  12. Set b3 = Workbooks("1")
  13. Set W1 = b1.Worksheets("1")
  14. Set W2 = b2.Worksheets("1")
  15. Set W3 = b3.Worksheets("1")
  16. Dim i3 As Long
  17. Dim r As Range
  18. Dim r1 As Range
  19. Dim r2 As Range
  20. Dim c As Range
  21. Dim N As Integer
  22. Dim j1 As Integer
  23. _
  24. Dim j3 As Integer
  25.  
  26.  
  27. 'j1 will be the number of columns copied from w1 to w3
  28. 'j3 will be the column to start placing the copied data from w1 in w3
  29. 'adjust as needed to fit your situation
  30.  
  31. j1 = W1.Cells(2, 256).End(xlToLeft).Column
  32. j3 = W2.Cells(2, 256).End(xlToLeft).Column + 1
  33.  
  34. 'next 3 lines assume headers to be skipped for column 5 processing
  35. W3.UsedRange.Offset(2, 0).Clear
  36. Set r1 = W1.Range("E3:E" & W1.Cells(65526, 5).End(xlUp).Row)
  37. Set r2 = W2.Range("E2:E" & W2.Cells(65526, 5).End(xlUp).Row)
  38. If W2.AutoFilterMode Then W2.Cells.AutoFilter
  39. i3 = 2
  40. For Each c In r1
  41. If Not IsEmpty(c) Then
  42. Set r = Nothing
  43. W2.Columns(5).AutoFilter 1, c.Value, xlOr, c.Value & "*"
  44. On Error Resume Next
  45. Set r = r2.SpecialCells(xlCellTypeVisible)
  46. On Error GoTo 0
  47.  
  48. If Not r Is Nothing Then
  49. i3 = i3 + 1
  50. N = r.Cells.Count - 1
  51. r.EntireRow.Copy W3.Cells(i3, 1)
  52. W1.Range(W1.Cells(c.Row, 1), W1.Cells(c.Row, j1)).Copy _
  53. W3.Range(W3.Cells(i3, j3), W3.Cells(i3 + N, j3 + j1 - 1))
  54. i3 = i3 + N
  55. End If
  56. End If
  57. Next c
  58. W2.Columns(5).AutoFilter
  59. Application.ScreenUpdating = True
  60.  
  61.  
  62. End Sub
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC