Anzeige
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender

Forumthread: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern

Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 14:03:59
tuska
Hallo,

Ich bitte um Prüfung des Makros "Save_ACTIVE_SHEET_to_specified_PATH".
Grundsätzlich funktioniert das Makro wenn ich den Pfad manuell in das Eingabefeld eintrage.

Mein Problem (bin nur Hobbyanwender und verfüge über keine Programmierkenntnisse):

In der Zeile ... Path = Path & "\" kann ich zwar Pfade zB wie folgt definieren
Path = C:\Everything\Tests\
Path = "D:\Test images\"

Nach Aufruf des Makros werden diese Pfade jedoch nicht berücksichtigt:
- Lasse ich den Pfad leer (Pfad im Makro eingestellt), dann wird das Arbeitsblatt NICHT gespeichert.
- Erwarten würde ich mir als Laie, dass nach Aufruf des Makros das Eingabefeld bereits automatisch mit dem Pfad befüllt ist.

Bitte um Unterstützung.
Danke im voraus für die Bemühungen!

PS:
Ich habe Excel-Version 2021 im Dropdown-Feld ausgewählt es wird jedoch "Excel-Version des Fragestellers: 2022" angezeigt
wenn ich den Beitrag mit Klick auf den Button "Beitrag Vorschau" anzeigen lasse.

Sub Save_ACTIVE_SHEET_to_specified_PATH()

'
' German: In der unten angeführten Zeile -> Path = Path & "\" - sollten Sie den Standardpfad anpassen können,
' also "C:\bak\" beispielsweise durch einen Netzwerkpfad ersetzen. Bei mir wurde der Pfad nicht automatisch vorgeschlagen
' und ich mußte den gewünschten Pfad manuell einsetzen.
'
' English: In the line below -> Path = Path & "\" - you should be able to adjust the default path,
' for example, replace "C:\bak\" with a network path. In my case, the path was not suggested automatically
' and I had to enter the desired path manually.
'
Path = 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!")
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,
' http://www.herber.de/forum/archiv/1380to1384/t1380461.htm 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
Anzeige

16
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 14:34:36
daniel
Hi

momentan hast du programmiert, dass zur Sprungmarke ErrorHandler: gesprungen wird, wenn du keinen Pfad eingibst:
Case ""

Goto ErrorHandler


hier könntest du einestellen, dass wenn du nichts eingegeben hast, hier der Pfad der Datei eingestellt wird:
Case ""

Path = ActiveWorkbook.path & "\"


das würde ich aber nicht machen, da du damit die Möglichkeit nimmst, die ganze Aktion abzubrechen.
wenn der aktuelle Pfad voreingestellt werden soll, dann kannst du das in der Inputbox machen:
Path = InputBox("Enter ... file format!", , ActiveWorkbook.Path)

Gruß Daniel

Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 14:43:02
daniel
wenn du automatisch den Desktop voreingestellt haben willst:

Path = InputBox("Enter ... format!", , Environ("USERPROFILE") & "\Desktop")

Gruß Daniel
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 19:46:47
tuska
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

Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 20:09:56
tuska
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
Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 15:01:37
tuska
Hallo Daniel,

Danke.

Mit Deinem Code erreiche ich aber nur das was ich zuvor schon geschrieben habe.

Ich habe geschrieben:
"Grundsätzlich funktioniert das Makro wenn ich den Pfad manuell in das Eingabefeld eintrage."
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 15:39:01
daniel
Hi
du hattest geschrieben:

- Erwarten würde ich mir als Laie, dass nach Aufruf des Makros das Eingabefeld bereits automatisch mit dem Pfad befüllt ist.

das machen meine Makros.
wie du es anstellen kannst, dass Path befüllt wird wenn du das Eingabefeld leer lässt, hatte ich auch beschrieben.
Allerdings dazu geschrieben, warum ich diese Option nicht verwenden würde.
Da kannst du dich natürlich drüber hinwegsetzen und andere Dinge eintragen:

also anstelle von:

        Case ""

GoTo ErrorHandler


dann
        Case ""

Path = "C:\Everything\Tests\"


oder was auch immer.
ActiveWorkbook.Path ist der Pfad der gerade aktiven Datei
ThisWorkbook.Path ist der Pfad der Datei, die das Makro enthält.
Wie du den Desktop als speicherort einstellen kannst, habe ich dir in meiner zweiten antwort gezeigt.

Gruß Daniel
Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 14:38:52
ralf_b
ersetze die Zeile mit dem Path= inputbox durch diese beiden Zeilen.
 


Dim msg$: msg = "Enter the path in which the sheet is to be saved - the file name extension (.xl??) is automatically added according to the file format!"
Path = InputBox(msg, "Choose Filepath", ActiveWorkbook.Path)


die datei wird übrigens dort gespeichert wenn der Pfad existiert. Wird die inputbox ohne Pfad angezeigt, dann ist die aktive Mappe eine neu erstellte Datei, die noch nicht gespeichert wurde und somit keinen Pfad hat.
Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 15:16:34
tuska
Hallo ralf_b,

Danke!

Mit diesem Code habe ich eine neue Variante kennengelernt, die mir sehr gefällt:
Als Vorschlag erscheint der PFAD in dem sich die EXCEL-DATEI mit dem Makro befindet.
Diese Variante werde ich mir speichern.

Gewünscht habe ich mir folgendes:
Es soll der PFAD als Vorschlag erscheinen, den ich IM MAKRO GESPEICHERT habe, zB
Path = "C:\Everything\Tests\"

Könntest Du bitte diese Variante ebenfalls prüfen.
Danke!
Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 22:35:29
tuska
Hallo ralf_b,

Vielen Dank für Deine Hilfe!

Wie schon erwähnt verfüge ich über keine Programmierkenntnisse.
Schlußendlich ist es mir aber doch aufgrund Deiner Unterstützung gelungen, das Makro wie folgt fertigzustellen!

Danke nochmals.

Gruß,
Karl

Mein finaler Code für: Save_ACTIVE_SHEET_to_PATH_Workbook

Sub Save_ACTIVE_SHEET_to_PATH_Workbook()

'
' Save_ACTIVE_SHEET_to_PATH_Workbook 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/1984810.html - Solution from 15 July 2024 - ralf_b

' -----------------------------------------------------------------------------------------------------------------------------
' English:
' 1. The default path can be customised in the line below -> Path = Path & "\".
' Example (complete line): Path = "C:\Everything\Tests\"
' After calling up the macro, the self-defined path is already suggested in the input field.
' After confirming by clicking on the "OK" button, the file is saved in the specified path.
'
' 2. If, after calling the macro, the input field is overwritten with a self-defined PATH - 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 file will be saved in the defined default path!
'
' 3. If the content of the input field is [accidentally] DELETED after the macro is called up, NO file is saved.

' -----------------------------------------------------------------------------------------------------------------------------
' German:
' 1. Der Standardpfad kann in der unten angeführten Zeile -> Path = Path & "\" angepasst werden.
' Beispiel (komplette Zeile): Path = "C:\Everything\Tests\"
' Nach Aufruf des Makros wird der selbst definierte Pfad im Eingabefeld bereits vorgeschlagen.
' Nach Bestätigung mit Klick auf den Button "OK" wird die Datei im angegebenen Pfad gespeichert.
'
' 2. Wird nach Aufruf des Makros das Eingabefeld mit einem selbst definierten PFAD - abschließend mit einem BACKSLASH '\' - ÜBERSCHRIEBEN,
' 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 wird die Datei in dem definierten Standardpfad gespeichert!
'
' 3. Wird nach Aufruf des Makros der Inhalt vom Eingabefeld [versehentlich] GELÖSCHT, dann wird KEINE Datei gespeichert.
' -----------------------------------------------------------------------------------------------------------------------------

Dim msg$: msg = "Enter the path in which the sheet is to be saved - the file name extension (.xl??) is automatically added according to the file format!"
Path = InputBox(msg, "Choose Filepath", ActiveWorkbook.Path)
Select Case Right(Path, 1)
Case ""
GoTo ErrorHandler
Case Is > "\"
' Path = Path & "\" ... insert the desired [network] path in the next line | German: Füge den gewünschten [Netzwerk-]Pfad in der nächsten Zeile ein
Path = "C:\Everything\Tests\"
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

Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 15:41:07
ralf_b
du musst doch nur ActiveWorkbook.Path durch Path ersetzen. wobei du path ja erst mit der inputbox füllst in deinem Makro. Also welche Variable hast du denn "vor"-belegt und wo ???
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 16:08:45
tuska
ralf_b,

> du musst doch nur ActiveWorkbook.Path durch Path ersetzen. wobei du path ja erst mit der inputbox füllst in deinem Makro.
Dann erhalte ich ein leeres Eingabefeld.

> Also welche Variable hast du denn "vor"-belegt und wo ???

Ich schrieb:
"...bin nur Hobbyanwender und verfüge über keine Programmierkenntnisse"

Dies ist mein derzeitiger CODE welcher so NICHT funktioniert:

Sub Save_ACTIVE_SHEET_to_specified_PATH()

'
Dim msg$: msg = "Enter the path in which the sheet is to be saved - the file name extension (.xl??) is automatically added according to the file format!"
Path = InputBox(msg, "Choose Filepath", Path)
Select Case Right(Path, 1)
Case ""
GoTo ErrorHandler
Case Is > "\"
Path = "C:\Everything\Tests\"
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 Vorschlag soll dieser Pfad kommen:
Path = "C:\Everything\Tests\"
Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 16:18:51
daniel
das hatte ich dir doch geschrieben:
wenn du es als Vorschlag haben willst, gib es als dritten Parameter der Inputbox an.
Path = InputBox(msg, "Choose Filepath", "C:\Everything\Tests\")


Gruß Daniel
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 15:49:14
Oberschlumpf
Hi,

und bitte WO IM gezeigten MAKRO hast du den gewünschten Pfad GESPEICHERT?
(oder seh wirklich nur ich das nicht?)

Versuch es so:
änder diesen Code...


Path = 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!")

...um in...


Dim lstrStdPfad As String
lstrStdPfad = "C:\Everything\Tests\"
Path = 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!")
If Mid(Path, 2, 1) > ":" Then
Path = lstrStdPfad
End If


Ciao
Thorsten
Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 17:05:46
tuska
Hallo Oberschlumpf,

Aufgrund Deines Beitrages konnte ich das Thema wie folgt lösen - siehe Kommentare (englisch/deutsch) im CODE.

Vielen Dank, dass Du Dich ebenfalls dieses Themas angenommen hast!
Mein Dank gilt auch an die anderen Profis hier im Forum für die Bemühungen.

Sub Save_ACTIVE_SHEET_to_specified_PATH()


' https://www.herber.de/forum/messages/1984803.html - Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
' -----------------------------------------------------------------------------------------------------------------------------

' English:
' 1. If the input field is left EMPTY after the macro is called, the Excel file is saved in the PATH,
' which was defined in the MACRO. EXAMPLE: Path = "C:\Everything\Tests\"
' It is NOT necessary to enter the file extension, e.g. .xlsm!
'
' 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!
' -----------------------------------------------------------------------------------------------------------------------------

' German:
' 1. Wird nach Aufruf des Makros das Eingabefeld LEER gelassen, dann wird die Excel-Datei in jenem PFAD gespeichert,
' welcher im MAKRO definiert wurde. BEISPIEL: Path = "C:\Everything\Tests\"
' Die Angabe der Dateierweiterung, zB .xlsm ist NICHT 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!
' -----------------------------------------------------------------------------------------------------------------------------

Dim lstrStdPfad As String
lstrStdPfad = "C:\Everything\Tests\"
Path = 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!")
If Mid(Path, 2, 1) > ":" Then
Path = lstrStdPfad
End If
Select Case Right(Path, 1)
Case ""
GoTo ErrorHandler
Case Is > "\"
Path = "C:\Everything\Tests\"
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
Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 17:17:17
Oberschlumpf
Hi,

schön, freut mich, dass ich helfen konnte.

Eine kleine Verbesserung in deinem jetzigen Code hab ich noch:

änder diesen Code...


Case Is > "\"
Path = "C:\Everything\Tests\"

...um in...


Case Is > "\"
Path = Path & "\"


Denn...angenommen, du hast in der Inputbox wirklich einen gültigen Pfad eingetragen, z Bsp "D:\muss hier hin" der nun aber ganz anders ist als dein "C:\Everything\Tests\", dann wird mit deinem jetzigen Code aus "D:\muss hier hin" trotzdem wieder "nur" "C:\Everything\Tests\"

Der einzige Fehler bei "D:\muss hier hin" ist ja nur, dass der abschließende \ fehlt.
Und mit meiner Korrektur wird an "D:\muss hier hin" nur noch das fehlende \ angehängt.

Jetzt sollte wirklich alles funktionieren, oder?

Ciao
Thorsten
Anzeige
AW: Makro (bitte prüfen) - Aktives Blatt auf Desktop speichern
15.07.2024 18:14:11
tuska
2Oberschlumpf

Hi,

Danke bei mir hat bereits zuvor alles funktioniert. :-)

