VB Question - AutoFilter related - 'Criteria1:' secion of my code

  • Thread starter Thread starter Midnight
  • Start date Start date
M

Midnight

Hi,

I have a simple bit of code to take me to another worksheet and
automatically select specific information falling under a autofilter.
Sample below:

Sheets("Sheet2").Select
Selection.AutoFilter Field:=1, Criteria1:="RandomText"
Range("A1").Select

This works great but it would be even better if I could refer to a
cell within the spreadsheet. How do I go about changing the criteria
to be a cell reference?

Since I have multiple sheets, do I need to specify the sheet name that
I'm calling the cell reference from?


Many, many thanks if you can help with this.
 
Selection.AutoFilter Field:=1, _
Criteria1:=worksheets("othersheet").range("a177").value

may work ok.
 
That works. Thanks Dave. Now there is one other thing that is not
essential, but will improve things even further. Is there a way of
looking at two cell ranges in the criteria instead of just the one.

To put it another way, in the same way you can go =E8&" "&F8 in a cell
- is something like that possible for the purpose of criteria in the
VB code?

I have no idea how this would be written - whether it would mean
putting in a AND command (if there is such a thing) or if it could be
included somehow in the range section.

Thanks.
 
Just concatenate your strings (I think):

Selection.AutoFilter Field:=1, _
Criteria1:=worksheets("othersheet").range("a177").value & " " _
worksheets("adifferent").range("x999").value
 
Hi Dave,

The code worked good but I had to take out the " " _ - I can see
why you put that in, and it's exactly what I need (to have a space
between the two values) but VB didn't allow it. Is there another way
you can think of doing it? I tried taking out the underscore but no
joy.

As a workaround, I can add a space to the end of the text string of
the first cell value (at the source), but it would be better if I
could incorporate the space into the code.

Jon.
 
I omitted an ampersand.

Criteria1:=worksheets("othersheet").range("a177").value & " " _
& worksheets("adifferent").range("x999").value

Sorry.
 
I omitted an ampersand.

Criteria1:=worksheets("othersheet").range("a177").value & " " _
& worksheets("adifferent").range("x999").value

Sorry.




The code worked good but I had to take out the " " _ - I can see
why you put that in, and it's exactly what I need (to have a space
between the two values) but VB didn't allow it. Is there another way
you can think of doing it? I tried taking out the underscore but no
joy.
As a workaround, I can add a space to the end of the text string of
the first cell value (at the source), but it would be better if I
could incorporate the space into the code.



--

Dave Peterson- Hide quoted text -

- Show quoted text -

Thanks for your help Dave. It now works just as I wanted.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top