如何在Excel/VBA 中向字符串(文件)添加增量计数(版本)?

How to add an incremental count (version) to a string (file) in Excel/VBA?

我尝试了很多不同的方法,但似乎无法正常工作。所以基本上,这是我完整代码的一小部分。
我正在使用 Microsoft Scripting Runtime 保存文件,使用 FileExists() 在保存之前检查文件是否确实存在。
如果我删除 IF-statement/Loop.
就可以正常工作 但是,现在感觉 FileExists 找不到字符串 MyFilePath,而我 运行 使用 IF/Loop。 (getdirsubparentpath 是一个函数)

Dim week, UserName As String
Dim MyFile, MyFilePath As String
Dim version As Integer


' Current week, XX
week = Format(Date, "ww")
' Username, e.g. niclas.madsen
UserName = Environ$("UserName")
' Initials, first letter of last and surname to caps
' e.g. niclas.madsen would be NM
UserName = UCase(Left(UserName, 1) & Mid(UserName, InStr(UserName, ".") + 1, 1))

' fix filename for saving purpose
MyFile = Replace(Replace("SupplierOrganization_W", "", ""), ".", "_") _
        & "" _
        & week _
        & " " _
        & UserName _
        & ".csv"
'SupplierOrganization_WXX NM

MyFilePath = getDirSubParentPath & MyFile

' Look for the MyFilePath, if it exists then
' Add "-1" after the week number, if 1 exists, add 2, etc.
If Len(Dir(MyFilePath)) <> 0 Then
version = 0
Do
version = version + 1
MyFilePath = Dir(getDirSubParentPath & "SupplierOrganization_W" & week & "-" & version & " " & UserName & ".csv")
Loop Until Len(Dir(MyFilePath)) < 0
End If

Dim tmpFile, tmpFilePath As String
tmpFile = getDirSubParentPath & "tmp_file.txt"


Dim tmpString As String
'Dim fso As New FileSystemObject


Dim fso As Object 'scripting.filesystemobject
Set fso = CreateObject("scripting.filesystemobject")

If fso.FileExists(MyFilePath) = True Then
    Application.ScreenUpdating = False
    Open MyFilePath For Input As #1
    Open tmpFile For Output As #2
    tmpString = Input(LOF(1), 1) 'read the entire file
    tmpString = Replace(tmpString, (Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
    & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) _
    & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) _
    & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
    & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) _
    & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) _
    & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
    & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) _
    & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) _
    & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
    & Chr(34)), "") 'eliminate double quotation and commas in the first line with UTF-8
    Print #2, tmpString 'output result
    Close #1
    Close #2
    fso.DeleteFile (MyFilePath) 'delete original file
    fso.CopyFile tmpFile, MyFilePath, True 'rename temp file
    fso.DeleteFile (tmpFile) 'delete temp file
    Application.ScreenUpdating = True
    MsgBox "Finished processing file", vbInformation, "Done!"
Else
    MsgBox "Cannot locate the file : " & MyFilePath, vbCritical, "Error"
End If
Set fso = Nothing
End Sub




' Get Parent Sub Directory Path
Function getDirSubParentPath()
getDirSubParentPath = ThisWorkbook.Path & Application.PathSeparator & "CSV" & Application.PathSeparator & "Parent" & Application.PathSeparator
End Function

我终于设法创建了一个看起来可行的解决方案。但是,代码可以使用一些清理 :) 但它完成了工作。
所以基本上,我在循环方面遇到了一些问题。它将 return 一个名为 W16-0 的文件(实际上应该是 W16)。如果找到 W16,它应该只添加“-X”。所以递增顺序应该是W16、W16-1、W16-2等
我正在做的是尝试定位是否有 W16-0,然后将其替换为 W16。此外,循环似乎会给我一个比我拥有的文件数量更高的数量。所以这也是我遇到错误的地方。所以如果我有一个 W16-4,它会要求宏找到并打开一个名为 W16-5 的文件,这个文件显然不存在。
如果有人能帮我清理代码,我将不胜感激!

Sub RemoveCommasDoubleQ()
'
'    Enable a reference to 'Microsft Scripting Runtime'
'    under VBA menu option Tools > References

Dim week, UserName As String
Dim MyFile, MyFilePath As String
Dim version As Integer

Dim fso As Object 'scripting.filesystemobject
Set fso = CreateObject("scripting.filesystemobject")

