2017-03-05 83 views
0

我想知道是否有(內置/簡單)選項來引用/連接/鏈接到具有變量名稱的工作簿?VBA中的參考名稱更改工作簿

我的xy問題是,我有工作簿b v45.xlsm並希望將數據導出到工作簿v34.xlsm版本號變化。所以我想知道是否每個工作簿都有一個子ID,Excel可以獨立於該名稱進行確認,並自動選擇該文件夾中的最新版本。

當然,最簡單的解決方案是在文件夾路徑中選擇包含字符串「a v」的最近修改的excel文件,假設文件夾路徑相同,但我很好奇是否有更方便/集成的選項。

親切的問候。

(對於未來的人看這個問題,這裏是我的手動解決方案:)

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 
+3

對此沒有內置功能 - 您需要按照您所描述的方式根據文件名進行操作。 –

回答

1

它應該是更可靠的解析文件名,看版本號,而不是着眼於最近修改的文件。遍歷所有這些檢查與類似的文件名:

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資源管理器重命名的完全相同的文件,或者當您使用另存爲...時創建的同一文件夾中是否有多個版本? 「自動選擇最新版本」的問題表明,在同一文件夾中創建了新版本。如果是這樣,這意味着您實際上正在更改要鏈接的工作簿,因此,任何形式的文件鏈接都無法正常工作。另外,即使您輸入了子ID,每個版本仍將具有相同的子ID。雖然這仍然可以識別同一文件的不同版本的文件,但您仍然需要遍歷所有這些文件以查找最新版本。如果文件名完全更改,子標識將有所幫助,但不會刪除搜索不同版本的需要。因此,如果您只需更改版本號就可以保持一致的文件名,則可以實現最簡單的解決方案。

+0

謝謝,基本名稱必須保持不變,只更改版本號(在同一文件夾中有多個版本)。我想知道是否有這樣的系統是爲這樣的問題設計的,因爲當事情變得更加複雜時,即多個用戶或不一致的文件名/其他依賴關係。 (據我所知,如果要引用多個不同的.xlsm文件,那麼sub-ID仍然會導致錯誤,但也許我通過建議介紹了有關此主題的問題)。但手動解決方案將是。感謝您的闡述! –