Abbi Asseged
Abbi Asseged

Reputation: 1

Beginner VBA: Learning Basic Dim Format

I am new to VBA and do not understand enough about Dim to even find the error.

  1. The following code should create an object variable that can store information about a Workbook object. What is wrong with the following code? Correct the error(s).
Dim wkbInventory as Workbook     
wkbInventory  =   Application.Workbooks(“myWorkbook.xlsm”) 

New to VBA and do not really understand/know enough about Dim to fix this.

Upvotes: 0

Views: 102

Answers (1)

braX
braX

Reputation: 11755

First, the “curly quotes” will not work... you need the "normal ones".

Secondly, you need to use Set - as a Workbook is an Object variable.

Dim wkbInventory as Workbook
Set wkbInventory = Application.Workbooks("myWorkbook.xlsm") 

That also assumes the workbook is already open, if it is not, then you need to do it like this instead:

Dim wkbInventory as Workbook
Set wkbInventory = Workbooks.Open("C:\Test\myWorkbook.xlsm") 

or like this:

Dim wkbInventory as Workbook
Set wkbInventory = Workbooks.Add("C:\Test\myWorkbook.xlsm") 

Note: A lot of times, normal quotes will turn into curly quotes if you are copy/pasting from a website that does not format things properly, or you are copy/pasting from something like a Word document. Try to pay attention to that.

Upvotes: 2

Related Questions