第n週報表內的B2=A2+'[第(n-1)週報表.xlsx]Sheet1'!$B$2
B2內的數值是否有方式能夠自動更新?
檔案名稱格式是固定的,每週會複製前一周的檔案改檔名第(n-1)週報表.xlsx後輸入A2數字,
B2的內容是否能夠自動算出,不用再自己修改連結"第(n-1)週報表.xlsx"
renewRef
並執行即可。Sub renewRef()
Dim thisWbk As String, n As Variant
Dim strPath As String, oldWbk As String
thisWbk = ThisWorkbook.Name
strPath = ThisWorkbook.Path
With CreateObject("vbscript.regexp")
.Global = True
.IgnoreCase = True
.MultiLine = True
.Pattern = "第(\d*)週週報表\.xlsx"
If .test(thisWbk) Then n = .Execute(thisWbk)(0).submatches(0)
End With
If n > 1 Then
oldWbk = Replace(thisWbk, n, n - 1)
If Dir(strPath & "\" & oldWbk) <> "" Then
ThisWorkbook.Worksheets(1).Range("B2").Formula = "=A2+'" & strPath & "\[" & oldWbk & "]Sheet1'!$B$2"
Else
ThisWorkbook.Worksheets(1).Range("B2").Value = "找不到檔案:" & oldWbk
End If
End If
End Sub