Patriot2879
Well-known Member
- Joined
- Feb 1, 2018
- Messages
- 1,259
- Office Version
- 2010
- Platform
- Windows
hi can you help please I have the code below which once updated updates other workbooks as they are all linked together, but for example if book 17 and book 18 is open and someone updates book17 then book18 doesn't update and becomes a read only file and wont save, can you help please?
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim a() As Variant, i As Long, j As Long
Dim sThisFullName As String, sSynchronized As String
Dim Wb As Workbook, IsOpen As Boolean
Dim FullName As Variant, FullNames As Range
If Target.Address <> "$AC$6" Or Target.Value = "" Then Exit Sub
' Determine Row # and Column #
i = Application.Match(Range("AC3"), Range("A1:A18"), 0)
If i = 0 Then MsgBox Range("AC4") & " not found in A1:A18", vbCritical: Exit Sub
j = Application.Match(Range("AC4"), Range("A2:R2"), 0)
If i = 0 Then
MsgBox Range("AC3").Value & " not found in A1:A18", vbCritical
Exit Sub
ElseIf j = 0 Then
MsgBox Range("AC4").Value & " not found in A2:R2", vbCritical
Exit Sub
End If
' Disable events handling, enable auto calculation
Application.EnableEvents = False
Application.Calculation = xlCalculationAutomatic
' Adjust the Intersection cell Value by substracting Input in AC6
Cells(i, j).Value = Cells(i, j).Value - Target.Value
' Clear ONLY Target cell and select it
Target.ClearContents
Target.Select
' Disable blinking
Application.ScreenUpdating = False
' Update A2 current region in synchronized workbooks listed in Sheet2!D2:D...
With Sheet2
Set FullNames = .Range("D2", .Cells(.Rows.Count, "D").End(xlUp))
End With
i = UBound(FullNames.Value) - 1
j = 0
sThisFullName = LCase(ThisWorkbook.FullName)
a() = Me.Range("A2").CurrentRegion.Value
For Each FullName In FullNames.Value
If InStr(FullName, "") > 0 And LCase(FullName) <> sThisFullName Then
j = j + 1
Application.StatusBar = "Updating (" & j & "/" & i & "): " & FullName
On Error Resume Next
Set Wb = Workbooks(Mid(FullName, InStrRev(FullName, "") + 1))
IsOpen = (Err = 0)
On Error GoTo exit_
If Not IsOpen Then
Set Wb = Workbooks.Open(FullName, UpdateLinks:=False)
End If
With Wb
.Sheets(Me.Name).Range("A2").CurrentRegion.Resize(UBound(a), UBound(a, 2)).Value = a()
.Save
If Not IsOpen Then .Close False
End With
sSynchronized = sSynchronized & IIf(j > 1, vbLf, "") & FullName
End If
Next
ThisWorkbook.Activate
exit_:
' Restore events handling, screen updating and status bar
Application.EnableEvents = True
Application.ScreenUpdating = True
Application.StatusBar = False
' Inform about error
If Err Then
MsgBox Err.Description, vbCritical, "Error!"
Else
' Put updating info in the comment of AC6
If Target.Comment Is Nothing Then Target.AddComment
With Target.Comment
.Visible = True
.Text Text:="[Updated " & j & " workbook(s) on " & Now & "]" & vbLf & sSynchronized
.Shape.TextFrame.AutoSize = True
.Shape.TextFrame.AutoSize = False
End With
End If
End Sub
Last edited by a moderator: