From the course: Excel: Learning VBA

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Declare variables and require declaration before use

Declare variables and require declaration before use - Microsoft Excel Tutorial

From the course: Excel: Learning VBA

Declare variables and require declaration before use

- [Instructor] The designers of the VBA programming language understood that many of the users who create their own code will not be professional programmers. Rather than enforcing some best practices such as naming all of your variables before you use them, the VBA language lets you create variables in the middle of a subroutine or a function without declaring them earlier. It's tempting to use that flexibility to avoid declaring your variables, but it can lead to problems. And in this movie, I will show you how to declare variables, and also demonstrate one of the problems that you can run into. My sample file is 02_02_Declare, and you can find it in the Ch02 folder of the Exercise Files collection. In this workbook, I have a single worksheet, and I have sales data in cell C5 through C9. And we'll be working with the value in C7 as part of our subroutine, but I'll click over to B2 now so that the code will be able…

Contents