3

Scenario

I have an input box by using the following code

total = Application.InputBox(prompt:="Please key in the total value", Title:="", Type:=2)

Problem

The input box is appearing randomly at the corners of the screen.

What I need

I need this input box align to the center of userform or the screen. Does anyone has any idea?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Anu
  • 1,123
  • 2
  • 13
  • 42
  • 1
    [Have you tried setting the `Top` and `Left` properties?](https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-inputbox-method-excel) – Comintern Aug 07 '18 at 03:02
  • I didn't try that. But that is a static method. If the screen size and others change based on user computer, position of inputbox will still be off sided. I need the position of input box always centered to the userform or the user computer screen regardless of screen resolution – Anu Aug 07 '18 at 03:07
  • 1
    You can just calculate it from [Application.Top](https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-top-property-excel), [Application.Height](https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-height-property-excel), [Application.Width](https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-width-property-excel), and [Application.Left](https://msdn.microsoft.com/en-us/vba/excel-vba/articles/application-left-property-excel). – Comintern Aug 07 '18 at 03:16
  • @Comintern Okay. Let me try that... – Anu Aug 07 '18 at 03:42
  • Let me know if that doesn't answer your question and I will re-open the question :) – Siddharth Rout Aug 07 '18 at 05:00
  • @Comintern Thanks. Your suggestion worked. I will post my answer soon – Anu Aug 07 '18 at 05:26
  • @SiddharthRout The answer you quoted is not working. Also I don't think my question is a duplicate of the question you quoted. – Anu Aug 07 '18 at 05:27
  • @Comintern I can't post my answer due to this question is locked. Anyway this is how i solved it `total = Application.InputBox(prompt:="Please key in the total value", left:=(Application.Width / 2), top:=(Application.Height / 2), Title:="", Type:=2)` – Anu Aug 07 '18 at 05:29
  • I re-opened the question – Siddharth Rout Aug 07 '18 at 05:38
  • Not sure, how your solution is working for you. it doesn't for me :) – Siddharth Rout Aug 07 '18 at 05:39
  • Leaving the [LINK](https://stackoverflow.com/questions/13275692/application-inputbox-left-and-top-not-working-in-excel-vba-why) here for future users – Siddharth Rout Aug 07 '18 at 05:41
  • @SiddharthRout For me when i removed `application.`, it is having runtime error. But when i use `application.` then its working. Unlike the post you quoted. Also I used `application.width/2` and `application.height/2` to get top and left values – Anu Aug 07 '18 at 05:53
  • The solution is not working always in all screen. Not sure why unstable. I will remove the marked answer. Probably someone else can help this issue – Anu Aug 18 '18 at 07:25

1 Answers1

3

Above issue solved this way

total = Application.InputBox(prompt:="Please key in the total value", left:=(Application.Width / 2), top:=(Application.Height / 2), Title:="", Type:=2)

Gave left and top properties. It can be found by getting Application.width and Application.height. Dividing these values by 2 roughly move the inputbox to center regardless of screen size

Edit 1

I thought problem solved. But when I used different systems, it is not stable. So hopefully someone can help on this

Anu
  • 1,123
  • 2
  • 13
  • 42