VBA code to filter a specific column

xxbc320xx

New Member
Joined
Mar 27, 2020
Messages
14
Office Version
  1. 365
Platform
  1. Windows
I have a large workbook that routinely pull a sheet from and email. I have a code to email but I need to set up a filter on a specific column before emailing. This column will change each week BUT will always be the 9th non-hidden column. Is there a way I create a line of code that will filter this column?
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Code:
sub FilterMyList()
dim rng as range

range("A1").select
set rng= ActiveSheet.UsedRange
Selection.AutoFilter
rng.AutoFilter Field:=9, Criteria1:="Overdue"
end sub
 
Upvote 0
The range "A1" will change time depending on what criteria. For example. One time it may be Column I another it may be Column HS. It will always be the 9th active column in the sheet.
Maybe I am misreading your code.
Code:
sub FilterMyList()
dim rng as range

range("A1").select
set rng= ActiveSheet.UsedRange
Selection.AutoFilter
rng.AutoFilter Field:=9, Criteria1:="Overdue"
end sub
 
Upvote 0
I am stuck.
I tried adding the rng autofilter into my code and keep getting an error. I am trying to have all this run in the backround.
I have a code that takes the page on the existing sheet, copies all the data (much of it is results of vlookups and calculations), pasts to another sheet as values only, opens outlook, opens a new message, attaches this new sheet, then deletes the temp sheet.
If I filter by greater than 0 and then run the code I get an error with the copy/paste area not matching. If I run without filtering it is fine. I don't mind not filtering the main copy, but the emailed copy needs to be filtered.
Here is the part giving me trouble.
VBA Code:
With Destwb.sheets(1).UsedRange
            .Cells.Copy
            .Cells.PasteSpecial xlPasteValues
            .Cells(1).Select
            .rng.AutoFilter Field:=9, Criteria1:=">0"
        End With
       Application.CutCopyMode = False
 
Upvote 0
Also
I opened a new workbook to just test this in general and it always filters the same column, regardless of whether or not it is hidden. I need to filer the 9th UNHIDDEN column.
 
Upvote 0

Forum statistics

Threads
1,215,754
Messages
6,126,680
Members
449,328
Latest member
easperhe29

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