Copy data programmatically between Excel files

dbfromnewjersey
dbfromnewjersey used Ask the Experts™
on
I have two Excel files (examples attached).

I've labeled one a Master File and the other a Transaction file.

The Master File gets much, but not all, of it's data from the Transaction File.

Each file contains around 200 rows.

The data is not arranged in the same order between the files and the order of each can't be changed.

There is a row name for each data row in each file but no naming convention between the two files.

What I mean by that is that there are instances where:

1. a particular row in the Master File may be related to a row in the Transaction file where the row
names DON'T match and you can't tell they are related.
2. a particular row in the Master File may be related to a row in the Transaction file where the row
names DO match.
3. a particular row in the Master File may be related to a row in the Transaction file where the row
names DON'T match exactly but are close enough to be able to tell they are related.

For reasons I won't bother to get into, the row names can't be changed.

I also am not going to link the files.


What I'm looking to be able to do is:

1. From the Master File, be able to click on a button that will open windows explorer and allow me to navigate to theTransaction File I want to select for "import".

2. Upon selecting the file, the program will, starting with the first (data) row name of the Master File (the row names are contained in column A of each file), search the row names in the Transaction File and if and when it finds the namein the Transaction File that is related to the name in the Master File, write the data in column 2 of the applicable row of the Transaction File to column 2 of the applicable row of the Master File.

3. Once the entire Transaction File is searched and a match is not found or a match is found and written to the Master File, the program should advance to the next row of the Master File and repeat the process.  Do this until the end of the Master File is reached.

If you look at the attached files and my notes in them, you should be able to see what I'm looking to achieve.


Thank you.
Master-File.xlsx
Transaction-File.xlsx
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Joe WinogradDeveloper
Fellow 2017
Most Valuable Expert 2018

Commented:
Hi db,
Some questions:

(1) You note that processing in the Transaction File can start in the row after "Row Name" appears in column A, but when should processing stop? After the first empty cell in column A? After N consecutive empty cells in column A? After some "marker" text (i.e., a stop processing flag) in column A?

(2) How would the program know that the "acctg" transaction (or accnt, accntg, acctng, etc.) belongs in the "Accounting" row of the Master File? Or that the "mrktg" transaction (or mrktng, mktg, mktng, etc.) belongs in the "Marketing" row? Will you provide a list of such mappings? Or do you want the program to employ some heuristics, such as number of characters that match in the column name, to make the determination?

(3) You say that "when it finds the name in the Transaction File that is related to the name in the Master File, write the data in column 2 of the applicable row of the Transaction File to column 2 of the applicable row of the Master File". Do you mean that it should add the data in column 2 of the Transaction File to the value in column 2 of the Master File? Or should it overwrite whatever is there (meaning the last value "wins")? If neither of those, what do you mean by "write" it?

(4) What is your deadline/time-frame for needing this program?

(5) What is your budget for developing this program?

Regards, Joe
Mechanical Engineer
Most Valuable Expert 2013
Top Expert 2013
Commented:
I suggest that you make a two column table listing the names that might be in the Transactions workbook along with the corresponding names in the Master workbook. This may be a many to one relationship (many different names in Transactions corresponding to one name in Master.

I put such a table in a newly added Data worksheet. The code is designed so it may be in another workbook (neither Master nor Transactions).
Sub ImportTransactions()
Dim wbSource As Workbook
Dim cel As Range, rgDest As Range, rgMatch As Range, rgSource As Range
Dim destIndex As Variant, destLbl As Variant

With ActiveWorkbook.Worksheets(1)
    Set rgDest = .Columns("A:A").Find("Row Name", LookAt:=xlWhole, LookIn:=xlValues, MatchCase:=False)
    Set rgDest = Range(rgDest.Offset(1, 0), .Cells(.Rows.Count, rgDest.Column).End(xlUp))
End With
Set rgMatch = ThisWorkbook.Worksheets("Data").ListObjects("tbMatching").DataBodyRange

On Error Resume Next
Set wbSource = GetTransactionFile()
On Error GoTo 0

Application.ScreenUpdating = False

If Not wbSource Is Nothing Then
    rgDest.Offset(0, 1).ClearContents  'Clear existing values from destination cells
    With wbSource.Worksheets(1)
        Set rgSource = Range(.Cells(1, 1), .Cells(.Rows.Count, 1).End(xlUp))
    End With
    
    For Each cel In rgSource.Cells
        Set destLbl = Nothing
        If cel.Value <> "" Then
            destLbl = Application.VLookup(cel.Value, rgMatch, 2, False)
            If Not IsError(destLbl) Then
                destIndex = Application.Match(destLbl, rgDest, 0)
                If Not IsError(destIndex) Then
                    rgDest.Cells(destIndex, 2).Value = cel.Offset(0, 1).Value
                End If
            End If
        End If
    Next
    
    wbSource.Close SaveChanges:=False
End If


End Sub

Function GetTransactionFile() As Workbook
Dim flPath As String
Dim wb As Workbook
flPath = Application.GetOpenFilename("Excel Files (*.xlsx), *.xlsx", Title:="Pick the transaction file")
If Not flPath = "False" Then
    Set GetTransactionFile = Workbooks.Open(flPath)
End If
End Function

Open in new window

Master-File.xlsm

Author

Commented:
Thanks byundt

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial