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...
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:
Sub ReplaceRowData()
Dim strFindWhat As String
Dim rngFound As Range
Dim i As Long
' Loop rows in Book 1.
For i = 1 To Workbooks("Book1").Sheets("Sheet1").UsedRange.Rows.Count
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.
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.
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:
Const xlValues = -4163
Const xlWhole = 1
Const xlByRows = 1
Const xlNext = 1
Sub ReplaceGender()
Dim strFindWhat As String
Dim strColWithName As String
Dim strColWithGender As String
Dim strFilePath As String
Dim appExcel As Object
Dim rngFound As Object
Dim i As Long
strColWithName = "A"
strColWithGender = "B"
strFilePath = "C:\Junk\Excel\Excel2\" ' <---Set the path to the workbooks here.
Set appExcel = CreateObject("Excel.Application")
With appExcel
' Open workbooks.
.Workbooks.Open (strFilePath & "Book1.xls")
.Workbooks.Open (strFilePath & "Book2.xls")
' Loop rows in Book 1.
For i = 1 To .Workbooks("Book1.xls").Sheets("Sheet1").UsedRange.Rows.Count
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...