ユーザーに作業してもらうメインの Excel ブックがあります。この Excel ブックの機能は、マクロなどを使用して他のブックをコピーして作成することです。このコピーは、誰でもアクセスできるネットワーク ドライブにあります。問題は、誰かがこのバージョンをデスクトップにコピーし、後でこのメイン ブックの新しいバージョンを作成した場合、そのユーザーがデスクトップにコピーした古いブックには、マクロなどの最新の更新が反映されないことです。古いブックでマクロを実行できないようにしたり、古いブックが機能するようにしたりする方法はありますか?
答え1
ステップ1:ネットワーク上の場所にあるメイン ファイルを確認するマクロを作成できます。これを行うにはDir
、またはを使用しますFSO
。
監督:
Sub Test_File_Exist_With_Dir()
Dim FilePath As String
Dim TestStr As String
FilePath = "\\Server\test\book1.xlsm"
TestStr = ""
On Error Resume Next
TestStr = Dir(FilePath)
On Error GoTo 0
If TestStr = "" Then
MsgBox "File doesn't exist"
Else
MsgBox "File exist"
End If
End Sub
FSO:
Sub Test_File_Exist_FSO_Late_binding()
'No need to set a reference if you use Late binding
Dim FSO As Object
Dim FilePath As String
Set FSO = CreateObject("scripting.filesystemobject")
FilePath = "\\Server\test\book1.xlsm"
If FSO.FileExists(FilePath) = False Then
MsgBox "file doesn't exist"
Else
MsgBox "File exist"
End If
End Sub
Sub Test_File_Exist_FSO_Early_binding()
'If you want to use the Intellisense help showing you the properties
'and methods of the objects as you type you can use Early binding.
'Add a reference to "Microsoft Scripting Runtime" in the VBA editor
'(Tools>References)if you want that.
Dim FSO As Scripting.FileSystemObject
Dim FilePath As String
Set FSO = New Scripting.FileSystemObject
FilePath = "\\Server\Ron\test\book1.xlsm"
If FSO.FileExists(FilePath) = False Then
MsgBox "File doesn't exist"
Else
MsgBox "File exist"
End If
End Sub
ステップ2:ファイルの最終更新日をチェックして、新しいバージョンが存在するかどうかを判断できます。
FileDateTime("\\Server\test\book1.xlsm")
サンプル結果:2016年6月1日午後7時40分18秒
ステップ3:新しいバージョンが存在する場合は、ユーザーにメッセージ ボックスを表示して、ネットワーク ドライブから新しいバージョンをコピーし、ワークブックを閉じるように指示できます。(ネットワークの場所からユーザーのワークステーションにコピー/貼り付けを自動化することはお勧めしません。これは簡単に面倒になる可能性があり、これがなくても必要なことは実行されます)
MsgBox "A new version of this file exists on the network share. Please use the new version. This workbook will now close."
ActiveWorkbook.Close savechanges:=False
参考文献: