Solved

Access VBA -- Object variable or With Block variable not set

Posted on 2013-12-19
9
3,267 Views
Last Modified: 2014-01-15
I am using Access VBA to open an Excel workbook, and read the workbooks contents into an array.

50% of the time I get the following error.  I have market the code line where the error occurs

Run-time error '91';
Object variable or With Block variable not set.

           Dim wbk As Excel.Workbook
            Set wbk = ExcelApp.Workbooks.Open(FileName:=CurrentProject.Path & "\" & strFile)
            '''''''''''''''''''''''''''''''''''''
            ' Get Control Tower sheet data
            '''''''''''''''''''''''''''''''''''''
            wbk.Sheets("Control Towers").Activate
            wbk.ActiveSheet.cells(1, 1).Select
            wbk.ActiveSheet.cells(1, 1).CurrentRegion.Select
            Dim rng As Excel.Range
            Set rng = Selection
            
            ColCount = rng.Columns.Count  <---   ERROR ON THIS LINE
            RowCount = rng.Rows.Count

Open in new window

0
Comment
Question by:rrhandle8
[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
  • 5
  • 2
  • 2
9 Comments
 
LVL 27

Expert Comment

by:MacroShadow
ID: 39730186
Do you have a selection when the error occurs?

The error indicates that the range wasn't defined, and since you populate the range with the selection, it would seem that when the code errs out there is no valid selection.
0
 

Author Comment

by:rrhandle8
ID: 39730204
Yes, I have a selection.  I have made the workbook visible, and I can see the current region is selected.
0
 

Author Comment

by:rrhandle8
ID: 39730209
I should have added that the selection in the code is saying it is nothing.  So I guess you are right, but why is the selection - nothing when I can see the area in the workbook is selected?
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!

 
LVL 27

Expert Comment

by:MacroShadow
ID: 39730406
If selection.Address is nothing I really don't know what to say.
0
 

Author Comment

by:rrhandle8
ID: 39730669
The solution is below


            'Dim rng As Excel.Range
            'Set rng = Selection.Address

            ColCount = wbk.ActiveSheet.cells(1, 1).CurrentRegion.Columns.Count
            RowCount = wbk.ActiveSheet.cells(1, 1).CurrentRegion.Rows.Count

Open in new window

0
 
LVL 20

Expert Comment

by:clarkscott
ID: 39743334
Dealing with Excel in VBA is sometimes challenging.  Whenever things don't seem to work, be as specific as possible.

original:
ColCount = rng.Columns.Count  

new:
ColCount = wbk.rng.Columns.Count  

Scott C
0
 

Author Comment

by:rrhandle8
ID: 39752111
Scott,

ColCount = wbk.rng.Columns.Count  still gives same error:



Run-time error '91';
Object variable or With Block variable not set.
0
 

Author Comment

by:rrhandle8
ID: 39752124
Scott,

Interesting find.

            Dim rng As Range
            Set rng = wbk.ActiveSheet.Range("testrange2")
            Debug.Print wbk.rng.Columns.Count

That gives an error, but this works

            Debug.Print wbk.ActiveSheet.Range("testrange2").Columns.Count

Am I doing something wrong with the way I am setting the range?
0
 
LVL 20

Accepted Solution

by:
clarkscott earned 500 total points
ID: 39753655
How about attaching it and let me see it.  Provide the Access and Excel files.

I'll figure it out for you.

Scott C
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

734 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