HERBERS
Excel-Forum
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Forumbeitrag
Excel-Version des Fragestellers:
2022
Erfahrungslevel des Fragestellers:
VBA nur mit Recorder
tuska
15.07.2024 20:09:56
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
Hallo Daniel,

leider ist mir ein Kopierfehler unterlaufen und die Erläuterungen zum Makro, auskommentiert mit "'" waren NICHT richtig.
(Der zuvor verlautbarte CODE funktioniert nur die Beschreibung stimmte nicht ganz).

Sorry!

Gruß,
Karl

Hier mein endgültiger CODE:

Sub Save_ACTIVE_SHEET_to_DESKTOP()

'
' Save_ACTIVE_SHEET_to_DESKTOP Makro
'

' https://www.herber.de/forum/messages/1984803.html - Topic "Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern"
' https://www.herber.de/forum/messages/1984812.html - Solution from 15 July 2024 - daniel
' -----------------------------------------------------------------------------------------------------------------------------

' ***************************************************** cd %$DESKTOP% *****************************************************
'
' English:
' 1. If the path is confirmed in the input field after the macro has been called up, the Excel file is saved on the desktop.
' 2nd line of the code ... lstrStdPfad = InputBox("Enter ..... format!", , Environ("USERPROFILE") & "\Desktop")
' It is NOT necessary to enter the file extension, e.g. .xlsm! Inverted commas in the path are required!
'
' 2. If the input field is FILLED with a PATH after the macro is called - followed by a BACKSLASH '\',
' then the Excel file is saved in this path. It is NOT necessary to enter the file extension, e.g. .xlsm!
' A path with spaces must NOT be placed in inverted commas, otherwise an error message will be displayed!
' -----------------------------------------------------------------------------------------------------------------------------

' German:
' 1. Wird nach Aufruf des Makros der Pfad im Eingabefeld bestätigt, dann wird die Excel-Datei auf dem Desktop gespeichert.
' 2. Zeile im Code: ... lstrStdPfad = InputBox("Enter ..... format!", , Environ("USERPROFILE") & "\Desktop")
' Die Angabe der Dateierweiterung, zB .xlsm ist NICHT erforderlich! Anführungszeichen im Pfad sind erforderlich!
'
' 2. Wird nach Aufruf des Makros das Eingabefeld mit einem PFAD - abschließend mit einem BACKSLASH '\' BEFÜLLT,
' dann wird die Excel-Datei in diesem Pfad gespeichert. Die Erfassung der Dateierweiterung, zB .xlsm ist NICHT erforderlich!
' Ein Pfad mit Leerzeichen darf NICHT in Anführungszeichen gesetzt werden, ansonsten erfolgt eine Fehlermeldung!
' -----------------------------------------------------------------------------------------------------------------------------

Dim lstrStdPfad As String
lstrStdPfad = InputBox("Enter the path in which the sheet is to be saved - the file name extension (.xl??) is automatically added according to the file format!", , Environ("USERPROFILE") & "\Desktop")
If Mid(Path, 2, 1) <> ":" Then
Path = lstrStdPfad
End If
Select Case Right(Path, 1)
Case ""
GoTo ErrorHandler
Case Is <> "\"
Path = Path & "\"
End Select
ActiveSheet.Copy
On Error GoTo ErrorHandler
' ActiveWorkbook.SaveAs Filename:=path & ActiveSheet.Name => Excel 2003
' ActiveWorkbook.SaveAs Filename:=path & ActiveSheet.Name, FileFormat:=52 => Excel 2010: The file name extension can be omitted,
' https://www.herber.de/forum/archiv/1380to1384/1380461_Excel_2010_per_makro_tabellenblatt_in_neue_Datei.html => Excel adds them automatically according to the file format.
ActiveWorkbook.SaveAs Filename:=Path & ActiveSheet.Name, FileFormat:=52
ActiveWorkbook.Close Savechanges:=False
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 1004
MsgBox ("Saving was NOT successful! - Check the path...")
ActiveWorkbook.Close Savechanges:=False
Case Else
End Select
End Sub
Als Antwort auf diesen Beitrag
tuska
15.07.2024 19:46:47
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
Hallo Daniel,

danke für Deine Beiträge!

