Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6920615
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:10:13+00:00 2026-05-27T10:10:13+00:00

I’m writing a script for a worksheet whose cells are populated based on an

  • 0

I’m writing a script for a worksheet whose cells are populated based on an Access database. I’m trying to clear the contents of the worksheet without removing any Autofilters that the user has set, and then reload the data based on on the database. Right now I’m using:

Sub populateSheet()

Dim sht As Worksheet
Dim db As Database
Dim rs As Recordset


Set db = OpenDatabase("c:\myDB.mdb")
Set rs = db.OpenRecordset("myData")
Set sht = ThisWorkbook.Sheets("my output")
With sht
    .Cells.value=empty
    For c = 0 To rs.Fields.Count - 1
        .Cells(1, c + 1) = rs.Fields(c).name
    Next
    .Range("a2").CopyFromRecordset rs
End With

End Sub

sub buildTable()

dim ws as workspace
dim db as database
dim dbPath as string

set ws=dbengine.workspaces(0)
set db=ws.createdatabase("c:\myDB.mdb")
db.execute "create table myData (field1 text,field2 text)"
db.execute "insert into myData (field1,field2) values (""1"",""a"")"
db.execute "insert into myData (field1,field2) values (""2"",""b"")"
db.execute "insert into myData (field1,field2) values (""3"",""a"")"

db.close

end sub
sub test()
 buildTable
 populateSheet
end sub

When I run .cells.clear, it wipes out the Autofilter. Is there a way I can keep the Autofilter settings so the new data will be filtered the same way? Or maybe record them and re-apply the same settings? I tried working with this solution, but I had trouble getting it to detect which columns were filtered.

EDIT:

I applied Jean-FrançoisCorbett’s approach in the above code, but it has a problem. Try this with a test table:

1 a
2 b
3 a

After you run populateSheet, then autofilter the 2nd column to only include “a”, the worksheet shows:

1 a
3 a

Then run populateSheet again, the sheet now shows:

1 a
1 a

If you remove the autofilter, rerun populateSheet, and re-apply the autofilter, you get the correct data, but that’s a very cumbersome extra step to get correct output.

EDIT:
I added code to create a database and make a table that you can use to test populateSheet, and changed some of the arguments in populateSheet to reflect this test database.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T10:10:14+00:00Added an answer on May 27, 2026 at 10:10 am

    Ok, I have a solution for this. Basically, I record the autofilter criteria in an array of variants, then remove the Autofilter settings, populate the sheet (using @Jean-FrançoisCorbett’s suggestion of sht.Cells.Value = Empty) then reapply the settings from the array. Code as follows:

    Sub reapplyAutofilter()
    
    Dim fltr As Filter
    Dim columnCriteria1() As Variant
    Dim columnCriteria2() As Variant
    Dim columnOperators() As Variant
    
    Dim filterCriteria() As Variant
    Dim sht As Worksheet
    Dim rangeAddr As String
    Dim fieldCount As Long
    Dim ctr As Long
    
    
    
    Set sht = ThisWorkbook.Sheets("mySheet")
    
    'don't analyze autofilter if it's not on
    If Not sht.AutoFilterMode Then              
        populateSheet
        Exit Sub
    End If
    
    
    'put autofilter settings in arrays for criteria1, criteria2, and operator    
    With sht.AutoFilter
        rangeAddr = .Range.Address                 
        fieldCount = .Filters.Count
        ReDim columnCriteria1(1 To fieldCount)
        ReDim columnCriteria2(1 To fieldCount)
        ReDim columnOperators(1 To fieldCount)
        For ctr = 1 To fieldCount
            With .Filters(ctr)
                If .On Then
                    columnCriteria1(ctr) = .Criteria1
                    columnOperators(ctr) = .Operator
                    If (.Operator = xlOr) or (.Operator=xlAnd) Then
                        columnCriteria2(ctr) = .Criteria2
                    Else
                        columnCriteria2(ctr) = Null
                    End If
                Else
                    columnCriteria1(ctr) = Null
                End If
            End With
        Next ctr
    End With
    
    'clear autofilter     
    sht.AutoFilterMode = False
    
    populateSheet
    
    
    're-apply autofilter settings    
    With sht.Range(rangeAddr)
        For ctr = 1 To fieldCount
            If Not IsNull(columnCriteria1(ctr)) Then
                If IsNull(columnCriteria2(ctr)) Then
                    .AutoFilter Field:=ctr, Criteria1:=columnCriteria1(ctr), Operator:=xlFilterValues
                Else
                    .AutoFilter Field:=ctr, Criteria1:=columnCriteria1(ctr), Criteria2:=columnCriteria2(ctr), Operator:=columnOperators(ctr)
                End If
            End If
        Next
    End With
    
    
    End Sub
    

    I’m not sure if this counts as me solving the problem, because I was inspired by Jean-FrançoisCorbett’s code. I did have to figure out on my own how to capture and reapply the Autofilter settings. Feel free to chime in about who should get credit on this one; otherwise, I’ll accept my own answer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I am writing an app with both english and french support. The app requests
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.