Solved

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

Posted on 2013-12-19
9
3,223 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 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
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…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

860 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