473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Trying to capture the values being filtered in AutoFilter and reapply in Excel

I'm trying to capture the values that are currently being Autofiltered by the user so I can re-apply them after I'm done with the code. Basically, if I can capture these four or five main column's values, store them, then I'm good. I would then go thru, filter the spreadsheet for what I need...do my update and reapply the original filter. Does that make sense? Anyone ever done this?
Nov 24 '09 #1
3 3015
Guido Geurs
767 Expert 512MB
dear,

I have tried and searched but nothing found.
Is it not simpler to write the filter in VBA instead of using the autofilter ?
Is the filter that You want to use complex ?
With a written filter it is also much easyer to capture the data.

br,
Nov 29 '09 #2
Guido Geurs
767 Expert 512MB
dear,

This will probably solve your problem.
See attachment

Open a new workbook with:
Range in A1 to .... with randum numbers from 1 to 30
Command with name "ComFilter"
VBA prg.=

=============================================
Dim FILTER_VALUES()

Private Sub Com_Filter_Click()
Call CopyFilter
End Sub

Sub CopyFilter()
Dim FILTERRANGE As Range
Dim RESULTSRANGE As Range
Dim I As Integer
Range("A1").Select
Selection.AutoFilter field:=1, Criteria1:=">11", Operator:=xlAnd, _
Criteria2:="<20"
'§ capture filtered rows
With ActiveSheet
With .AutoFilter.Range
On Error Resume Next
Set RESULTSRANGE = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
'§ copy cells
If RESULTSRANGE Is Nothing Then
MsgBox "No data to copy"
Else
Set FILTERRANGE = .AutoFilter.Range
FILTERRANGE.Offset(1, 0).Resize(FILTERRANGE.Rows.Count - 1).Copy _
Destination:=.Range("D1")
End If
'§ reset filter
.ShowAllData
.AutoFilterMode = False
End With
'§ Fill array
Range("D1").Select
ReDim FILTER_VALUES(0)
Do Until ActiveCell.Value = ""
FILTER_VALUES(UBound(FILTER_VALUES)) = ActiveCell.Value
ActiveCell.Offset(1, 0).Activate
ReDim Preserve FILTER_VALUES(UBound(FILTER_VALUES) + 1)
Loop
'§ Clear D
Columns("D").Select
Selection.Clear
'§ Dump array to E1
Range("E1").Select
For I = LBound(FILTER_VALUES) To UBound(FILTER_VALUES)
ActiveCell.Value = FILTER_VALUES(I)
ActiveCell.Offset(1, 0).Activate
Next
End Sub

================================================== =

br, (;o})
Attached Files
File Type: zip Filter in array and dump array_END.zip (12.9 KB, 80 views)
Dec 2 '09 #3
Guido Geurs
767 Expert 512MB
Dear,

This is for more than 1 coll.
See attachment

============================================
Dim FILTER_VALUES() As ARRAYRECORD
Private Type ARRAYRECORD
Numbers As Integer
Strings As String
End Type

Private Sub Com_Filter_Click()
Call CopyFilter
End Sub

Sub CopyFilter()
Dim FILTERRANGE As Range
Dim RESULTSRANGE As Range
Dim I As Integer
Range("A1").Select
Selection.AutoFilter Field:=1, Criteria1:=">10", Operator:=xlAnd, _
Criteria2:="<20"
Selection.AutoFilter Field:=2, Criteria1:="=*a*"
'§ capture filtered rows
With ActiveSheet
With .AutoFilter.Range
On Error Resume Next
Set RESULTSRANGE = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
End With
'§ copy cells
If RESULTSRANGE Is Nothing Then
MsgBox "No data to copy"
Else
Set FILTERRANGE = .AutoFilter.Range
FILTERRANGE.Offset(1, 0).Resize(FILTERRANGE.Rows.Count - 1).Copy _
Destination:=.Range("D1")
End If
'§ reset filter
.ShowAllData
.AutoFilterMode = False
End With
'§ Fill array
Range("D1").Select
ReDim FILTER_VALUES(0)
Do Until ActiveCell.Value = ""
ReDim Preserve FILTER_VALUES(UBound(FILTER_VALUES) + 1)
FILTER_VALUES(UBound(FILTER_VALUES)).Numbers = ActiveCell.Value
FILTER_VALUES(UBound(FILTER_VALUES)).Strings = ActiveCell.Offset(0, 1).Value
ActiveCell.Offset(1, 0).Activate
Loop
'§ Clear D
Columns("D:E").Select
Selection.Clear
'§ Dump array to E1
Range("F1").Select
For I = LBound(FILTER_VALUES) + 1 To UBound(FILTER_VALUES)
ActiveCell.Value = FILTER_VALUES(I).Numbers
ActiveCell.Offset(0, 1).Value = FILTER_VALUES(I).Strings
ActiveCell.Offset(1, 0).Activate
Next
End Sub

==============================================
Attached Files
File Type: zip Filter 2 colls in array and dump array_END.zip (14.6 KB, 122 views)
Dec 2 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Matthew Kramer | last post by:
I have two questions: 1) Does anyone know how to do the equivalent of an autofilter in Excel but in an Access table? This enables you to see a list of all the types of names or categories in...
1
by: CCC | last post by:
Our company have used Excel to filter out documents base on date, from_company, type_of_document, country to generate a list of where the documents physical location is. The number of records have...
3
by: rquinnan | last post by:
Good Evening all, I would greatly appreciate any assistance on this Access 2003 quandary I'm in. And I do apologize if this has been answered somewhere else, I didn't see one that addressed my...
2
by: JM | last post by:
Anyone know how to do an autofilter on a workbook in C#? Any snippets of code or reference material would be helpful!
0
by: Amy B | last post by:
Hi, I am using the code below to dynamically build an html table and write it to excel an excel spreadsheet. This code works great except that I need the headers use AutoFilter on the headers,...
0
by: Jon B | last post by:
Hi Everyone! I'm in need of help again! I'm just wondering if WinForm DataGrid can have Excel like AutoFilter? Here's an example: http://www.dotnetforce.com/Content.aspx?t=a&n=210 But again,...
0
by: denvercr | last post by:
Hi Guys, I need to embed a dropdown list in my excel automation in C#, Do you have any idea how to use Excel.Range.AutoFilter method? or any suggested sites where I can learn this thing?.. Thank...
4
by: rnot | last post by:
Hello, I would like to paste data at a cell found by the Selection.Autofilter function. My two first columns hold a "scenario" and "replication" data. I would like upon running a macro to...
1
by: jonathanD | last post by:
Excel Gurus, I am suffering with a problem with the following Code Me.ListBox1.List = Range("a1:f10").Cells.SpecialCells_(xlCellTypeVisible).VALUE The listbox will popoulate...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.