Consulta sobre userform abiertos en excel

Buenos días
Tengo una consulta con los userform en Excel, y es que tengo unos formularios que funcionan bien en un determinado libro de Excel, pero si abro otro libro de Excel
el userform se abre en el nuevo libro.

Alguien sabe cómo hacer para que el userform se ejecute en un solo libro?

Nota, utilizo este código:


Option Explicit ' Declaración de variantes para los botones de maximizar y minimizar
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA"
(ByVal hWnd As Long, ByVal nIndex As Long) As Long Private Const WS_MINIMIZEBOX As Long = &H20000
Private Const WS_MAXIMIZEBOX As Long = &H10000
Private Const GWL_STYLE As Long = (-16)
Private Const WS_SYSMENU As Long = &H80000 'variable para ocultar el botón x


Private Sub UserForm_Initialize() ' Declaración de variantes para los botones de maximizar y minimizar
Dim lngMyHandle As Long, lngCurrentStyle As Long, lngNewStyle As Long

If Application.Version < 9 Then

lngMyHandle = FindWindow("THUNDERXFRAME", Me.Caption)

Else
lngMyHandle = FindWindow("THUNDERDFRAME", Me.Caption)
End If
lngCurrentStyle = GetWindowLong(lngMyHandle, GWL_STYLE)
lngNewStyle = lngCurrentStyle Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX

'SetWindowLong lngMyHandle, GWL_STYLE, lngNewStyle
Dim hWnd As Long 'para ocultar el botón "X"
Dim lngWstyle As Long
'Localizar la ventana del formulario
hWnd = FindWindow(vbNullString, Me.Caption) lngWstyle = GetWindowLong(hWnd GWL_STYLE) 'Borrar el botón X SetWindowLong hWnd, GWL_STYLE, lngWstyle And (Not
WS_SYSMENU)
'Dibujar de nuevo la barra
DrawMenuBar hWnd
End Sub


Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode <> vbFormCode Then Cancel = True 'para deshabilitar el botón salir
End Sub


De antemano, Gracias

1 respuesta

Respuesta
1

Con ese código haces que el userform se ejecute en primer plano, cuando abres otro libro, pareciera que se ejecuta nuevamente el userform, pero no es que se ejecute otra vez, ya estaba en ejecución y te va a a parecer en primer plano y atrás el otro libro que recién abriste.

Si lo que necesitas es que el formulario tenga los botones de minimizar y maximizar, prueba con esta macro, le hice unos ajustes al código.

Option Explicit ' Declaración de variantes para los botones de maximizar y minimizar
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function DrawMenuBar Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Const WS_MINIMIZEBOX As Long = &H20000
Private Const WS_MAXIMIZEBOX As Long = &H10000
Private Const GWL_STYLE As Long = (-16)
Private Const WS_SYSMENU As Long = &H80000 'variable para ocultar el botón x
Private Sub UserForm_Initialize() ' Declaración de variantes para los botones de maximizar y minimizar
Dim lngMyHandle As Long, lngCurrentStyle As Long, lngNewStyle As Long
If Application.Version < 9 Then
lngMyHandle = FindWindow("THUNDERXFRAME", Me.Caption)
Else
lngMyHandle = FindWindow("THUNDERDFRAME", Me.Caption)
End If
lngCurrentStyle = GetWindowLong(lngMyHandle, GWL_STYLE)
lngNewStyle = lngCurrentStyle Or WS_MINIMIZEBOX Or WS_MAXIMIZEBOX
SetWindowLong lngMyHandle, GWL_STYLE, lngNewStyle
Dim hWnd As Long 'para ocultar el botón "X"
Dim lngWstyle As Long
'Localizar la ventana del formulario
'hWnd = FindWindow(vbNullString, Me.Caption)
'lngWstyle = GetWindowLong(hWnd, GWL_STYLE) 'Borrar el botón X
'SetWindowLong hWnd, GWL_STYLE, lngWstyle And (Not WS_SYSMENU)
'Dibujar de nuevo la barra
'DrawMenuBar hWnd
End Sub

Saludos.Dam
Si es lo que necesitas.

Añade tu respuesta

Haz clic para o

Más respuestas relacionadas