2016-07-12 225 views
0

我在網上發現了一些VBA代碼,並對我需要的內容進行了修改。我遇到了能夠改變路徑的一個問題。我的印象是:硬編碼VBA SaveAs路徑?

CurrentFile = ThisWorkbook.FullName 

會再打完整的文件名,包括路徑到當前保存,但是當我運行是不言而喻的代碼到我的/文件(未在該文件被保存)。有沒有辦法可以用硬編碼路徑修改下面的內容?

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 = Application.GetSaveAsFilename(_ 
    InitialFileName:=NewFileName, _ 
    fileFilter:=NewFileType) 

If NewFile <> "" And NewFile <> "False" Then 
    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 

末次在這裏代碼

回答

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

結束如果

+0

不喜歡它。你有沒有看到爲什麼這不會抓住當前文件路徑的任何原因?如果我能夠解決這個問題,那將會很棒 – user2679225

+0

@ user2679225看看編輯。 – BigElittles

0

當我運行是不言而喻的代碼到我的/文件(未在該文件被保存)

這是因爲你沒有提供一個完全合格的(完整路徑)到文件中,您剛剛給出了一個名稱,所以它打開了默認位置爲\ 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