、あなたが含むワークブックのコピーを貼り付けます各人物のフォルダに上記のコード。彼らはあなたがそれのように自身の名前を変更したいブックを開くとき:
< <人物名>> _Manual偵察< < MM.DD.YY >>の.xlsx
を私はあなたが左に元のファイルをしたいと仮定しますそのファイルを開いて翌日の新しいxlsxを作成することはできますが、すでに存在する場合はファイルを作成しません(xlsmを1日に2回開く場合)。
もう1つのポイントは、名前が付けられた個人用フォルダですか?
など。 G:\MMS Trade Payables\John
あなたのコードでは、変数thisWb
をActiveWorkbook
に設定しました。
あなただけのいつものコードが実行されているワークブックを参照するThisWorkbook
を使用することができます
だから、これらの仮定で、このコードを試してみてください。
Sub Name_And_Save_Report()
Dim fName As String
Dim sNewFile As String
'Get the folder name.
fName = GetParentFolder(ThisWorkbook.Path)
'Could also get the Windows user name.
'fName = Environ("username")
'Or could get the Excel user name.
'fname = application.username
'Or could just ask them.
'fname = InputBox("Enter your name (example-John):")
sNewFile = ThisWorkbook.Path & Application.PathSeparator & _
fName & "_Manual Recon " & Format(Date, "mm.dd.yy") & ".xlsx"
If Not FileExists(sNewFile) Then
'Turn off alerts otherwise you'll get
'"The following features cannot be saved in macro-free workbooks...."
'51 in the SaveAs means save in XLSX format.
Application.DisplayAlerts = False
ThisWorkbook.SaveAs sNewFile, 51
Application.DisplayAlerts = True
End If
End Sub
Public Function FileExists(ByVal FileName As String) As Boolean
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
FileExists = oFSO.FileExists(FileName)
Set oFSO = Nothing
End Function
Public Function GetParentFolder(ByVal FilePath As String) As String
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
GetParentFolder = oFSO.GetFolder(FilePath).Name
Set oFSO = Nothing
End Function
私はここにこれを残しておきます私の最初の答えとして:
これはどういう意味ですか?
FileSystemObject
を使用して、再帰的に親フォルダ名を取得します。
Sub Test()
MsgBox ThisWorkbook.Path & vbCr & RelativePath(ThisWorkbook.Path, 2)
'Will return "C:\Users\e6y550m" - step back 2 folders.
MsgBox RelativePath("C:\Users\e6y550m\Documents\MANUAL RECS\", 2)
'Your line of code:
'ActiveWorkbook.SaveAs FileName:=RelativePath(thisWb.Path, 2) & "\" & fname & "_Manual Recon" & " " & Format(Date, "MM.DD.YY") & ".xlsx"
End Sub
'FilePath - path to file, not including file name.
'GetParent - the number of folders in the path to go back to.
Public Function RelativePath(FilePath As String, Optional GetParent As Long) As String
Dim oFSO As Object
Set oFSO = CreateObject("Scripting.FileSystemObject")
'If rightmost character is "\" then we've reached the root: C:\
If GetParent = 0 Or Right(FilePath, 1) = Application.PathSeparator Then
RelativePath = oFSO.GetFolder(FilePath)
'If we've reached the root then remove the "\".
If Right(RelativePath, 1) = Application.PathSeparator Then
RelativePath = Left(RelativePath, Len(RelativePath) - 1)
End If
Else
'GetParent is greater than 0 so call the RelativePath function again with
'GetParent decreased by 1.
RelativePath = RelativePath(oFSO.GetParentFolderName(FilePath), GetParent - 1)
End If
Set oFSO = Nothing
End Function
"相対パス"?何に対して? (あるいは、あなたは共有ディレクトリをどこかに指定していますか?)明らかに答えは 'thisWb.Path'をあなたが望むパスで置き換えることですが、それが何であるかを示唆する質問は何もないので、難しいでしょう助けます。 – YowE3K