Für den Desktop habe ich jetzt Deinen Vorschlag getestet:
Path = InputBox("Enter ... format!", , Environ("USERPROFILE") & "\Desktop")

Dieser Code hat bei mir PERFEKT funktioniert!

Vielen Dank für Deine Unterstützung!

Gruß,
Karl

PS:
Mir stehen jetzt insgesamt sogar drei getestete und perfekt funktionierende Lösungen(!) zur Verfügung:
- Save_ACTIVE_SHEET_to_DESKTOP - daniel
- Save_ACTIVE_SHEET_to_specified_PATH  - Oberschlumpf
- Save_ACTIVE_SHEET_to_PATH_Workbook - Oberschlumpf

Mein finaler Code für: Save_ACTIVE_SHEET_to_DESKTOP

Sub Save_ACTIVE_SHEET_to_DESKTOP()

'
' Save_ACTIVE_SHEET_to_DESKTOP Makro
'

' https://www.herber.de/forum/messages/1984803.html - Topic "Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern"
' https://www.herber.de/forum/messages/1984812.html - Solution from 15 July 2024 - daniel
' -----------------------------------------------------------------------------------------------------------------------------

' English:
' 1. If the path is confirmed in the input field after the macro has been called up, the Excel file is saved on the desktop.
' 2nd line of the code ... lstrStdPfad = InputBox("Enter ..... format!", , Environ("USERPROFILE") & "\Desktop")
' It is NOT necessary to enter the file extension, e.g. .xlsm! Inverted commas in the path are required!
'
' 2. If the input field is FILLED with a PATH after the macro is called - followed by a BACKSLASH '\',
' then the Excel file is saved in this path. It is NOT necessary to enter the file extension, e.g. .xlsm!
' A path with spaces must NOT be placed in inverted commas, otherwise the saved path will be used!
' -----------------------------------------------------------------------------------------------------------------------------

' German:
' 1. Wird nach Aufruf des Makros der Pfad im Eingabefeld bestätigt, dann wird die Excel-Datei auf dem Desktop gespeichert.
' 2. Zeile im Code: ... lstrStdPfad = InputBox("Enter ..... format!", , Environ("USERPROFILE") & "\Desktop")
' Die Angabe der Dateierweiterung, zB .xlsm ist NICHT erforderlich! Anführungszeichen im Pfad sind erforderlich!
'
' 2. Wird nach Aufruf des Makros das Eingabefeld mit einem PFAD - abschließend mit einem BACKSLASH '\' BEFÜLLT,
' dann wird die Excel-Datei in diesem Pfad gespeichert. Die Erfassung der Dateierweiterung, zB .xlsm ist NICHT erforderlich!
' Ein Pfad mit Leerzeichen darf NICHT in Anführungszeichen gesetzt werden, ansonsten erfolgt eine Fehlermeldung!
' -----------------------------------------------------------------------------------------------------------------------------

Dim lstrStdPfad As String
lstrStdPfad = InputBox("Enter the path in which the sheet is to be saved - the file name extension (.xl??) is automatically added according to the file format!", , Environ("USERPROFILE") & "\Desktop")
If Mid(Path, 2, 1) <> ":" Then
Path = lstrStdPfad
End If
Select Case Right(Path, 1)
Case ""
GoTo ErrorHandler
Case Is <> "\"
Path = Path & "\"
End Select
ActiveSheet.Copy
On Error GoTo ErrorHandler
' ActiveWorkbook.SaveAs Filename:=path & ActiveSheet.Name => Excel 2003
' ActiveWorkbook.SaveAs Filename:=path & ActiveSheet.Name, FileFormat:=52 => Excel 2010: The file name extension can be omitted,
' https://www.herber.de/forum/archiv/1380to1384/1380461_Excel_2010_per_makro_tabellenblatt_in_neue_Datei.html => Excel adds them automatically according to the file format.
ActiveWorkbook.SaveAs Filename:=Path & ActiveSheet.Name, FileFormat:=52
ActiveWorkbook.Close Savechanges:=False
Exit Sub
ErrorHandler:
Select Case Err.Number
Case 1004
MsgBox ("Saving was NOT successful! - Check the path...")
ActiveWorkbook.Close Savechanges:=False
Case Else
End Select
End Sub

Folgenachrichten
Antwort auf Beitrag erstellen

Beispieldatei hochladen