need some help with code

teachnmyself

Board Regular
Joined
Jan 29, 2008
Messages
110
hey guys..I aquired this code from one of you and i cant seem to get it to work. The purpose of it is to allow the userform to stay up on the screen even if excel is minimized(or closed would be better) If anybody has any idea how to make this work please help me... the name of my form is Userform1..if it helps write it.

Code:
Option ExplicitPrivate Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _ByVal lpWindowName As String) As Long Private 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 Const HWND_TOPMOST = -1Const HWND_NOTOPMOST = -2Const SWP_NOSIZE = &H1Const SWP_NOMOVE = &H2Const SWP_NOACTIVATE = &H10Const SWP_SHOWWINDOW = &H40 Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As LongPrivate Sub UserForm_Initialize()    Dim hWnd As Long    Application.WindowState = xlMinimized         While hWnd = 0        DoEvents        hWnd = FindWindow(vbNullString, Me.Caption)    Wend         SetWindowPos hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE         SetForegroundWindow FindWindow("xlmain", Application.Caption)End SubPrivate Sub UserForm_Terminate()    Application.WindowState = xlNormalEnd Sub
</PRE>Credit goes to whoever wrote the code - found it online but have since lost the link :rolleyes:
 
<!-- / message --><!-- sig -->
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
sorry about that i was in a rush and didtn preview it teh first time to make sure it was right.

Code:
Option Explicit
 
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
 
Private 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
 
Const HWND_TOPMOST = -1
Const HWND_NOTOPMOST = -2
Const SWP_NOSIZE = &H1
Const SWP_NOMOVE = &H2
Const SWP_NOACTIVATE = &H10
Const SWP_SHOWWINDOW = &H40
 
Private Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
 
Private Sub UserForm_Initialize()
    Dim hWnd As Long
    Application.WindowState = xlMinimized
 
    While hWnd = 0
        DoEvents
        hWnd = FindWindow(vbNullString, Me.Caption)
    Wend
 
    SetWindowPos hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE
 
    SetForegroundWindow FindWindow("xlmain", Application.Caption)
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,887
Messages
6,122,095
Members
449,064
Latest member
Danger_SF

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top