Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1832to1836
Aktuelles Verzeichnis
Verzeichnis Index
Übersicht Verzeichnisse
Vorheriger Thread
Rückwärts Blättern
Nächster Thread
Vorwärts blättern
Anzeige
HERBERS
Excel-Forum (Archiv)
20+ Jahre Excel-Kompetenz: Von Anwendern, für Anwender
Inhaltsverzeichnis

Makro automatisch ausführen?

Makro automatisch ausführen?
21.06.2021 16:23:06
Andreas
Hi zusammen,
habe eine Frage zu meinem Code unten. Und zwar gibt mir dieser in meiner Datei wieder, wie viele Dokumente sich in einem bestimmten Ordner befinden.
Wie bringt man Excel dazu, dass diese Zahl automatisch aktualisiert wird?
Dachte das macht man mit "Calculate", hat aber leider nicht funktioniert : (
Wenn also eine weitere Datei in den Ordner eingefügt wird, muss ich immer zuerst das Makro laufen lassen? Oder gibt es da doch eine andere Lösung?
Vielen Dank vorab für eure Hilfe.

Private Sub Worksheet_Calculate()
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Worksheets("Tabelle1").Range("A1").Value = fso.GetFolder("C:\Users\Desktop\Excel").Files.Count
Set fso = Nothing
End Sub
Gruß Andreas

12
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Makro automatisch ausführen?
21.06.2021 17:05:05
Nepumuk
Hallo Andreas,
eine Möglichkeit:
Code:

[Cc][+][-]

Option Explicit Option Private Module Private Declare PtrSafe Function FindFirstChangeNotificationA Lib "kernel32.dll" ( _ ByVal lpPathName As String, _ ByVal bWatchSubtree As Long, _ ByVal dwNotifyFilter As Long) As LongPtr Private Declare PtrSafe Function FindCloseChangeNotification Lib "kernel32.dll" ( _ ByVal hChangeHandle As LongPtr) As Long Private Declare PtrSafe Function WaitForMultipleObjects Lib "kernel32.dll" ( _ ByVal nCount As Long, _ ByRef lpHandles As LongPtr, _ ByVal bWaitAll As Long, _ ByVal dwMilliseconds As Long) As Long Private Const FILE_NOTIFY_CHANGE_FILE_NAME As Long = &H1 Private Const FILE_NOTIFY_CHANGE_DIR_NAME As Long = &H2 Private Const FILE_NOTIFY_CHANGE_ATTRIBUTES As Long = &H4 Private Const FILE_NOTIFY_CHANGE_SIZE As Long = &H8 Private Const FILE_NOTIFY_CHANGE_LAST_WRITE As Long = &H10 Private Const FILE_NOTIFY_CHANGE_SECURITY As Long = &H100 Private Const INVALID_HANDLE_VALUE As Long = -1 Private Const INFINITE As Long = &HFFFF Private Const WAIT_ABANDONED As Long = &H80 Private Const WAIT_FAILED As Long = &HFFFFFFFF Private Const WAIT_OBJECT_0 As Long = &H0 Private Const WAIT_TIMEOUT As Long = &H102 Private lblnAbort As Boolean Public Sub Ueberwachung_Start() ' Anpassen - Backslash am Ende nicht löschen Const FOLDER_PATH As String = "G:\Eigene Dateien\Eigene Excelbeispiele\" Dim strFileName As String Dim alngptrNotify(1) As LongPtr Dim lngWaitStatus As Long alngptrNotify(0) = FindFirstChangeNotificationA( _ FOLDER_PATH, True, FILE_NOTIFY_CHANGE_FILE_NAME) If alngptrNotify(0) = INVALID_HANDLE_VALUE Then Call MsgBox("Überprüfung auf Dateiänderungen ist nicht möglich") Exit Sub End If alngptrNotify(1) = FindFirstChangeNotificationA( _ FOLDER_PATH, True, FILE_NOTIFY_CHANGE_DIR_NAME) If alngptrNotify(1) = INVALID_HANDLE_VALUE Then Call MsgBox("Überprüfung auf Ordneränderungen ist nicht möglich") Exit Sub End If lblnAbort = False Do DoEvents lngWaitStatus = WaitForMultipleObjects(2, alngptrNotify(0), False, 10) Select Case lngWaitStatus Case WAIT_OBJECT_0 Call MsgBox("Eine Datei auf " & FOLDER_PATH & " wurde erstellt oder gelöscht.") Call FindCloseChangeNotification(alngptrNotify(0)) alngptrNotify(0) = FindFirstChangeNotificationA( _ FOLDER_PATH, True, FILE_NOTIFY_CHANGE_FILE_NAME) Case WAIT_OBJECT_0 + 1 MsgBox "Ein Ordner auf " & FOLDER_PATH & " wurde erstellt oder gelöscht." Call FindCloseChangeNotification(alngptrNotify(1)) alngptrNotify(1) = FindFirstChangeNotificationA( _ FOLDER_PATH, True, FILE_NOTIFY_CHANGE_DIR_NAME) End Select Loop Until lblnAbort Call FindCloseChangeNotification(alngptrNotify(0)) Call FindCloseChangeNotification(alngptrNotify(1)) End Sub Public Sub Ueberwachung_Stop() lblnAbort = True End Sub

Gruß
Nepumuk
Anzeige
AW: Makro automatisch ausführen?
22.06.2021 12:29:38
Andreas
Mahlzeit Nepumuk,
erst einmal vielen Dank für deine Mühe!
Hab das gleich ausprobiert, es scheint auch zu gehen, hab auf jeden Fall die Meldung bekommen, dass eine Datei im Verzeichnis erstellt oder gelöscht wurde.
Bin mir jetzt nur nicht sicher, was davon ich anpassen muss, damit die Anzahl auch auf dem Blatt zu sehen ist.
Wäre super wenn du mir das noch erklären kannst : )
Vielen Dank vorab
Gruß Andreas
AW: Makro automatisch ausführen?
22.06.2021 12:35:13
Nepumuk
Hallo Andreas,
einfach an Stelle der MsgBox deinen Code einfügen.
Gruß
Nepumuk
AW: Makro automatisch ausführen?
22.06.2021 13:39:11
Andreas
Hi Nepumuk,
das hab ich leider nicht verstanden.
Den Code habe ich in ein neues Modul eingefügt, ist das falsch?
MsgBox kannte ich noch gar nicht, hab aber mal gegoogelt und sowas gibt es in meiner Datei nicht.
Hab dann versucht irgendwelche Dinge aus den Entwicklertools zu nehmen und denen das Makro zuzuweisen, dieses wird aber gar nicht aufgeführt.
Was mach ich nur falsch?
Gruß Andreas
Anzeige
AW: Makro automatisch ausführen?
22.06.2021 13:49:17
max.kaffl@gmx.de
Hallo Andreas,
was ist daran jetzt so schwierig:
Code:

