?
Solved

Excel VBA "Object or With Block variable not set"

Posted on 2010-01-10
12
Medium Priority
?
252 Views
Last Modified: 2013-11-25

Hi there,  I can't figure out why my code stops on the second line of code (tbldata2VisCount) and not on the first? I hover over tbldataVisCount and it shows the correct count of filtered items but when I hover over tbldata2VisCount, the result = Emply

 Dim tbldataVisCount As Variant
 Dim tbldata2VisCount As Variant


 tbldataVisCount = Sheet4.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
 tbldata2VisCount = Sheet2.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1    ' this line keeps breaking
            dblCount = tbldataVisCount + tbldata2VisCount

Any thoughts?
0
Comment
Question by:Calvin LeBlanc
[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
  • 6
  • 2
  • 2
  • +1
12 Comments
 
LVL 13

Expert Comment

by:ioane
ID: 26280326
Make sure you have a Sheet2 in your workbook.

Go to vba editor and check the names of the worksheets in the project tree.

Note, you want the Object instance name, not the Worksheet name.

Another way to write it that may work better is to use the Worksheet name:

 tbldataVisCount = Sheets("Sheet_Name").AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
0
 

Author Comment

by:Calvin LeBlanc
ID: 26280359
The workbook does have a Sheet2. I just tried using the sheet name and no go.
I tried to compare how the previous line is written and all seams to be fine.
0
 

Author Comment

by:Calvin LeBlanc
ID: 26280368
I"m basically wanting to count the number of result rows  in a "Filtered" table in two separate tables (tbl1data and tbl2data) and then add those two results to be used as a reference.  
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 50

Expert Comment

by:Dave Brett
ID: 26280433
Can you pls post your file

Cheers
Dave
0
 

Author Comment

by:Calvin LeBlanc
ID: 26280530
Dave, in attempting to sanitize the raw data in the workbook I restarted Excel and it seams to be working fine now. I'll test it a little more and post a status tomorrow afternoon. thanks for the assist !!
0
 
LVL 13

Accepted Solution

by:
ioane earned 1000 total points
ID: 26280561
Are you using AutoFilter in both tables?

See what you get when you add this:

Debug.print "Sheet4 AutoFilter is: " & iif(Sheet4.AutoFilterMode,"on","off")
 tbldataVisCount = Sheet4.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1
Debug.print "Sheet2 AutoFilter is: " & iif(Sheet2.AutoFilterMode,"on","off")
 tbldata2VisCount = Sheet2.AutoFilter.Range.Columns(1).SpecialCells(xlCellTypeVisible).Count - 1    ' this line keeps breaking
0
 

Author Comment

by:Calvin LeBlanc
ID: 26280605
Thanks for the post Tramtrak. Your solution seams to work too. I'll do more testing and post / divy up points tomorrow evening.

Thanks to you both for the assistance.
0
 
LVL 50

Expert Comment

by:Dave Brett
ID: 26280612
If the file is working now then we haven't solved anything - so you delete your question and get your points refunded. :)

Cheers

Dave
0
 

Author Comment

by:Calvin LeBlanc
ID: 26280631
As an FYI, these lines of code seam to work fine on a data set of 20 records (in both tables) but breaks with a data set of 250,000 records in each of the tables.
still testing.
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 26281289
Have you tried defining all your counter variables as Long?
0
 
LVL 85

Assisted Solution

by:Rory Archibald
Rory Archibald earned 1000 total points
ID: 26281295
Also if you are using tables in excel 2007 you really want the autofilter property of the listobject, not the worksheet.
Regards
Rory
0
 

Author Closing Comment

by:Calvin LeBlanc
ID: 31675920
Both solutions helped me better understand how to troubleshoot and resolve the issue.
Thanks to the both of you for the assistance.
0

Featured Post

Independent Software Vendors: 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

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…
This article describes how you can use Custom Document Properties to store settings and other information in your workbook so that they will be available the next time you open the workbook.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

777 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