Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having problems with VBA. Run time error '91' appears to me. Any idea?

Tags:

excel

vba

I'm trying to unzip a file using VBA but get the run time error 91. This is my code:

Dim Destino As String
Dim Origen As String
Dim oAplica As Object

Destino = "C:\Users\Oscar Mayorga\Downloads"
Origen = "C:\Users\Oscar Mayorga\Downloads\PD20210822.zip"
Set oAplica = CreateObject("Shell.Application")
oAplica.Namespace(Destino).CopyHere oAplica.Namespace(Origen).Items
like image 423
Oscar Mayorga Avatar asked Aug 25 '21 02:08

Oscar Mayorga


People also ask

How do I fix Runtime error 91 in VBA?

Error: "Runtime Error 91" is a Visual BASIC error which means "Object variable not set". This indicates that the object was never created using the "Set" command before being used. Remedy: Be sure to use the SET statement to create the new oject.

How do I fix runtime error in VBA?

To work around this problem, unprotect the worksheet to enable the macro to run. You can manually unprotect the worksheet or by using the Unprotect method in the macro.

How do I fix object variable not set error 91?

First you must declare the object variable. Then you must assign a valid reference to the object variable using the Set statement. Similarly, a With... End With block must be initialized by executing the With statement entry point.


Video Answer


1 Answers

Change Destino = "C:\Users\Oscar Mayorga\Downloads to Destino = "C:\Users\Oscar Mayorga\Downloads\". Also change declaration as String to Variant

Is this what you are trying?

Dim Destino As Variant
Dim Origen As Variant
Dim oAplica As Object

Destino = "C:\Users\Oscar Mayorga\Downloads\"
Origen = "C:\Users\Oscar Mayorga\Downloads\PD20210822.zip"
Set oAplica = CreateObject("Shell.Application")
oAplica.Namespace(Destino).CopyHere oAplica.Namespace(Origen).Items

Explanation: You are using Late Binding. To understand this, use Early Binding as shown below. Set a reference to Microsoft Shell Controls and Automation from Tools | References

enter image description here

Option Explicit

Sub Sample()
    Dim Destino As Variant
    Dim Origen As Variant
    Dim oAplica As Shell32.Shell

    Destino = "C:\Users\Oscar Mayorga\Downloads\"
    Origen = "C:\Users\Oscar Mayorga\Downloads\PD20210822.zip"
    Set oAplica = New Shell32.Shell
    oAplica.Namespace(Destino).CopyHere oAplica.Namespace(Origen).Items
End Sub

Now when you press spacebar after you type the bracket, you will notice the below

enter image description here

It expects a Variant. More about it can be read in Shell.NameSpace method

In case the above link dies

enter image description here

like image 120
Siddharth Rout Avatar answered Nov 14 '22 21:11

Siddharth Rout