Hi,
This thread helped me with a very annoying problem. I see now why my Input Box positioning was not “ working “....
Application.InputBox followed the Top and Left instructions up to Excel 2003, but in 2007 it started ignoring them, and still does in Excel 2010 and 2013.......
( Currently I am using XL2007 ).. and maybe i can add a little that could be useful for others hitting this Thread in a search:
I was using / needing wanting / to take in a range using this type of command:
Code:
[color=blue]Dim[/color] RngIn [color=blue]As[/color] Range
[color=blue]Set[/color] RngIn = Application.InputBox(prompt:="Select ref Range", Title:="Ref RangeIn", Default:="A17388", Left:=posLeft, Top:=posTop, Type:=8)
[color=blue]Dim[/color] strRange [color=blue]As[/color] [color=blue]String[/color]
As reported in this Thread the “Top” and “Left” Arguments are not appearing to “Function” In the Application InputBox Method for XL 2007 +
But I did note that the ( unqualified ) InputBox Function appears to “Function” in its “Top” and “Left” Arguments.
This will “work” ( It only supports a string, and does not allow me to take in a Range Object. Also i do not quite follow the logic to the positioning yet, - Maybe that must be experimented a bit every time! )
Code:
[color=blue]Let[/color] strRange = InputBox("Type in ref Range", "string of RangeIn", "A17388", posLeft, posTop) [color=darkgreen]'InputBox with no object qualifier calls the InputBox Function http://stackoverflow.com/questions/13275692/application-inputbox-left-and-top-not-working-in-excel-vba-why[/color]
[color=blue]Set[/color] RngIn = ws.Range("" & strRange & "") [color=darkgreen]'Remember to qualify Range with Worksheet for this String Input Option[/color]
[color=blue]End[/color] [color=blue]Sub[/color]
Here a complete test Code to try:
Code:
[color=blue]Option[/color] [color=blue]Explicit[/color]
[color=darkgreen]'[/color]
[color=blue]Sub[/color] InputBoxText1() '
Rem 1) [color=darkgreen]'Workbook Infoinfo. CHANGE TO SUIT YOUR Workbook and sheet[/color]
[color=blue]Dim[/color] WB [color=blue]As[/color] Workbook: [color=blue]Set[/color] WB = Workbooks("ProAktuellex8600x2.xlsm")
[color=blue]Dim[/color] ws [color=blue]As[/color] Worksheet: [color=blue]Set[/color] ws = WB.Worksheets("Sheet1")
ws.Cells(1, 1).Select [color=darkgreen]'Found in practice that this seemingly unecerssary step sometimes illiminates strange inconsistant errors[/color]
Rem 2) [color=darkgreen]'Select Range from.[/color]
[color=blue]Dim[/color] posTop [color=blue]As[/color] [color=blue]Long[/color], posLeft [color=blue]As[/color] [color=blue]Long[/color]
[color=blue]Let[/color] posTop = ws.Cells(10, 10).Top: [color=blue]Let[/color] posLeft = ws.Cells(10, 10).Left [color=darkgreen]'Cell( 1, 1 ).Top/.Left gives 0.[/color]
[color=darkgreen]'http://www.mrexcel.com/forum/excel-questions/447043-left-top-arguments-application-inputbox-method.html 'Application InputBox Method Left Top arguments do not work xl2007 + Jon Peltier[/color]
[color=darkgreen]'Application.InputBox followed the Top and Left instructions up to Excel 2003, but in 2007 it started ignoring them, and still does in Excel 2010 and 2013.[/color]
[color=blue]Dim[/color] RngIn [color=blue]As[/color] Range
[color=blue]Set[/color] RngIn = Application.InputBox(prompt:="Select ref Range", Title:="Ref RangeIn", Default:="A17388", Left:=posLeft, Top:=posTop, Type:=8)
[color=blue]Dim[/color] strRange [color=blue]As[/color] [color=blue]String[/color]
[color=darkgreen]'http://stackoverflow.com/questions/13275692/application-inputbox-left-and-top-not-working-in-excel-vba-why[/color]
[color=blue]Let[/color] strRange = InputBox("Type in ref Range", "string of RangeIn", "A17388", posLeft, posTop) [color=darkgreen]'InputBox with no object qualifier calls the InputBox Function http://stackoverflow.com/questions/13275692/application-inputbox-left-and-top-not-working-in-excel-vba-why[/color]
[color=blue]Set[/color] RngIn = ws.Range("" & strRange & "") [color=darkgreen]'Remember to qualify Range with Worksheet for this String Input Option[/color]
[color=blue]End[/color] [color=blue]Sub[/color]
Alan
_...........................................................................
P.s.
Oh and BTW – this was a nice “positional idea Tip also
.....
x = Cells(1, n).Left
y = Cells(n, 1).Top
A = Application.InputBox("Hello", Left:=x, Top:=y, Type:=1)
....
P.P.s.
Unfortunately i do not understand the codes given in Post #7 and could not get them to “work”. Running the
Sub TestInputBox()
seems to give a displayed Input Box in the same position, regardless of if I change the positional Arguments in the Application,InputBox test Line . So it appears not to give any “Functioning” positioning?.
Anyone coincidentally tried this and know how/ if it should “work”? - I do note that the code as given will not run at all using Option Explicit, and the “Thing” GetCurrentThreadId does not appear to be defined.
Using the code in the reference given in Post # 7 here,
[RESOLVED] Excel InputBox position works in 2003, but not 2007-VBForums
does appear to work. The code is very similar, but a bit different to that given in Post #7 here.
I have done a modified version to tie up with my example which appears to work at “my End”. To Demo this code, copy all codes complete to a module, then Run
Sub InputBoxSiddharthRoutAlan()
_ .. and experiment a bit with different “posTop” and “posLeft” and run again etc.
Code:
[color=blue]Option[/color] [color=blue]Explicit[/color]
[color=darkgreen]' Siddharth Rout http://www.vbforums.com/showthread.php?617519-RESOLVED-Excel-InputBox-position-works-in-2003-but-not-2007[/color]
[color=darkgreen]'...."....What I have done is I have 'Hooked' the input box and then changed it's position...." ??[/color]
[color=blue]Private[/color] [color=blue]Declare[/color] [color=blue]Function[/color] UnhookWindowsHookEx [color=blue]Lib[/color] "user32" ([color=blue]ByVal[/color] hHook [color=blue]As[/color] [color=blue]Long[/color]) [color=blue]As[/color] [color=blue]Long[/color]
[color=blue]Private[/color] [color=blue]Declare[/color] [color=blue]Function[/color] GetCurrentThreadId [color=blue]Lib[/color] "kernel32" () [color=blue]As[/color] [color=blue]Long[/color]
[color=blue]Private[/color] [color=blue]Declare[/color] [color=blue]Function[/color] SetWindowsHookEx [color=blue]Lib[/color] "user32" Alias "SetWindowsHookExA" ([color=blue]ByVal[/color] idHook [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] lpfn [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] hmod [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] dwThreadId [color=blue]As[/color] [color=blue]Long[/color]) [color=blue]As[/color] [color=blue]Long[/color]
[color=blue]Private[/color] [color=blue]Declare[/color] [color=blue]Function[/color] SetWindowPos [color=blue]Lib[/color] "user32" ([color=blue]ByVal[/color] hwnd [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] hWndInsertAfter [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] x [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] y [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] cx [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] cy [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] wFlags [color=blue]As[/color] [color=blue]Long[/color]) [color=blue]As[/color] [color=blue]Long[/color]
[color=blue]Private[/color] hHook [color=blue]As[/color] Long [color=darkgreen]' Handle to the Hook procedure[/color]
[color=blue]Private[/color] [color=blue]Const[/color] WH_CBT = 5 [color=darkgreen]'Hook type[/color]
[color=blue]Private[/color] [color=blue]Const[/color] HCBT_ACTIVATE = 5 'Hook type
[color=blue]Private[/color] [color=blue]Const[/color] SWP_NOSIZE = &H1 [color=darkgreen]'SetWindowPos Flags- Retains the current size[/color]
[color=blue]Private[/color] [color=blue]Const[/color] SWP_NOZORDER = &H4 [color=darkgreen]'SetWindowPos Flags- Retains the current Z order[/color]
[color=blue]Dim[/color] posTop [color=blue]As[/color] Long, posLeft [color=blue]As[/color] Long
[color=darkgreen]'[/color]
[color=blue]Private[/color] [color=blue]Function[/color] MsgBoxHookProc([color=blue]ByVal[/color] lMsg [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] wParam [color=blue]As[/color] [color=blue]Long[/color], [color=blue]ByVal[/color] lParam [color=blue]As[/color] [color=blue]Long[/color]) [color=blue]As[/color] Long
[color=blue]If[/color] lMsg = HCBT_ACTIVATE [color=blue]Then[/color]
SetWindowPos wParam, 0, posLeft, posTop, 0, 0, SWP_NOSIZE + SWP_NOZORDER [color=darkgreen]'Change position[/color]
UnhookWindowsHookEx hHook [color=darkgreen]'Release the Hook[/color]
[color=blue]End[/color] [color=blue]If[/color]
MsgBoxHookProc = [color=blue]False[/color]
[color=blue]End[/color] [color=blue]Function[/color]
[color=darkgreen]'[/color]
[color=blue]Sub[/color] InputBoxSiddharthRoutAlan() '
Rem 1) [color=darkgreen]'Workbook Info. CHANGE TO SUIT YOUR Workbook and sheet'[/color]
[color=blue]Dim[/color] WB [color=blue]As[/color] Workbook: [color=blue]Set[/color] WB = Workbooks("ProAktuellex8600x2.xlsm")
[color=blue]Dim[/color] ws [color=blue]As[/color] Worksheet: [color=blue]Set[/color] ws = WB.Worksheets("Sheet1")
Rem 2) [color=darkgreen]'Select Range from.[/color]
[color=blue]Let[/color] hHook = SetWindowsHookEx(WH_CBT, AddressOf MsgBoxHookProc, 0, GetCurrentThreadId)
posTop = 100: posLeft = 10 [color=darkgreen]'EXPERIMENT a bit with different Pos![/color]
[color=blue]Dim[/color] RngIn [color=blue]As[/color] Range
[color=blue]Set[/color] RngIn = Application.InputBox(prompt:="Select ref Range", Title:="Ref RangeIn", Default:="A17388", Left:=posLeft, Top:=posTop, Type:=8)
[color=blue]End[/color] [color=blue]Sub[/color]
I confess i cannot understand the code well enough to explain it so will not use it ( yet ) myself. ( I shall probably stay with the InputBox Function for now ). But it could be useful for someone.