Solved

MS access vba turn of filter in Excel worksheet

Posted on 2014-02-27
5
1,159 Views
Last Modified: 2014-02-27
From Ms access using VBA turn off filter in MS Excel sheet.  Some time user leave the filter turn on in Excel sheet. This cause the import to fail.
Ms Acess 2010
Excel 2010

I need to turn the filter off in the Excel sheet using VBA from Ms Access.

Thank you
0
Comment
Question by:newholyman
  • 3
  • 2
5 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 39892211
How are you currently importing your data?  If you are using Excel Automation try setting the autofilter mode of your worksheet object:


xlWS.AutoFilterMode = False


if you are not currently using automation, it is done similar to this:


Dim xlApp as Excel.Application
Dim xlWB  Excel.Workbook
Dim xlWS as Excel.Worksheet

Set xlApp = New Excel.Application
Set xlWB = xlApp.Workbooks.Open("C:\Yourfilename.xls")
Set xlSH = xlWB.Sheets("YourWorkSheet")

if xlWS.AutoFilterMode = True then xlWS.AutoFilterMode = False

 ' etc

' clear up objects

xlwb,close
xlapp.quit
set xlwb = nothing
set xlapp = nothing
setxlws = nothing

Open in new window

0
 

Author Comment

by:newholyman
ID: 39892493
received error message object does not support this object or method

Dim xlObj As Object, Sheet As Object
Dim xlFile As String
xlFile = ("file")
Rem xlFile = wrk_copy
    Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Open xlFile
    xlObj.Visible = True
    Set Sheet = xlObj.ActiveWorkbook.Worksheets("sheetname")
    If xlObj.AutoFilterMode = True Then xlObj.AutoFilterMode = False
  xlObj.ActiveWorkbook.Save
  xlObj.ActiveWorkbook.Close
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 39892661
<<If you are using Excel Automation try setting the autofilter mode of your worksheet object >>

Your code is trying to set the autofilter mode of the *application* object (xlObj), which may be the cause of your error.

It should be on the worksheet object (declared as "Sheet" in your code)
0
 

Author Closing Comment

by:newholyman
ID: 39892758
Here the final vba that worked;
Dim xlObj As Object, Sheet As Object
Dim xlFile As String
xlFile = ("file")
Rem xlFile = wrk_copy
    Set xlObj = CreateObject("Excel.Application")
    xlObj.Workbooks.Open xlFile
    xlObj.Visible = True
    Set Sheet = xlObj.ActiveWorkbook.Worksheets("sheetname")
    If Sheet.AutoFilterMode = True Then Sheet.AutoFilterMode = False
  xlObj.ActiveWorkbook.Save

Thank you
0
 
LVL 61

Expert Comment

by:mbizup
ID: 39892818
If the suggestion I posted did the trick (If Sheet.AutoFilterMode = True Then Sheet.AutoFilterMode = False), why did you award a B grade?

EE's grading standards are here:

http://support.experts-exchange.com/customer/portal/articles/481419-what-grade-should-i-award-?b_id=44

A B grade means the solution given lacked some information or required you to do a good amount of extra work to resolve the problem. When closing the question, the asker should explain why a B grade was awarded.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
It took me quite some time to sort out all the different properties of combo and list boxes available from Visual Basic at run-time. Not that the documentation is lacking: the help pages are quite thorough and well written. The problem was rather wh…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
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…

708 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

13 Experts available now in Live!

Get 1:1 Help Now