0

I'm getting compiler error while running below code:

Sub AddNameInList()

  FilmName = InputBox("Type in a new film name")

   Worksheets("sheet2").Activate

   Range("b1").End(xlDown).Offset(1, 0).Select

   ActiveCell.Value = FilmName

   MsgBox FilmName & " was added to the list"

End Sub

Error Message

Compiler Error: Variable not defined

3
  • on which line does it break? Commented Apr 19, 2016 at 19:38
  • In First Line at "FileName" Commented Apr 19, 2016 at 19:43
  • 1
    I guess these issues would be clearer if the error message was changed to say "Variable not dimensioned" Commented Apr 19, 2016 at 20:41

1 Answer 1

2

You need to declare FilmName:

Sub AddNameInList()
Dim FilmName As String

FilmName = InputBox("Type in a new film name")

Worksheets("sheet2").Range("B1").End(xlDown).Offset(1, 0).Value = FilmName

MsgBox FilmName & " was added to the list"

End Sub

If you have the Option Explicit set either at the top of the module or in the settings then it requires that all variables be declared.

Also avoid using .Select and .Activate. In general they are not needed and only slow down the code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.