' Current week, XX
week = Format(Date, "ww")
' Username, e.g. niclas.madsen
UserName = Environ$("UserName")
' Initials, first letter of last and surname to caps
' e.g. niclas.madsen would be NM
UserName = UCase(Left(UserName, 1) & Mid(UserName, InStr(UserName, ".") + 1, 1))

' fix filename for saving purpose
MyFile = Replace(Replace("SupplierOrganization_W", "", ""), ".", "_") _
            & "" _
            & week _
            & " " _
            & UserName _
            & ".csv"
    'SupplierOrganization_WXX NM

'MyFilePath = ThisWorkbook.Path & "\CSV\Parent\" & MyFile
MyFilePath = getDirSubParentPath & MyFile

Debug.Print MyFilePath
Debug.Print "BEFORE LOOP"
'version = 1

Do While Len(Dir(MyFilePath)) <> 0
     '// If it does, then append a _000 to the name
     '// Change _000 to suit your requirement
    MyFilePath = getDirSubParentPath & "SupplierOrganization_W" & week & "-" & version & " " & UserName & ".csv"

     '// Increment the counter
    version = version + 1

     '// and go around again

    If MyFilePath = getDirSubParentPath & "SupplierOrganization_W" & week & "-0" & " " & UserName & ".csv" Then
       MyFilePath = getDirSubParentPath & "SupplierOrganization_W" & week & " " & UserName & ".csv"
       Debug.Print MyFilePath
       Debug.Print "IF LOOP"
    End If
Loop
Debug.Print MyFilePath
Debug.Print "LOOP"

If fso.FileExists(getDirSubParentPath & "SupplierOrganization_W" & week & "-" & version & " " & UserName & ".csv") = False Then
    MyFilePath = getDirSubParentPath & "SupplierOrganization_W" & week & "-" & version - 2 & " " & UserName & ".csv"
    MsgBox getDirSubParentPath & "SupplierOrganization_W" & week & "-" & version & " " & UserName & ".csv"
End If

fileName = fso.GetFileName(MyFilePath)
Debug.Print fileName

If MyFilePath = getDirSubParentPath & "SupplierOrganization_W" & week & "-0" & " " & UserName & ".csv" Then
   MyFilePath = getDirSubParentPath & "SupplierOrganization_W" & week & " " & UserName & ".csv"
   Debug.Print MyFilePath
   Debug.Print "her it should be 0"
End If

If MyFilePath = getDirSubParentPath & "SupplierOrganization_W" & week & "-" & " " & UserName & ".csv" Then
   MyFilePath = getDirSubParentPath & "SupplierOrganization_W" & week & "-" & version & " " & UserName & ".csv"
End If



Debug.Print "HER ER VI"
fileName = fso.GetFileName(MyFilePath)
Debug.Print fileName


Dim tmpFile, tmpFilePath As String
tmpFile = getDirSubParentPath & "tmp_file.txt"

Dim tmpString As String

Debug.Print "------"
Debug.Print MyFilePath

If fso.FileExists(getDirSubParentPath & "SupplierOrganization_W" & week & "-0" & " " & UserName & ".csv") = True Then
   MsgBox "Found the W-0"
   MyFilePath = getDirSubParentPath & "SupplierOrganization_W" & week & " " & UserName & ".csv"
End If

Debug.Print "Found 0?"
Debug.Print MyFilePath


If fso.FileExists(MyFilePath) = True Then
    Application.ScreenUpdating = False
    Open MyFilePath For Input As #1
    Open tmpFile For Output As #2
    tmpString = Input(LOF(1), 1) 'read the entire file
    tmpString = Replace(tmpString, (Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
    & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) _
    & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) _
    & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
    & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) _
    & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) _
    & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
    & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) _
    & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) _
    & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) & Chr(34) & Chr(44) & Chr(34) _
    & Chr(34)), "") 'eliminate double quotation and commas in the first line with UTF-8
    Print #2, tmpString 'output result
    Close #1
    Close #2
    fso.DeleteFile (MyFilePath) 'delete original file
    fso.CopyFile tmpFile, MyFilePath, True 'rename temp file
    fso.DeleteFile (tmpFile) 'delete temp file
    Application.ScreenUpdating = True
    MsgBox "Finished processing file", vbInformation, "Done!"
Else
    MsgBox "Cannot locate the file : " & MyFile, vbCritical, "Error"
End If
Set fso = Nothing
End Sub