Sorry my fault, wrong path name.
Its not retrieving anything now. I think its me. I have changed the path and it is not debugging but it is not retrieving.
Have I done something.
Sub UpdateData()
Dim wsIf1 As Worksheet: Dim wsIf2 As Worksheet
Dim wsCap As Worksheet: Dim wsMAT As Worksheet
Dim wbCap As Workbook: Dim wbMAT As Workbook
Dim cIf1(40) As Variant: Dim cCap(40) As Variant: Dim cMAT(40) As Variant
Dim rIf1 As Range: Dim rMAT As Range: Dim rCap As Range
Set wsIf1 = Worksheets("Sheet1")
Set wsIf2 = Worksheets("Sheet2")
'You need to change this to the relevant path for the CAP and MAT files
CAP = "D:\Documents and Settings\Olivia-Trimble\Desktop\CAP sample.xls"
MAT = "D:\Documents and Settings\Olivia-Trimble\Desktop\03-01 MATS agreements sample.xls"
Application.ScreenUpdating = False
Workbooks.Open CAP
Set wbCap = ActiveWorkbook
Set wsCap = ActiveSheet
ActiveWindow.Visible = False
Workbooks.Open MAT
Set wbMAT = ActiveWorkbook
Set wsMAT = ActiveSheet
ActiveWindow.Visible = False
If1Col = wsIf1.UsedRange.Columns.Count
'Determine the column matches between the two sheets (IF and CAP)
a = 1
For x = 1 To If1Col
For y = 1 To If1Col
If wsCap.Cells(1, x) = wsIf1.Cells(5, y) Then
cCap(a) = wsCap.Cells(1, x).Column
cIf1(a) = wsIf1.Cells(1, y).Column
a = a + 1
Exit For
End If
Next y
Next x
'Update Sheet1 of the IF workbook with the matching data from CAP workbook
For Each rIf1 In wsIf1.Range("D5:D" & wsIf1.UsedRange.Rows.Count)
If rIf1.Value <> "" Then
For Each rCap In wsCap.Range("C2:C" & wsCap.UsedRange.Rows.Count)
If rCap = rIf1 Then
For z = 1 To a - 1
wsIf1.Cells(rIf1.Row, cIf1(z)) = wsCap.Cells(rCap.Row, cCap(z))
Next z
Exit For
End If
Next rCap
End If
Next rIf1
'Determine the column matches between the two sheets (IF and MAT)
a = 1
For x = 1 To If1Col
For y = 1 To If1Col
If wsIf1.Cells(5, y) <> "" Then
If wsMAT.Cells(1, x) = wsIf1.Cells(5, y) Then
cMAT(a) = wsMAT.Cells(1, x).Column
cIf1(a) = wsIf1.Cells(1, y).Column
a = a + 1
Exit For
End If
End If
Next y
Next x
'Update Sheet1 of the IF workbook with the matching data from MAT workbook
For Each rIf1 In wsIf1.Range("D5:D" & wsIf1.UsedRange.Rows.Count)
If rIf1.Value <> "" Then
For Each rMAT In wsMAT.Range("A2:A" & wsMAT.UsedRange.Rows.Count)
If rMAT = rIf1 Then
For z = 1 To a - 1
wsIf1.Cells(rIf1.Row, cIf1(z)) = wsMAT.Cells(rMAT.Row, cMAT(z))
Next z
Exit For
End If
Next rMAT
End If
Next rIf1
wbCap.Close False
wbMAT.Close False
UpdateSheet
End Sub
Libby
Bookmarks