VBA code to filter data

STEVENS3010

Board Regular
Joined
Feb 4, 2020
Messages
87
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi all,

I have a sheet with 5 columns of data that I need filter one column for a a specific value. I've used the macro recorded to help me; the script is below...

VBA Code:
Selection.AutoFilter
    ActiveSheet.Range("$A$2:$E$5654").AutoFilter Field:=3, Criteria1:=Array( _
        "AWAIT DIP UNDERWRITING"), _
        Operator:=xlFilterValues

My questions are, based on the above script, am I correct in assuming it will only filter the range between A2:E5654? If so, as the number of rows of data will be changing, is it possible to code the script to run between columns A and E, but to automatically look for the last row, rather than specifying a particular row number?

My second question then was around copying and pasting the filtered data to a new sheet. Is it possible? If so, is it clever enough to only copy and paste the filtered data, even if this changes every time the filter is applied?

If anybody could help I'd really appreciate it.

Thanks.
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
Assuming you have a header row in row 2 & there are no blank rows in the data, you can use
VBA Code:
Sub stevens()
   With ActiveSheet
      .Range("A2:E2").AutoFilter 3, "AWAIT DIP UNDERWRITING"
      .AutoFilter.Range.Offset(1).Copy Sheets("Sheet2").Range("A2")
   End With
End Sub
 
Upvote 0
Assuming you have a header row in row 2 & there are no blank rows in the data, you can use
VBA Code:
Sub stevens()
   With ActiveSheet
      .Range("A2:E2").AutoFilter 3, "AWAIT DIP UNDERWRITING"
      .AutoFilter.Range.Offset(1).Copy Sheets("Sheet2").Range("A2")
   End With
End Sub

This is brilliant, thanks so much. One thing I did forget to ask was whether it was also possible to sort the filtered data by alphabetical order for column E?
 
Upvote 0
Just turn on the macro recorder & do it manually, then you will have the code. :)
 
Upvote 0
Hi, me again.

If I were to use the below code to filter for more than one value e.g. "Await DIP Underwriting", "Await application" etc... how would I need to amend the code?

Assuming you have a header row in row 2 & there are no blank rows in the data, you can use
VBA Code:
Sub stevens()
   With ActiveSheet
      .Range("A2:E2").AutoFilter 3, "AWAIT DIP UNDERWRITING"
      .AutoFilter.Range.Offset(1).Copy Sheets("Sheet2").Range("A2")
   End With
End Sub

Thanks
 
Upvote 0
How about
VBA Code:
Sub stevens()
   Dim Ary As Variant
   
   Ary = Array("AWAIT DIP UNDERWRITING", "Await application", "Something else")
   With ActiveSheet
      .Range("A2:E2").AutoFilter 3, Ary, xlFilterValues
      .AutoFilter.Range.Offset(1).Copy Sheets("Sheet2").Range("A2")
   End With
End Sub
 
Upvote 0
Really appreciate you helping me out again - thanks so much.

I've just added the code and amended some of it to add addtional values and it keeps coming up with an error... THis part of the code seems to be the issue?

VBA Code:
   Ary = Array("APPLICATION RECEIVED", "AWAIT CREDIT SANCTION", "AWAIT CREDIT UNDERWRITING", _
    "AWAIT INSTRUCT SURVEY, "AWAIT PRESENT FOR UNDERWRITING", _
   "AWAIT SURVEY REPORT", "AWAIT UNDERWRITING")

I'm guessing I'm doing something wrong but I don't know what exactly?
 
Upvote 0
You're missing a "
Rich (BB code):
"AWAIT INSTRUCT SURVEY",
 
Upvote 0

Forum statistics

Threads
1,214,819
Messages
6,121,749
Members
449,050
Latest member
excelknuckles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top