473,399 Members | 3,832 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,399 software developers and data experts.

How do I filter data using one of two different fields?

7
Greetings,

I have a routine that iterates through many worksheets and Autofilters (using criteria filterStart and filterEnd) a range of data (A8:H200) using (field:=1). The code works well for that. What I want to do is filter using field:=1 unless field:=1 is empty, then I want to filter by another field. I have no idea how to accomplish this.

Expand|Select|Wrap|Line Numbers
  1. Sub ApplyDateFilter()
  2.  
  3.     Dim Ws As Worksheet
  4.     Dim filterStart As Long, filterEnd As Long
  5.     Dim i As Integer, reply As Integer
  6.     filterStart = Range("B1").Value 'assume this is the start date
  7.     filterEnd = Range("B2").Value 'assume this is the end date
  8.  
  9.         If filterStart = 0 Or filterEnd = 0 Then
  10.             reply = MsgBox("Please enter both filter dates!", vbOK, "Filter Dates")
  11.         Else
  12.             Application.ScreenUpdating = False
  13.  
  14.                 For i = 2 To Sheets.Count - 1 'ignores the first and last worksheet
  15.                 Set Ws = Sheets(i)
  16.                 Ws.AutoFilterMode = False 'Remove any existing filters
  17.                 Ws.Range("A8:H200").AutoFilter field:=1, Criteria1:=">=" & filterStart, _
  18.                 Operator:=xlAnd, Criteria2:="<=" & filterEnd
  19.                 Ws.Activate
  20.                 Ws.Range("I1").Select
  21.                 Center_it 'Puts filtered totals in visible window
  22.                 Next i
  23.  
  24.             Sheet1.Select
  25.             Range("B1:B2").Interior.ColorIndex = 3
  26.             Application.ScreenUpdating = True
  27.  
  28.         End If
  29. End Sub
Thanks for any help.
Apr 5 '17 #1
2 6269
SioSio
272 256MB
If you want to skip the empty line and process, it will be as follows. (By the way, named argument names can be omitted)
Expand|Select|Wrap|Line Numbers
  1.  Ws.Range("A8:H200").AutoFilter field:=1, Criteria1:=">=" & filterStart, _
  2.                 Operator:=xlAnd, Criteria2:="<=" & filterEnd
  3.  
  4.  Ws.Range( Ws.Range("A8"), Cells(Rows.Count, 8).End(xlUp)).AutoFilter 1,">=" & filterStart, _
  5.                 xlAnd, "<=" & filterEnd
  6.  
  7.  
Jan 8 '20 #2
Create a new parameter:

data type string
allowable values set to list
add from field - user field
Quick filter on user-sending

select condition tab, By formula
[user.sending]=[user.parameter]
Repeat for user-receiving sheet.

Lastly, right-click user.parameter and select 'add to sheet' (or add it to you dashboard).
Mar 2 '20 #3

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

Similar topics

2
by: RichieTJ | last post by:
-- Access Rookie Question-- Ok here is the situation: I have a form called frmServices, that has three fields called SvcNo, SvcName and SvcCity. I created a button that brings a popup form...
1
by: Randy | last post by:
I have trying to figure out the above topic. Can someone help me with this? I need for staff to be able to go to a dialog box or a form and then be able to enter data in mutliple fields and...
3
by: shaqattack1992-newsgroups | last post by:
Hello Everyone, At work, employees have been using a spreadsheet that I am trying to import into an access database to make some reports. The problem I'm having is that in the spreadsheet, they...
4
by: AtCor | last post by:
I am trying to filter data using count. For a given day and patient, I would like to return all the valid tests. When I use a count this way, it only returns the patients with at least 4 tests. I...
23
nehashri
by: nehashri | last post by:
hi i am designing a database using Ms Access and ASP. i have 3 tables in access namely 'PERSONAL', other as 'POLICY' and 3rd one is named as 'STAFF'. in the contact table i have ID, Name, Children...
13
by: ahd2008 | last post by:
Hi everyone, I have a form for departments and personnel. There are there fields for the sake of search: DepartmentSearch as combo box Division\UnitSearch as combo box Number as combo box ...
3
by: tasawer | last post by:
Hi, I have an accident claims form that I need to requery on one of three different fields. Primary field is AccidentID ClientName (Can appear on more than one AccidentID) SolicitorsRef...
0
by: Andre Saputra | last post by:
Dear all. I really need help for my code. im using text box to filter data in listview, heres the code reloadlistview1 "select * from sales where TANGGAL between #" & Text1.Text & "#" & _ ...
4
by: mesairv | last post by:
Hi everyone. Im new here. I had been reading several topics in line with filtering, but i cannot find one related to my concern. Please help me to create a command button that will filter my...
3
by: DesignsOnline | last post by:
I have a table with many different fields in, including 40 separate image fields. ("Pic1" "Pic2" "Pic3" etc...) each field has a single value (an image name) in, I need to be able to select all...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.