Solved

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

Posted on 2013-12-19
9
3,164 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
  • 5
  • 2
  • 2
9 Comments
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 26

Expert Comment

by:MacroShadow
Comment Utility
If selection.Address is nothing I really don't know what to say.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:rrhandle8
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In the article entitled Working with Objects – Part 1 (http://www.experts-exchange.com/Microsoft/Development/MS_Access/A_4942-Working-with-Objects-Part-1.html), you learned the basics of working with objects, properties, methods, and events. In Work…
Regardless of which version on MS Access you are using, one of the harder data-entry forms to create is one where most data from previous entries needs to be appended to new records, especially when there are numerous fields and records involved.  W…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now