Filter using assigned variable

kamal1jacq

New Member
Joined
Feb 4, 2010
Messages
12
Hi,

I am doing a filter using the below code:

filterRange.AutoFilter Field:=11, Criteria1:="<>"

However I do not want to filter as mentioning field as 11

Using Find I found a text "reversed" and assigned it to a variable called "celladdress"

Now cell address shows as K1

So I want to filter using this cell address (variable)

Can this be done and what will be the code.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Try
Code:
Field:=celladdress.column
 
Upvote 0
run time error '424
Object required
Showing error in the below coding

Code:
Sub assgn()


Dim Wrk1 As Workbook
Dim LCol As Long
Dim Rng As Range


'Application.ScreenUpdating = False
'Application.DisplayAlerts = False


C = Application.GetOpenFilename
Set Wrk1 = Workbooks.Open(C)

Wrk1.Activate


Sheets("Sheet1").Select
Cells.Find(What:="Reversed with", After:=ActiveCell, LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
celladdr = Selection.Address(False, False)


MsgBox celladdr


    Dim src As Worksheet
    Dim tgt As Worksheet
    Dim trgt As Worksheet
    Dim filterRange As Range
    Dim copyRange As Range
    Dim lastRow As Long
    Dim visibleTotal As Long


    Set src = Wrk1.Sheets("Sheet1")
    Set tgt = Wrk1.Sheets("Sheet3")
  
src.AutoFilterMode = False
' find the last row with data in column A
    lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
    
    MsgBox lastRow
    
    
  Set filterRange = src.Range("A1:Z" & lastRow)
  Set copyRange = src.Range("A1:Z" & lastRow)
    filterRange.AutoFilter Field:=celladdr.Column, Criteria1:="<>"
 
Last edited by a moderator:
Upvote 0
In future when posting code, please use code tags (the # icon in the reply window).
try
Code:
Sub assgn()


Dim Wrk1 As Workbook
Dim LCol As Long
Dim Rng As Range
Dim Fnd As Range

'Application.ScreenUpdating = False
'Application.DisplayAlerts = False


C = Application.GetOpenFilename
Set Wrk1 = Workbooks.Open(C)

Wrk1.Activate


Sheets("Sheet1").Select
Set Fnd = Cells.find(What:="Reversed with", After:=activecell, lookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)
celladdr = Fnd.Address(False, False)


MsgBox celladdr


    Dim src As Worksheet
    Dim tgt As Worksheet
    Dim trgt As Worksheet
    Dim filterRange As Range
    Dim copyRange As Range
    Dim lastRow As Long
    Dim visibleTotal As Long


    Set src = Wrk1.Sheets("Sheet1")
    Set tgt = Wrk1.Sheets("Sheet3")
  
src.AutoFilterMode = False
' find the last row with data in column A
    lastRow = src.Range("A" & src.Rows.Count).End(xlUp).Row
    
    MsgBox lastRow
    
    
  Set filterRange = src.Range("A1:Z" & lastRow)
  Set copyRange = src.Range("A1:Z" & lastRow)
    filterRange.AutoFilter Field:=Fnd.Column, Criteria1:="<>"
 
Upvote 0
Fantastic, It worked. Amazing.

Thanks for your help.

By the way why do we have to use # icon when posting code.
 
Upvote 0
Glad to help & thanks for the feedback

By the way why do we have to use # icon when posting code.
It helps to preserve formatting, thereby making it easier to read.
Also a number of members will not offer assistance if you haven't used code tags, so it's in your best interest to use them
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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