I have a simple inputbox in my VBA code. I would like to set its starting position. I know the parameters [LEFT] and [TOP] should do that, but they won't work.
Here is what I have:
x = Application.InputBox(MyPrompt, MyTitle, , 50, 50)
and here is the function syntax
InputBox(Prompt, Title, Default, Left, Top, HelpFile, HelpContextId, Type)
(I left the third parameter [DEFAULT] blank).
No matter what numbers I use for LEFT and TOP, the inputbox always starts at the same place. What is wrong with that?
There's a difference between
which does use the top/left parameters, and
which doesn't.
Note the top/left are in Points, so your provided values will need to be larger than you may be used to...
http://msdn.microsoft.com/en-us/library/office/aa195768(v=office.11).aspx
High,
This Thread came up a lot in searches I did a couple of weeks ago when trying to resolve a similar issue.
I have a solution now, so thought I would pass it on for the benefit of anyone else landing here with the same issue
Issues Summary and clarification of a couple of points given already
The VBA Input Box Function ( and the VBA MsgBox Box Function) are Modal, in other words you cannot do anything to the spreadsheet when they are up. They seem to work well.
The Application Input Box Method should allow you to do this spreadsheet selection when you choose the extra last Type:= option it has as , Type:=8. ( In fact the spreadsheet selection is available for all Type:= options, but the Type:=8 ( Range ) was of particular interest to the OP as it is to me. The VBA Input Box Function only allows you to type in string information in the Box Input Bar )
The Application Input Box Method does allow you to do this Type:=8 ( Range ) via spreadsheet selection, but a couple of things are broken:
_ The ability to position the Pop up ( appears to be broken since Excel 2007 )
_ The Microsoft help function does not appear to me to work in Excel 2003 2007 2010. I do not know if it ever worked for the Application Input Box Method
A solution I am using now
( This is specifically not a solution with a UserForm as I am also considering that separately for my work as a comparison). This is a Pop Up User pseudo InputBox with range selection alternative using API Programs.
This solution overcomes these problems, which is the main reason I did it, especially because of the first problem, which is the main issue of this Thread. It also has a few extra things that might be useful:
_ You can choose the size of the Pop up ( width , height )
_ You can adjust the “z” things… I am not too clear on these options but in simple terms it means that you arrange how it appears in terms of the order of what windows you see, how and in which priority you see it, what windows are “under” or “above” it to see etc..
_ A simple change of the ByRef to ByVal in the signature line of a Call ed routine allows you to change the value of a range object to that of your selection, but the original range object will not change, that is to say its address remains as before the selection. That could give you an extra option in how you select and move around in a spreadsheet.
All codes should be copied to the same code module. ( If the detailed comments irritate then you can easily tank them all with a couple of clicks – see here: mrexcel.com/forum/about-board/795476-comments-code.html#post3893448 )
The first sections Rem 1 and Rem 2 makes the necessary API programmes available and declares (Dim’s) a few related globial variables. This section will need to go at the top of a code module.
Rem 1 is straight forward and makes available a pseudo Non Modal message box.
Rem 2 is a bit more complicated and makes available a few API program things needed to mess about with Windows when they come up.
Section 3a) would be your main code in which you wanted to use / call up the Pop Up User pseudo InputBox with range selection
The simple demo I have done helps illustrate that thing I mentioned about the change of the ByRef to ByVal. If you play around with the code and change that (ByXxx) at the start of the next code section, then I think you will get the point of what I am suggesting there
So in section 3a) the main thing related to the issue is the line
Call HangAHookToCatchAPIssinUserDLL MsgBoxThenBringThatMsgBoxUp(RSel)
That code line makes the pop up come up, and the selected spreadsheet range is returned as a range object in the variable RSel. The code will wait until you have made the selection, but it will not prevent you from making the selection. So in that respect it works similarly to the Application Input Box Method ( when you use that Application Input Box Method and choose the last option as Type:=8 ).
Section 3b)-3c) does two main things.
_ A “Hook” is “Hanged” which does both
__ “catch” events similar to my “Non Modal message box” popping up,
and then when it does it
__ triggers off a Function WinSubWinCls JerkBackOffHooKterd
I have tried to explain everything in more detail in the ‘Comments (and first reference below.)
What actually appears to happen in end effect is that typically as my message box Pops up the function is triggered 6 times before it gets to the “event” I actually want which is the Message box window being activated
Section Rem 4 is the Function WinSubWinCls JerkBackOffHooKterd
This picks out the specific event I want, that being my “Non modal message box” being activated, and changes the window dimensions, using the API thing SetWindowPos
( You will need to experiment about with, mainly, the middle four numbers 10 50 400 150
The two numbers on the ends, 0 40 , you may want to adjust as well. Understanding those two numbers fully is a bit beyond me – it took me best part of a week to figure out WTF all the other stuff was about ).
The last thing this function does is “kill” or “drop” or ““take off” the “Hook”” or ““Unhook” the hook”. If you don’t do that the function seems to be triggered indefinitely.
A last thing on a similar point: Some other codes doing something similar that I saw, seemed to cause a wild recursion: The size adjustment done in the function by SetWindowPos seemed to set off the function code again. The stack seemed to be limited to 30. I could not see any reason to do that, and in fact it seemed to sometimes cause some weird inconsistent ghostly images to appear on my desktop, sometimes permanently!. My function just does the thing that I think it should do once. It appears more stable. I did have a lot of fun braking things before I got the API stuff correct. But since then the code seems to work well without problems in a number of code situations on different computers and different Excel versions. ( In all the situations that I have tried, my code has Exit-ed as I have expected after an If clause detects if the function is on the start of a first recursion copy run. ( I think it would probably do no harm to unhook directly after the SetWindowPos as well***, just in case the SetWindowPos does not cause recursion. ( ***It seems to do no harm to attempt to “unhook” a few times) )
Hope that might help the OP and others with the same or similar issues.
Alan
References:
http://www.excelfox.com/forum/showthread.php/2227-VBA-Input-Pop-up-Boxes-Application-InputBox-Method-versus-VBA-InputBox-Function
http://www.mrexcel.com/forum/excel-questions/447043-left-top-arguments-application-inputbox-method.html
http://www.vbforums.com/showthread.php?617519-RESOLVED-Excel-InputBox-position-works-in-2003-but-not-2007
https://www.excelforum.com/excel-new-users-basics/1099015-vba-application-inputbox-option-helpfile-helpcontextid.html#post4827566