Excel MsgBox Ja und Nein funtioniert nicht

Hallo,
ich habe ein Problem mit meiner MsgBox. Das Positionieren passt, jedoch JA Nein funktioniert nicht.
Danke!
Gruß Sigi

Sub Aufruf_MsgBox()
If msg("Möchten Sie die " & vbNewLine & _
"Tabelle verlassen " & vbNewLine & _
"verlassen… " _
, vbYesNo + vbCritical, „Tabelle 1 verlassen…“, 0, 0) Then
If vbNo = True Then
Sheets(„Tabelle2“).Select
ElseIf vbYes = True Then
Exit Sub
End If
End If
End Sub

Private Declare Function FindWindow Lib „user32“ Alias _
„FindWindowA“ (ByVal lpClassName As String, ByVal _
lpWindowName As String) As Long
Public Declare Function UnhookWindowsHookEx Lib „user32“ (ByVal hHook As Long) As Long
Public Declare Function GetWindowLong Lib „user32“ Alias „GetWindowLongA“ (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function GetCurrentThreadId Lib „kernel32“ () As Long
Public Declare Function SetWindowsHookEx Lib „user32“ Alias „SetWindowsHookExA“ (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
Public Declare Function SetWindowPos Lib „user32“ (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal x As Long, ByVal y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Public Declare Function GetWindowRect Lib „user32“ (ByVal hwnd As Long, lpRect As RECT) As Long

Public Const GWL_HINSTANCE = (-6)
Public Const SWP_NOSIZE = &H1
Public Const SWP_NOZORDER = &H4
Public Const SWP_NOACTIVATE = &H10
Public Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5
Public hHook As Long
Public MsgBoxPosX As Integer
Public MsgBoxPosY As Integer

Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type

Function WinProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
If lMsg = HCBT_ACTIVATE Then
SetWindowPos wParam, 0, MsgBoxPosX, MsgBoxPosY, 0, 0, SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
UnhookWindowsHookEx hHook
End If
WinProc = False
End Function

Function msg(msgText As String, msgButton As Long, msgTitel As String, ByVal xPos As Long, ByVal yPos As Long)
Dim hInst As Long
Dim XLInst As Long
Dim Thread As Long
MsgBoxPosX = xPos
MsgBoxPosY = yPos
XLInst = FindWindow(„xlmain“, vbNullString)
hInst = GetWindowLong(XLInst, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc, hInst, Thread)
MsgBox msgText, msgButton, msgTitel, 0, 0
End Function

Moin,

wozu eine selbstgebastelte Funktion msg? Das liefert VBA doch schon:

Sub antwort()

    If MsgBox("Möchten Sie ...", vbYesNo + vbCritical) = vbYes Then
        MsgBox ("ja")
    Else
        MsgBox ("nein")
    End If
End Sub

Gruß Ralf

Hallo Ralf,
Danke für Deine Antwort!
Die MsgBox wird positioniert und dann funktioniert das Ja oder Nein nicht.
Funktion:
MsgBox msgText, msgButton, msgTitel, 0, 0
Ich möchte diese Funktion für mehrere MsgBoxen nutzen.
Danke!
Gruß
Sigi

Hi,

eine Funktion muss was zurückgeben, also schreib in die letzte Zeile von msg

   msg = MsgBox(msgText, msgButton, msgTitel, 0, 0)

Und was bei

   If vbNo = True 

rauskommt, weiß ich nicht, wahrscheinlich NULL. Ich würde den Aufruf so interpretieren:

Sub Aufruf_MsgBox()
    If msg("Möchten Sie die " & vbNewLine & _
            "Tabelle verlassen " & vbNewLine & _
            "verlassen... " _
            , vbYesNo + vbCritical, "Tabelle 1 verlassen...", 0, 0) _ 
    = vbNo Then
        Sheets("Tabelle2").Select
    '    MsgBox "nein"
    Else
    '    MsgBox "ja"
        Exit Sub
    End If
End Sub


Hallo Ralf,
mit vbNo = true usw. funktioniert es auch nicht.
Aber trotzdem Danke für Deine Hilfe.
Gruß
Sigi
Meine Lösung:
Function msg(msgText As String, msgButton As Long, msgTitel As String, ByVal xPos As Long, _
ByVal yPos As Long)
Dim hInst As Long
Dim XLInst As Long
Dim Thread As Long
Dim sText As String, sText1 As String
MsgBoxPosX = xPos
MsgBoxPosY = yPos
XLInst = FindWindow(„xlmain“, vbNullString)
hInst = GetWindowLong(XLInst, GWL_HINSTANCE)
Thread = GetCurrentThreadId()
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc, hInst, Thread)
If MsgBox(msgText, msgButton, msgTitel, 0, 0) = vbYes Then
Call M1
Else
Call M2
End If
End Function

Sub Aufruf_MsgBox()
If msg("Möchten Sie die " & vbNewLine & _
"Tabelle verlassen " & vbNewLine & _
"verlassen… " _
, vbYesNo + vbCritical, „Tabelle 1 verlassen…“, 0, 0) Then
End If
End Sub

Hi,

mit vbNo = true usw. funktioniert es auch nicht.

wundert mich nicht, vbNo ist eine Konstante vom Typ long und hat den Wert 7.

Mit dem Auswerten des Funktionswertes sparst Du 2 Subroutinen. Der Maxime KISS (keep it simple & stupid) zu folgen ist nämlich nie falsch.

Jetzt noch eine Frage aus purer Neugier: Wozu dient das Ganze?

Gruß Ralf

Hallo Ralf,
eigendlich sollte das ganze dazu dienen dass ich bei einer Userform andere Button überdecke und
die MsgBox dort erscheint wo ich den Button drücke. Mal links oben mal rechts oben.
Wie ich nun feststellen mußte, funktioniert das ganze nur bei einem Tabellenblatt jedoch nicht über den Aufruf bei einer Userform.
Also habe ich das jetzt weggelassen.
Nun versuche ich statt der MsgBox eine Userform als Meldung zu nehmen.
Doch das klappt auch nicht.
Aufbau: 1.Userform.Show(0) - muß sein, damit ich später im TB arbeiten kann Userform1.Hide
2.Userform.Show mit den Button ja und nein.
Doch wenn ich die 2.Userform schliesse (unload me), dann wird auch die 1.Userform geschlossen.
Das Ganze ist ein Fakturierungprogramm mit Kunden, Projekt, Angebot usw. Verwaltung
Eine Lösung dazu habe ich noch nicht gefunden.
Danke nochmals für Deine Hilfe.
Gruß
Sigi

Danke vielmals. Userforms kenne ich leider nur vom Sehen, deshalb erschließt sich mir nicht so recht, was Du da vorhast ':frowning:

Mir liegt Access näher, und nach meiner Erfahrung stiftenKnöpfe, die mal sichtbar sind und mal nicht, eh nur Verwirrung.

Gruß Ralf

Hallo Ralf,
leider kenne ich mich in Access überhaupt nicht aus.
Excel ist da eher mein Ding.
Ich versuche nur ein perfektes und leicht verständliches Programm für das Bauwesen zu entwerfen.
Das sollte jedoch nur für meine Firma sein und da sind die Ansprüche doch etwas höher.
Alle Programme welche ich bisher über das Internet gefunden habe sind nichts für den Laien oder zu umständlich.
Schade das Du kein Excel Profi bis (wie ich aus Deiner Anmerkung herauslese).
Gruß
Sigi