Live-Forum - Die aktuellen Beiträge
Anzeige
Archiv - Navigation
1648to1652
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

Cancel = True

Cancel = True
08.10.2018 10:57:54
Markus
Hallo zusammen,
ich habe eine kleine Prozedur geschrieben, die mir das Schließen des Dokuments bei bestimmten Zuständen verbieten soll.
Die Prozedur "Field_Check" steht in Module 1. Aufgerufen wird sie sowohl über das Sheet1 mit _
Hilfe eines Worksheet_Change als auch über ThisWorkbook mittels Workbook_BeforeClose.

Private Sub Worksheet_Change(ByVal Target As Range)
If Intersect(Target, Range("C2:E5")) Is Nothing Then Exit Sub
Call Module1.Field_Check
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call Module1.Field_Check
End Sub

Die eigentliche Prozedur in Module 1 sieht dann wie folgt aus:

Public Sub Field_Check()
Dim intCol As Integer
Dim intRow As Integer
Dim i As Integer
For intRow = 3 To 5
i = 1
For intCol = 3 To 5
If Cells(intRow, intCol) > Cells(intRow, intCol - i).Value Then
Cells(intRow, intCol).Select
With Selection.Interior
.ColorIndex = 3
.Pattern = xlSolid
End With
If Cells(7, intCol).Value = "" Then
Cells(7, intCol).Select
Cancel = True
MsgBox "Comment required in" & ActiveCell.Address, vbExclamation
End If
End If
If Cells(intRow, intCol) 

Leider funktioniert das Cancel = True nicht und ich kann das Dokument schließen, obwohl die Kommentarzellen C7:E7 leer sind.
Steht "Cancel = True" an der falschen Stelle?
Vielen Dank vorab
Markus

2
Beiträge zum Forumthread
Beiträge zu diesem Forumthread

Betreff
Datum
Anwender
Anzeige
AW: Cancel = True
08.10.2018 11:06:14
Oberschlumpf
Hi Markus,
ja, das Cancel = True steht an der falschen Stelle; es muss im BeforeClose-Ereignis ausgewertet werden.
Versuch mal das hier (ungetestet):
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If Field_Check = True Then
Cancel = True
End If
End Sub

Die eigentliche Prozedur in Module 1 sieht dann wie folgt aus:
Function Field_Check() As Boolean
Dim intCol As Integer
Dim intRow As Integer
Dim i As Integer
For intRow = 3 To 5
i = 1
For intCol = 3 To 5
If Cells(intRow, intCol) > Cells(intRow, intCol - i).Value Then
Cells(intRow, intCol).Select
With Selection.Interior
.ColorIndex = 3
.Pattern = xlSolid
End With
If Cells(7, intCol).Value = "" Then
Cells(7, intCol).Select
Field_Check = True
MsgBox "Comment required in" & ActiveCell.Address, vbExclamation
Exit Function
End If
End If
If Cells(intRow, intCol) Cells(intRow, intCol).Select
With Selection.Interior
.ColorIndex = xlNone
.Pattern = xlSolid
End With
End If
i = i + 1
Next intCol
Next intRow
End Function
Hilfts?
Wenn nicht, zeig bitte eine Bsp-Datei.
Ciao
Thorsten
Anzeige
AW: Cancel = True
08.10.2018 11:12:16
Markus
Hi Thorsten,
vielen Dank für die schnelle Rückmeldung! Wieder einmal etwas gelernt - funktioniert wie gewünscht :)
Danke und Gruß
Markus

19 Forumthreads zu ähnlichen Themen

Anzeige
Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige

Beliebteste Forumthreads (12 Monate)

Anzeige
Anzeige
Anzeige