excel 使用文件名引用文件的上一版本

5lhxktic  于 2022-12-05  发布在  其他
关注(0)|答案(1)|浏览(138)

我正在保存工作簿,并在其旁边加上v[ ],以区分最新版本和较早版本

Workbook v1
Workbook v2
...
Workbook v365

是否有一种方法可以创建一个动态公式来执行以下操作:
1.检测当前版本(365)
1.参照先前版本(Workbook v364)中的特定储存格(例如A2)
请告诉我!任何帮助都将不胜感激。

nvbavucw

nvbavucw1#

由于您没有回答我的澄清问题,下一个代码将假定当前工作簿是活动工作簿,所有版本都存在于同一文件夹中,并且“引用单元格”将是以前版本工作簿中的一个范围,必须从该范围中提取值,该值将在当前工作簿中名为活动工作簿的工作表中找到

Sub referenceThePrevVersion()
 Dim wb As Workbook, curVers As Long, curName As String
 Dim prevVersName As String, refVal As Variant, arrCur
 Const refCell As String = "A2"

 Set wb = ActiveWorkbook 'it my be ThisWorkbook if you need this one where the code exists
 curName = wb.name          'the current workbook name
 arrCur = Split(curName)   'split its name by spaces and place the words in an array
 curVers = CLng(Mid(Split(arrCur(UBound(arrCur)), ".")(0), 2)) 'extract the current version
 prevVersName = VBA.replace(curName, "v" & curVers, "v" & curVers - 1) 'obtain the prev version decreasing a unit
 
 'extract the value of refCell, without opening the previous workbook:
 refVal = CellV(wb.Path & "\", prevVersName, wb.ActiveSheet.name, Range(refCell).address(, , xlR1C1))
 
 MsgBox refVal 'show the extracted value...
End Sub

Private Function CellV(fpath As String, fName As String, SheetName As String, strRange As String) As Variant
   Dim strForm As String
    strForm = "'" & fpath & "[" & fName & "]" & SheetName & "'!" & strRange
    CellV = Application.ExecuteExcel4Macro(strForm)
End Function

提取的值为CellV。它现在显示在消息中,您可以根据需要使用它。
当然,如果您知道以前的版本名称及其路径,则可以打开它并对其数据执行任何所需的操作。
请在测试代码后发送一些反馈。如果有什么不够清楚的地方,不要犹豫,要求澄清。

相关问题