' Macro to update changed data to Changed Data Tracking workbook B
Sub MergeData()
Dim cmFile As String
Dim wwb As Workbook
Dim rwb As Workbook
' Disable screen updates to eliminate flashing
Application.ScreenUpdating = False
On Error GoTo errhandler
If IsWorkbookOpen(WORKBOOK_B) Then ' WORKBOOK_B is defined as global constant
' Since already opened, point wwb to opened workbook
Set wwb = Workbooks(WORKBOOK_B)
Else
' Open tracking workbook to write to
Set wwb = Workbooks.Open(TPATH) ' Full pathname for WORKBOOK_B defined as a global constant
End If
wwb.Application.Calculation = xlCalculationManual ' set formula in Tracking workbook to calculate manually
wwb.Application.CalculateBeforeSave = True
' Setup to look for .xlsm workbooks in cost changes directory
cmFile = Dir(CMPATH & "*.xlsm", vbNormal) ' CMPATH is cost changes directory defined as a global constant
If cmFile = "" Then
Res = MsgBox("No workbooks found in cost changes directory. Exiting macro.", vbCritical)
wwb.Close SaveChanges:=False
Exit Sub
End If
FailCount = 0
' Loop until no .xlsm workbooks to process
Do Until cmFile = ""
' Open each .xlsm workbook for processing with password
Set rwb = Workbooks.Open(CMPATH & cmFile, , , , "password123")
' Call procedure to read from .xlsm workbook & update tracking workbook B
Call CopyData(wwb, rwb, cmFile)
' Close each .xlsm workbook without saving
rwb.Close SaveChanges:=False
errhandler:
If Err.Number > 0 Then
Res = MsgBox("Error processing " & cmFile & ": Write down error message & inform macro administrator")
Err.Clear
End If
' Look for next .xlsm workbook
cmFile = Dir
Loop
wwb.Close SaveChanges:=True
Res = MsgBox("Completed processing")
Application.ScreenUpdating = True
End Sub
Bookmarks