2016-04-15 41 views
1

以前は、XLDialogaveAsを使用してExcelファイルを指定された場所に保存する方法について質問しました(これはまだ保存されていないファイルでも機能します) - Excel VBA XLDialogSaveAs function not working。しかし、私はすでにコンピュータに保存されているExcelファイルに対して同じことをやろうとしていますが、代わりに場所を変更します。指定された場所にExcel VBAファイルを保存する

I持って下に次のコード:私はExcelファイルの保存場所を変更し、保存する前に、ダイアログボックス内で指定した場所を表示するために使用することができVBA機能を優れ

Option Explicit 

Sub externalRatingChangeFile() 

    'Declare the data type of the variables 
    Dim wks As Worksheet 
    Dim sFilename As String 

    'Set wks to the current active worksheet 
    Set wks = ActiveWorkbook.ActiveSheet 

    'Set the location to save the file to a variable 
    sFilename = "H:\testing file" 

    'Save as .xlsx file in the specific location stated earlier 
    'If there are errors in the code, set wks to nothing and end the process 
    On Error GoTo err_handler 
    ChDrive sFilename 
    ChDir sFilename 
    Application.Dialogs(xlDialogSaveAs).Show (sFilename & "\TestingFile - " & Format(Date, "YYYYMMDD") & ".xlsx") 

    'System to/not display alerts to notify Users that they are replacing an existing file. 
    Application.DisplayAlerts = True 

    err_handler: 
    'Set Wks to its default value 
    Set wks = Nothing 

End Sub 

は誰もが知っていますか?ありがとう!

+1

SaveAsを参照してください。Excel VBA []( "http://stackoverflow.com/questions/36320580/saveas-wont-accpet-strings-that-c​​ontain-in-excel-vba"/36320966#36320966)。 – Jeeped

+0

ありがとう! @ジープ – JJ2015

答えて

1

私はこの問題を以下のコードで解決することができました。

Set fdlg = Application.FileDialog(msoFileDialogSaveAs) 
With fdlg 
    .InitialFileName = sFilename 
    .Show 

'If there are errors in the code, set wks to nothing and end the process 
On Error GoTo err_handler 
    wks.SaveAs (fdlg.SelectedItems(1)) 
End With 

ありがとうございます!

関連する問題