VBA中的参考名称更改工作簿

我想知道是否有一个(内置/简单)选项来引用/连接/链接到一个variables名称的工作簿?

我的xy问题是,我有工作簿b v45.xlsm,并希望将数据导出到工作簿v34.xlsm版本号变化。 所以我想知道是否有每个工作簿的子标识,Excel可以独立于名称进行标识,自动select该文件夹中的最新版本。

当然,最简单的解决方法是,在文件夹path中select包含string“av”的最近修改的excel文件,假定文件夹path相同,但是我很好奇是否有更方便/集成的选项。

亲切的问候。

(对于未来的人来看这个问题,这里是我的手动解决scheme:)

Sub find_planner_name() Dim objFSO As Object Dim objFolder As Object Dim objFile As Object Dim i As Integer Dim string_object(0 To 2) As String 'saving the filenames as strings Dim count As Integer 'counting nr of files encountered Dim save_version_number(0 To 1) As Long 'Create an instance of the FileSystemObject Set objFSO = CreateObject("Scripting.FileSystemObject") 'Get the folder object Set objFolder = objFSO.GetFolder(ThisWorkbook.Path) i = 1 'loops through each file in the directory and prints their names and path For Each objFile In objFolder.Files 'print file name 'Cells(i + 1, 1) = objFile.name count = count + 1 ReDim version_number(0 To count) As Long string_object(0) = "" string_object(1) = "" string_object(2) = "" string_object(0) = objFile.name If Right(string_object(0), 5) = ".xlsm" Or Right(string_object(0), 5) = ".xlsb" Then If Left(string_object(0), 10) = " planner v" Or Left(string_object(0), 10) = " planner v" Then string_object(1) = Right(string_object(0), Len(string_object(0)) - 10) MsgBox (string_object(1)) Do While IsNumeric(Left(string_object(1), 1)) = True If IsNumeric(Left(string_object(1), 1)) = True Then string_object(2) = string_object(2) & Left(string_object(1), 1) string_object(1) = Right(string_object(1), Len(string_object(1)) - 1) End If Loop If version_number(count) < string_object(2) And string_object(2) > 0 Then version_number(count) = string_object(2) MsgBox (version_number(count)) save_version_number(0) = version_number(count) save_version_number(1) = count End If End If End If i = i + 1 Next objFile count = save_version_number(1) 'rewrite maxima back version_number(count) = save_version_number(0) 'rewrite maxima back 'MsgBox ("done " & version_number(count)) Dim myMax As Long Dim count_results As Long For count_results = LBound(version_number, 1) To UBound(version_number, 1) If version_number(count_results) > myMax Then myMax = version_number(count_results) Findmax = count_results 'MsgBox (version_number(count_results)) End If 'MsgBox (version_number(count_results) & " and count_results = " & count_results) Next count_results 'the name of the planner = name_planner = " planner v" & version_number(Findmax) & ".xlsm" ' check if xlsm or xlsb 'MsgBox (name_planner) If Dir(ThisWorkbook.Path & "\" & name_planner) <> "" Then MsgBox ("File exists. and name is " & name_planner) Else name_planner = " planner v" & version_number(Findmax) & ".xlsb" End If End Sub 

分析文件名查看版本号而不是查看最近修改的文件应该更可靠。 循环浏览所有的文件,检查文件名如下:

 strFile = Dir(DirectoryPath) Do while strFile <> "" 'Parse strFile for intNewVersionNumber if intNewVersionNumber > intVersionNumber then intVersion Number = intNewVersionNumber strFile = Dir Loop strFile = 'Reconstruct filename from intVersionNumber 

从你的问题,我认为这可能实际上是必要的,即使可能有几种添加/检查Excel文件的元数据的方法。

当您说工作簿名称更改时,它实际上是完全相同的文件正在通过Windows资源pipe理器重命名,或者当您使用另存为…创build相同的文件夹中有多个版本吗? “自动select最新版本”的问题表明,在同一文件夹中创build了新版本。 如果是这样,这意味着你实际上正在改变你链接的工作簿,所以任何forms的文件链接都不会起作用。 另外,即使您input了一个子ID,每个版本仍然会有相同的子ID。 虽然这仍然可以识别同一文件的不同版本的文件,但您仍然需要遍历所有这些文件以查找最新版本。 如果文件名完全更改,则子标识将有所帮助,但不会删除search不同版本的需要。 所以,如果你可以保持一致的文件名,只改变版本号,你就可以实现最简单的解决scheme。