2016-04-15 12 views
1

zuvor fragte ich eine Frage zum Speichern einer Excel-Datei an einem bestimmten Speicherort mit XLDialogaveAs (die für Dateien funktioniert, die noch gespeichert werden müssen) - Excel VBA XLDialogSaveAs function not working. Ich versuche jedoch, das Gleiche für eine Excel-Datei zu tun, die bereits auf dem Computer gespeichert wurde, aber stattdessen den Speicherort zu ändern.Excel VBA Speichern von Datei im angegebenen Speicherort

Ich habe die folgenden Codes unter:

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 

Kennt jemand die VBA-Funktion übertreffen kann ich den Speicherort der Excel-Datei zu ändern, verwenden und zeigt die bezeichnete Stelle im Dialogfeld vor dem Speichern? Vielen Dank!

+1

Siehe SaveAs wird keine Zeichenfolgen, die enthalten "." In Excel VBA [] (http://stackoverflow.com/questions/36320580/saveas-wont-accpet-strings-that-contain-in-excel-vba/36320966 # 36320966). – Jeeped

+0

Vielen Dank! @Jeeped – JJ2015

Antwort

1

Ich habe es geschafft, dieses Problem mit dem folgenden Code zu lösen.

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 

Vielen Dank!

Verwandte Themen