Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA: Run time error '91'?

Tags:

vba

All I'm trying to do here is save a reference to the currently active window, but it doesn't seem to be working. It gives me a run time error on the last line.

Dim SourceWindow As Window, QACheckWindow As Window
SourceWindow = ActiveWindow

I'm not exactly sure why. Isn't ActiveWindow supposed to return the currently active window? If not, how can I make a reference to it?

EDIT: The above is right at the beginning of my function, so all there is before it is Sub FuncName()

like image 873
supercheetah Avatar asked Sep 20 '13 23:09

supercheetah


People also ask

How do I fix object variable not set error 91 in Excel?

This error has the following causes and solutions: You attempted to use an object variable that isn't yet referencing a valid object. Dim MyObject As Object ' Create object variable. Set MyObject = Sheets(1) ' Create valid object reference.

How do you fix object variables not set?

To correct this errorMake sure you aren't referring to an object variable that has been set to Nothing . Search your code for the keyword Nothing , and revise your code so that the object isn't set to Nothing until after you have referenced it. Make sure that any array variables are dimensioned before you access them.

How do I fix type mismatch error in VBA?

Step 1: Write the subprocedure for VBA Type Mismatch. Step 2: Again assign a new variable, let's say “A” as Byte data type. Let's understand the Byte Data type here. Byte can only store the numerical value from 0 to 255.


1 Answers

In VB object variables require the Set keyword to be assigned. Object properties that are objects also need to be Set. Runtime error 91 "object variable not set" is raised when the assignment doesn't use that keyword.

This is inherited from legacy Let keyword to assign values, and Set keyword to assign references; the Let eventually was deprecated (although still needed for defining properties) and the Set remained, leaving the VB6/VBA value assignment syntax like [Let] variable = value, where "Let" is optional.

In the declaration and assignment:

Dim SourceWindow As Window, QACheckWindow As Window
'this is like saying "Let SourceWindow = ActiveWindow":
SourceWindow = ActiveWindow

SourceWindow is an object, assigned as if it were a value - this causes VBA to attempt let-coercion through a default member call. If the object wasn't initialized, the member call fails with error 91. If the object was initialized but doesn't have a default member, error 438 is raised.

So in this case error 91 is being raised because of an implicit member call; the .net equivalent would be a NullReferenceException:

Dim SourceWindow As Window, Dim WindowTitle As String
'"SourceWindow" reference isn't set, the object can't be accessed yet:
WindowTitle = SourceWindow.Caption 

I'm going to go a bit overboard here, but the legacy Let statement should not be confused with the Let clause (in VB.net) which, in the LINQ query syntax (in VB.net), computes a value and assigns it to a new, query-scoped variable (example taken from MSDN):

From p In products 
Let Discount = p.UnitPrice*0.1 '"Discount" is only available within the query!
Where Discount >= 50
Select p.ProductName, p.UnitPrice, Discount

VB.net assigns both values and references, without the need to specify a Let or a Set, because in .net this distinction is a much thinner line, given how everything ultimately derives from System.Object... including System.ValueType. That's why the Set keyword was also deprecated in VB.net, and also why the VB.net syntax for defining properties has dropped the Let in favor of Set - because parameterless default members are illegal in VB.NET, so this ambiguous let-coercion doesn't happen.

like image 177
Mathieu Guindon Avatar answered Sep 18 '22 15:09

Mathieu Guindon