If you scour the internet you’ll find plenty of examples of how to give your VBA userform minimize and maximize buttons. The exact code will vary depending on the trinkets the programmer wants, but a typical snippet will look something like this:

The code is fine but it needs to be updated to run in 64-bit environments. In short, the way do to this is to include the PtrSafe keyword after each Declare keyword and to look up each function on MSDN and then use LongPtr instead of Long for each parameter and output if it represents a handle or a pointer. If you want the functions to work across both 32-bit and 64-bit Office then you may have to declare them multiple times inside a conditional compilation block. There are a couple of useful articles on MSDN which go into this in more depth:

Note To write code that is compatible with both 32-bit and 64-bit versions of Windows, use GetWindowLongPtr. When compiling for 32-bit Windows, GetWindowLongPtr is defined as a call to the GetWindowLong function.

To declare these new functions correctly in VBA, consideration needs to be given to the possibility that you’re using 32-bit Office – even if it is on 64-bit Windows. On 64-bit Windows, 32-bit Excel will try to use the 32-bit User32.dll in the C:\Windows\SYSWOW64 directory. This makes perfect sense but if you don’t believe me you can see it by using Process Explorer:

The 32-bit User32.dll doesn’t contain the GetWindowLongPtr and SetWindowLongPtr functions. You can see this by using Dependency Walker:

In other words, if you try to call them from 32-bit Excel you will get the run-time error 453: Can’t Find DLL entry point [method name] in User32.dll. This means that the Win64 compilation constant needs to be used rather than VBA7 (I will talk more about these constants in a later blog post), so the API declarations should be written as follows: