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
1条答案
按热度按时间nvbavucw1#
由于您没有回答我的澄清问题,下一个代码将假定当前工作簿是活动工作簿,所有版本都存在于同一文件夹中,并且“引用单元格”将是以前版本工作簿中的一个范围,必须从该范围中提取值,该值将在当前工作簿中名为活动工作簿的工作表中找到:
提取的值为
CellV
。它现在显示在消息中,您可以根据需要使用它。当然,如果您知道以前的版本名称及其路径,则可以打开它并对其数据执行任何所需的操作。
请在测试代码后发送一些反馈。如果有什么不够清楚的地方,不要犹豫,要求澄清。