我在网上找到了一些VBA代码,并根据我的需要进行了修改。我遇到了一个能够改变路径的问题。我的印象是:
Route::auth();
Route::get('/', 'HomeController@index');
Route::get('create', 'MessageController@create');
将回调整个文件名,包括当前保存位置的路径,但是当我运行代码时,它会转到我的/ Documents(而不是保存文件的位置)。有没有办法可以用硬编码路径修改下面的内容?
CurrentFile = ThisWorkbook.FullName
此处结束子代码
答案 0 :(得分:1)
只需对您的代码进行一次小调整或2次修复就可以解决问题。我评论了你的旧代码,这样你就可以看到我改变了什么。您在保存时不想像指定的那样指定文件格式,因为如果您这样做,它将始终提示您更改版本的兼容性问题。保持空白,它将默认为工作表已经存在的版本。您可以在NewFile =之后编辑C:\,无论您需要什么,只需将其保留在引号中。
或者,您可以更改excel的默认保存位置,但这不是VBA修复。
Option Explicit
Sub SaveWorkbookAsNewFile()
Dim ActSheet As Worksheet
Dim ActBook As Workbook
Dim CurrentFile As String
Dim NewFileType As String
Dim NewFile As String
Dim NewFileName As String
NewFileName = "Checklist " & Format(Now, "MMMM-dd-yyyy")
Application.ScreenUpdating = False ' Prevents screen refreshing.
CurrentFile = ThisWorkbook.FullName
'NewFileType = "Excel Files 1997-2003 (*.xls), *.xls," & _
' "Excel Files 2007 (*.xlsx), *.xlsx," & _
' "All files (*.*), *.*"
NewFile = "C:\" & NewFileName
'NewFile = Application.GetSaveAsFilename( _
' InitialFileName:=NewFileName, _
' fileFilter:=NewFileType)
If NewFile <> "" And NewFile <> "False" Then
ActiveWorkbook.SaveAs Filename:=NewFile, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
' ActiveWorkbook.SaveAs Filename:=NewFile, _
' FileFormat:=xlNormal, _
' Password:="", _
' WriteResPassword:="", _
' ReadOnlyRecommended:=False, _
' CreateBackup:=False
Set ActBook = ActiveWorkbook
Workbooks.Open CurrentFile
ActBook.Close
End If
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
If NewFile <> "" And NewFile <> "False" Then
actsheet.SaveAs ("C:/HardcodedLocationHere.xlsx") ' if this fails, actbook
FileFormat:=xlNormal, _
Password:="", _
WriteResPassword:="", _
ReadOnlyRecommended:=False, _
CreateBackup:=False
Set ActBook = ActiveWorkbook
Workbooks.Open CurrentFile
ActBook.Close
结束如果
答案 2 :(得分:0)
当我运行代码时,它会转到我的/ Documents(而不是保存文件的地方)
这是因为您没有为文件提供完全限定(完整路径),您刚刚给出了一个名称,因此它打开了对话框的默认位置\ Documents。
我更喜欢FileDialog
对象而不是Application.GetSaveAsFileName
方法。
Option Explicit
Sub SaveWorkbookAsNewFile()
Dim NewFile As String
Dim NewFileName As String
Dim fdlg as FileDialog
NewFileName = "Checklist " & Format(Now, "MMMM-dd-yyyy")
Application.ScreenUpdating = False ' Prevents screen refreshing.
Set fdlg = Application.FileDialog(msoFileDialogSaveAs)
fdlg.InitialFileName = ThisWorkbook.Path & Application.PathSeparator & NewFileName
fdlg.Show
If fdlg.SelectedItems.Count <> 1 Then GoTo EarlyExit
'# Gets the new file full path & name
NewFile = fdlg.SelectedItems(1)
ThisWorkbook.SaveCopyAs(NewFile)
EarlyExit:
Application.ScreenUpdating = True
End Sub