[Cc][+][-]

Option Explicit Option Private Module Private Declare PtrSafe Function FindFirstChangeNotificationA Lib "kernel32.dll" ( _ ByVal lpPathName As String, _ ByVal bWatchSubtree As Long, _ ByVal dwNotifyFilter As Long) As LongPtr Private Declare PtrSafe Function FindCloseChangeNotification Lib "kernel32.dll" ( _ ByVal hChangeHandle As LongPtr) As Long Private Declare PtrSafe Function WaitForMultipleObjects Lib "kernel32.dll" ( _ ByVal nCount As Long, _ ByRef lpHandles As LongPtr, _ ByVal bWaitAll As Long, _ ByVal dwMilliseconds As Long) As Long Private Const FILE_NOTIFY_CHANGE_FILE_NAME As Long = &H1 Private Const FILE_NOTIFY_CHANGE_DIR_NAME As Long = &H2 Private Const FILE_NOTIFY_CHANGE_ATTRIBUTES As Long = &H4 Private Const FILE_NOTIFY_CHANGE_SIZE As Long = &H8 Private Const FILE_NOTIFY_CHANGE_LAST_WRITE As Long = &H10 Private Const FILE_NOTIFY_CHANGE_SECURITY As Long = &H100 Private Const INVALID_HANDLE_VALUE As Long = -1 Private Const INFINITE As Long = &HFFFF Private Const WAIT_ABANDONED As Long = &H80 Private Const WAIT_FAILED As Long = &HFFFFFFFF Private Const WAIT_OBJECT_0 As Long = &H0 Private Const WAIT_TIMEOUT As Long = &H102 Private lblnAbort As Boolean Public Sub Ueberwachung_Start() ' Anpassen - Backslash am Ende nicht löschen Const FOLDER_PATH As String = "G:\Eigene Dateien\Eigene Excelbeispiele\" Dim strFileName As String Dim alngptrNotify(1) As LongPtr Dim lngWaitStatus As Long Dim objFileSystem As Object alngptrNotify(0) = FindFirstChangeNotificationA( _ FOLDER_PATH, True, FILE_NOTIFY_CHANGE_FILE_NAME) If alngptrNotify(0) = INVALID_HANDLE_VALUE Then Call MsgBox("Überprüfung auf Dateiänderungen ist nicht möglich") Exit Sub End If alngptrNotify(1) = FindFirstChangeNotificationA( _ FOLDER_PATH, True, FILE_NOTIFY_CHANGE_DIR_NAME) If alngptrNotify(1) = INVALID_HANDLE_VALUE Then Call MsgBox("Überprüfung auf Ordneränderungen ist nicht möglich") Exit Sub End If lblnAbort = False Do DoEvents lngWaitStatus = WaitForMultipleObjects(2, alngptrNotify(0), False, 10) Select Case lngWaitStatus Case WAIT_OBJECT_0 Set objFileSystem = CreateObject(Class:="Scripting.FileSystemObject") Worksheets("Tabelle1").Range("A1").Value = _ objFileSystem.GetFolder("C:\Users\Desktop\Excel").Files.Count Set objFileSystem = Nothing Call FindCloseChangeNotification(alngptrNotify(0)) alngptrNotify(0) = FindFirstChangeNotificationA( _ FOLDER_PATH, True, FILE_NOTIFY_CHANGE_FILE_NAME) Case WAIT_OBJECT_0 + 1 MsgBox "Ein Ordner auf " & FOLDER_PATH & " wurde erstellt oder gelöscht." Call FindCloseChangeNotification(alngptrNotify(1)) alngptrNotify(1) = FindFirstChangeNotificationA( _ FOLDER_PATH, True, FILE_NOTIFY_CHANGE_DIR_NAME) End Select Loop Until lblnAbort Call FindCloseChangeNotification(alngptrNotify(0)) Call FindCloseChangeNotification(alngptrNotify(1)) End Sub Public Sub Ueberwachung_Stop() lblnAbort = True End Sub