Soferne ich Pfade kopiere werden bei mir in Total Commander 11.03 oder Everything 1.5.0.1383a (x64)
die Pfade IMMER mit einem abschließenden Backslash kopiert (ohne Anführungszeichen),
d.h. der vorherige Code würde bei mir IMMER funktionieren, und zwar
auch MIT Leerzeichen im Pfad und OHNE Anführungszeichen im Eingabefeld.

Jetzt habe ich den CODE noch auf Deinen Vorschlag hin geändert und er funktioniert ebenfalls PERFEKT!

Mein finaler Code lautet:

Sub Save_ACTIVE_SHEET_to_specified_PATH()


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

' English:
' 1. If the input field is left EMPTY after the macro is called, the Excel file is saved in the PATH,
' which was defined in the MACRO. EXAMPLE - 2nd line of the code ...: lstrStdPfad = "C:\Everything\Tests\"
' It is NOT necessary to enter the file extension, e.g. .xlsm!
'
' 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!
' It is also NOT necessary to put a path with spaces in inverted commas.
' -----------------------------------------------------------------------------------------------------------------------------

' German:
' 1. Wird nach Aufruf des Makros das Eingabefeld LEER gelassen, dann wird die Excel-Datei in jenem PFAD gespeichert,
' welcher im MAKRO definiert wurde. BEISPIEL - 2. Zeile im Code ...: lstrStdPfad = "C:\Everything\Tests\"
' Die Angabe der Dateierweiterung, zB .xlsm ist NICHT 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!
' Es ist auch NICHT erforderlich einen Pfad mit Leerzeichen in Anführungszeichen zu setzen.
' -----------------------------------------------------------------------------------------------------------------------------

Dim lstrStdPfad As String
lstrStdPfad = "C:\Everything\Tests\"
Path = 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!")
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


Besten Dank für Deine Bemühungen!

Gruß,
Karl

PS:
Ich werde versuchen anderen Helfern noch zu antworten.
Konnte noch nicht alle Vorschläge prüfen, zB "Speichern auf Desktop" von User daniel.
An jene Profi-Unterstützer die hier mitlesen: Vielen herzlichen Dank für die vielen Vorschläge und die Unterstützung!
Anzeige
;

Forumthreads zu verwandten Themen

Anzeige
Anzeige
Anzeige
Anzeige
Entdecke relevante Threads

Schau dir verwandte Threads basierend auf dem aktuellen Thema an

Alle relevanten Threads mit Inhaltsvorschau entdecken
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Entdecke mehr
Finde genau, was du suchst

Die erweiterte Suchfunktion hilft dir, gezielt die besten Antworten zu finden

Suche nach den besten Antworten
Unsere beliebtesten Threads

Entdecke unsere meistgeklickten Beiträge in der Google Suche

Top 100 Threads jetzt ansehen
Anzeige