Solved

Excel VBA error opening workbook

Posted on 2011-02-27
7
465 Views
Last Modified: 2012-05-11
Hi. I am getting the following error on the line marked with ** in the code below.

"excel vba object variable or with block variable not set"

The strange thing is that it opens the workbook


Sub Main()

    On Error GoTo EH

    MYDOC_DIR = Environ("userprofile") & "\Desktop"
    Dim oWb As Workbook
    oWb = Application.Workbooks.Open(MYDOC_DIR & "\" & "FILE FROM ITS.xlsm") '**
    Windows("FILE FROM ITS.xlsm").Activate
    oWb.Sheets(1).Cells.Copy
    ThisWorkbook.Sheets(1).Cells.Paste
    
    Exit Sub
EH:
    MsgBox Err.Description

End Sub

Open in new window

0
Comment
Question by:murbro
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 500 total points
ID: 34992818
Try:

Set oWb = Application.Workbooks.Open(MYDOC_DIR & "\" & "FILE FROM ITS.xlsm") '**
instead of
oWb = Application.Workbooks.Open(MYDOC_DIR & "\" & "FILE FROM ITS.xlsm") '**

Chris
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34992826
Do you need to declare MYDOC_DIR as a local variable?

Dim MYDOC_DIR As String

Open in new window

0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34992838

chris_bottomley:  Good thinking, but "set" is not needed in VB assignment statements.

http://msdn.microsoft.com/en-us/library/z2wkh0tk(v=vs.80).aspx

Quite a mystery.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Closing Comment

by:murbro
ID: 34992860
Hi. The "Set" worked - the declaration of the variable made no difference.
Thanks
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34992892
chris_bottomley, any idea why the Set is needed on that line but not on the previous line that sets the value of MYDOC_DIR?  Or why it is needed at all?

Great you solved the problem but it is still a question why it worked.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 34992906
Simply put objects need to be set, variables do not ... therefore a reference to an application, workbook or worksheet are objects and therefore are always set.

Chris
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34993036
Thanks!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question