Gruß
Nepumuk
Anzeige
AW: Makro automatisch ausführen?
22.06.2021 14:04:52
Andreas
Hallo Nepumuk,
schwierig ist, dass ich nicht weiß was ich machen muss, dass mir das Ergebnis der Überwachung in irgendeiner Zelle angezeigt wird =/
Gruß Andreas
AW: Makro automatisch ausführen?
23.06.2021 15:32:17
Andreas
Hallo Nepumuk,
würde so gerne deinen Lösungsvorschlag verwenden, aber ich weiß einfach nicht wie?
Hab MsgBox gegoogelt und dabei nur andere VBA Codes gefunden.
Kannst du mir bitte sagen, wie ich das Ergebnis des Codes in meiner Datei Darstellen kann?
https://www.herber.de/bbs/user/146747.xlsx
Das Ergebnis muss in Feld 6 stehen.
Wäre dir sehr Dankbar
Gruß Andreas
Anzeige
AW: Makro automatisch ausführen?
23.06.2021 15:40:40
Nepumuk
Hallo Andreas,
Feld 6 ist welche Zelle?
Gruß
Nepumuk
AW: Makro automatisch ausführen?
23.06.2021 15:41:40
Andreas
Sorry, Zelle S6 habe ich gemeint.
Gruß Andreas
AW: Makro automatisch ausführen?
25.06.2021 11:34:15
Andreas
Hi Nepumuk,
danke fürs einbauen, es kommt kein Fehler, aber in S6 passiert einfach nichts.
Hab dann mal den Dateipfad geändert, und geguckt ob er das von einem anderen Verzeichnis zählen und in S6 wiedergeben kann.
Aber das funktioniert leider auch nicht.
Hast du eine Idee, woran das liegen kann? Den \ am Ende habe ich stehen lassen, wie du in der VBA beschreibst.
Gruß Andreas
Anzeige
AW: Makro automatisch ausführen?
25.06.2021 11:55:28
Nepumuk
Hallo Andreas,
keine Ahnung, bei mir funktioniert das.
Gruß
Nepumuk

308 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige