Results 1 to 17 of 17

Thread: Matching cells from 2 files

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332

    Matching cells from 2 files

    I have 2 files with data. What i want to do is to match rows of data, and if a match is found, i want to update information from 1 file to the other...

    to do this, means for each row in the 1st file, i will have to do a search in the whole 2nd file...

    is this possible?

  2. #2
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Do you just need to match the values in one column to the values in the other book? This example matches values in col A in one book to values in col A of another book and if found copies over the data from the found row.

    VB Code:
    1. Sub ReplaceRowData()
    2.    
    3.     Dim strFindWhat     As String
    4.     Dim rngFound        As Range
    5.     Dim i               As Long
    6.    
    7.     ' Loop rows in Book 1.
    8.     For i = 1 To Workbooks("Book1").Sheets("Sheet1").UsedRange.Rows.Count
    9.    
    10.         ' Get text from col A of Book 1.
    11.         strFindWhat = Workbooks("Book1").Sheets("Sheet1").Range("A" & i).Value
    12.        
    13.         ' Find in col A of Book 2.
    14.         Set rngFound = Workbooks("Book2").Sheets("Sheet1").Cells.Find( _
    15.             What:=strFindWhat, LookIn:=xlValues, LookAt:=xlWhole, _
    16.             SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
    17.        
    18.         ' If found, replace values in Book 1 with values from found row in Book 2.
    19.         If Not rngFound Is Nothing Then
    20.             Workbooks("Book1").Sheets("Sheet1").Rows(i).Value = _
    21.             Workbooks("Book2").Sheets("Sheet1").Rows(rngFound.Row).Value
    22.         End If
    23.                
    24.    Next i
    25.    
    26. End Sub

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    thank you for your reply

    well, basically, bboth files have identical fields...

    the fields i am mainly concerned with in this situation is :
    1) Name
    2) Gender field

    Currently, the 2nd file has the correct gender fields

    I want to match the names in both files, once a match is found, the gender field is copied from the 2nd file to overwrite the gender field in the 1st file. Also, it would be nice to be able to highlight the cells of those names in the 1st file had no match in the 2nd file.

    Can your code do this?

  4. #4
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Sure.

    VB Code:
    1. Sub ReplaceGender()
    2.    
    3.     Dim strFindWhat         As String
    4.     Dim rngFound            As Range
    5.     Dim strColWithName      As String
    6.     Dim strColWithGender    As String
    7.     Dim i               As Long
    8.    
    9.     strColWithName = "A"
    10.     strColWithGender = "B"
    11.    
    12.     ' Loop rows in Book 1.
    13.     For i = 1 To Workbooks("Book1").Sheets("Sheet1").UsedRange.Rows.Count
    14.    
    15.         ' Get text from col A of Book 1.
    16.         strFindWhat = Workbooks("Book1").Sheets("Sheet1").Range(strColWithName & i).Value
    17.        
    18.         ' Find in col A of Book 2.
    19.         Set rngFound = Workbooks("Book2").Sheets("Sheet1").Columns(strColWithName & ":" & strColWithName).Find( _
    20.             What:=strFindWhat, LookIn:=xlValues, LookAt:=xlWhole, _
    21.             SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
    22.        
    23.         ' If found, replace values in Book 1 with values from found row in Book 2.
    24.         If Not rngFound Is Nothing Then
    25.             Workbooks("Book1").Sheets("Sheet1").Range(strColWithGender & i).Value = _
    26.             Workbooks("Book2").Sheets("Sheet1").Range(strColWithGender & rngFound.Row).Value
    27.         Else
    28.             ' Highlight row with no match.
    29.             Workbooks("Book1").Sheets("Sheet1").Rows(i).Interior.ColorIndex = 37
    30.         End If
    31.                
    32.    Next i
    33.    
    34. End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    Workbooks("Book1").Sheets("Sheet1").UsedRange.Rows.Count

    Workbooks("Book1").Sheets("Sheet1").Range(strColWithName & i).Value

    these 2 statements when i used, gave error mesages "Subscript out of range", how can i fix this?

    also, i have to put my excel file called "Book1" in the same folder as that of my vb project with this code right?

  6. #6
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    The subsripts are then workbook and worksheet names that you are passing. Sochange "Book1" and/or "Sheet1" to match the names of the actual workbook & worksheet that you are referenceing.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    i did save my file as Book1 and the default name for sheet is sheet1, id idn't change that...
    so it should be same rite?

    so wat's the cause ut hink?

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    elo, sorry for long w ait....i've tried but i dunno y i still get the error message despite doing wat u've siad...

  9. #9
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    The you must be setting strColWithName to a column outside of the used range. What line is rasiing the error?

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    For i = 1 To Workbooks("Book1").Sheets("Sheet1").UsedRange.Rows.Count

    this line gives the error

    subscript out of range


    i checked, the StrColWithName is correct, both are correct...
    should i attach a sample of my code and files for u to check?

  11. #11
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    Yep. Post the files. Either the workbook or worksheet name doesn't match your code.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    okay, i still can't figure out, but here are my sample files...
    Attached Files Attached Files

  13. #13
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    You need to use .xls in the book names: "Book1.xls". Either that or use the Workbook indexes: Workbook(1), Workbook(2), etc.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    i did wat u said but i still get the errros...attached are the files...
    Attached Files Attached Files

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    er.........could u help me see if there's any problem with it please as i can't find the problem...please advise...thank you.

  16. #16
    Fanatic Member WorkHorse's Avatar
    Join Date
    Jul 2002
    Location
    Where you live.
    Posts
    591
    You didn't say you were doing this for a VB app. You can't just copy in VBA code. ou have to create an application object and open the workbooks. Here:

    VB Code:
    1. Const xlValues = -4163
    2. Const xlWhole = 1
    3. Const xlByRows = 1
    4. Const xlNext = 1
    5.  
    6. Sub ReplaceGender()
    7.    
    8.     Dim strFindWhat         As String
    9.     Dim strColWithName      As String
    10.     Dim strColWithGender    As String
    11.     Dim strFilePath         As String
    12.     Dim appExcel            As Object
    13.     Dim rngFound            As Object
    14.     Dim i               As Long
    15.    
    16.     strColWithName = "A"
    17.     strColWithGender = "B"
    18.     strFilePath = "C:\Junk\Excel\Excel2\" ' <---Set the path to the workbooks here.
    19.    
    20.     Set appExcel = CreateObject("Excel.Application")
    21.    
    22.     With appExcel
    23.        
    24.         ' Open workbooks.
    25.         .Workbooks.Open (strFilePath & "Book1.xls")
    26.         .Workbooks.Open (strFilePath & "Book2.xls")
    27.        
    28.         ' Loop rows in Book 1.
    29.         For i = 1 To .Workbooks("Book1.xls").Sheets("Sheet1").UsedRange.Rows.Count
    30.        
    31.             ' Get text from col A of Book 1.
    32.             strFindWhat = .Workbooks("Book1.xls").Sheets("Sheet1").Range(strColWithName & i).Value
    33.            
    34.             ' Find in col A of Book 2.
    35.             Set rngFound = .Workbooks("Book2.xls").Sheets("Sheet1").Columns(strColWithName & ":" & strColWithName).Find( _
    36.                 What:=strFindWhat, LookIn:=xlValues, LookAt:=xlWhole, _
    37.                 SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
    38.            
    39.             ' If found, replace values in Book 1 with values from found row in Book 2.
    40.             If Not rngFound Is Nothing Then
    41.                 .Workbooks("Book1.xls").Sheets("Sheet1").Range(strColWithGender & i).Value = _
    42.                 .Workbooks("Book2.xls").Sheets("Sheet1").Range(strColWithGender & rngFound.Row).Value
    43.             Else
    44.                 ' Highlight row with no match.
    45.                 .Workbooks("Book1.xls").Sheets("Sheet1").Rows(i).Interior.ColorIndex = 37
    46.             End If
    47.                    
    48.         Next i
    49.    
    50.         .Workbooks("Book1.xls").Save
    51.         .Workbooks("Book1.xls").Close False
    52.         .Workbooks("Book2.xls").Close False
    53.        
    54.         .Quit
    55.    
    56.     End With
    57.    
    58.     Set appExcel = Nothing
    59.    
    60. End Sub
    61.  
    62. Private Sub Command1_Click()
    63.     ReplaceGender
    64. End Sub

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jan 2002
    Posts
    332
    hey there WorkHorse, really need to thank you for the help with the code...for your information it was a success!
    although the whole search and replace operation took about 9 hours to complete (yeap! i left the PC running from morning to evening at my workplace), coz both of the files had over 7000 entries...

    i need your advice on my post at http://www.vbforums.com/showthread.p...hreadid=268301

    could you give me ur opinions there? thanx a lot!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width