5

I have been trying to get the name of a file name when someone uses this Macro. But for some reason every time I run the Macro I get the error: Compile error: Wrong number of arguments or invalid property assignment

I have looked at tons of videos and other responses but none of them have helped me deal with this error.

Sub inputbox()
Dim x As Variant
x = inputbox("Please enter your file name:", "File name")    
MsgBox ("Your file name is" & x) 
End Sub
James
  • 65
  • 4
  • 1
    Spell `Variant` with an `a` instead of an `e` and add `Application.` before your `InputBox` – braX May 29 '18 at 19:21
  • 1
    I spelled Variant correctly in my code, and Application.Inputbox works great thank you. Do you have any idea why it wouldn't work when using just InputBox? – James May 29 '18 at 19:31
  • It was getting confused because you named your function the same name as the application.inputbox - so that's one way to get around it. The answer's way is better tho. – braX May 29 '18 at 19:39

1 Answers1

5

Here is your procedure, corrected: (Copy & paste into a new module.)

Option Explicit

Sub MyInputBoxTest()
    Dim x As Variant
    x = inputbox("Please enter your file name:", "File name")
    MsgBox ("Your file name is " & x)
End Sub

Explanation:

  • Variant was misspelled. Note that since Variant is the default data type, you actually don't need to specify it. Dim x is the same as Dim x as Variant.

  • You can't use names like InputBox OR MsgBox as the name of your procedure. They are reserved words -- already used by another procedure built-in to VBA, so it confuses the compiler since it doesn't know which one you're referring to.

  • Added a space after the word is. (purely cosmetic)

Functions like MsgBox and InputBox can be called different ways depending on whether you use brackets and whether you need to return a value.

InputBox "Hi"
InputBox ("Hi")

...either of these will run fine (but won't return the what the user enters.)

Dim x
x = InputBox ("Hi")

...will return the value to variable x, however:

x = InputBox "Hi"

...will throw an error.


It's highly recommended that, especially while learning, you add the line Option Explicit to the very top of every module. This will help "force" you to properly declare and refer to variables, objects, etc, by generating compile errors when you try to compile (F9) or run (F5) the code.

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
  • 1
    I tried your code and still got the same error. Do you know what else could cause the error? – James May 29 '18 at 19:40
  • 1
    @James - is my code the only code in the module? Copy and pasted without changes? Which line is giving the error, and what is the error? – ashleedawg May 29 '18 at 19:43
  • It wasn't the only code in the module. I deleted the other code (which was an earlier attempt to add an InputBox) and it started working. Thank you so much for your help. – James May 29 '18 at 19:46
  • 2
    FWIW you **can** use `inputbox` as the name of a procedure/function, you just really shouldn't. `Function inputbox() As String inputbox = "Test" End Function` is perfectly valid, as opposed to trying something like `Function Else()`. – BigBen May 29 '18 at 19:47
  • @BigBen Not if you intend to use `inputbox` in the function... or anywhere else. – ashleedawg May 29 '18 at 19:51
  • 1
    Just making a distinction between a truly reserved word, and built-in functions(`inputbox`, `msgbox`) that can technically be "overwritten". – BigBen May 29 '18 at 19:54
  • 1
    @BigBen - think that just confuses matters unnecessarily at this early of an point in learning to code, wouldn't you agree? I think my white lie is a good rule of thumb. *"Don't use words VBA wants to use for something else." Call them "reserved", like they already have dinner plans...* – ashleedawg May 29 '18 at 19:57
  • @BigBen I could point out a hundred other rules that are perfectly true and will do an equivalent job in confusing the OP. There's a reason lesson plans go in order in schools, rather than jumping around. Kids are told using calculators for math is wrong. Is that true? It is when your teacher says it is. – ashleedawg May 29 '18 at 20:21