0

I have a problem with the .Match function. If I set a range inside it with quotes and letters ("A:A") it goes well. But when I try to set it with variable, it gives a 1004 error.

This works:

a = WorksheetFunction.Match(Range("A1"), Sheets("Data").Range("A:A"), 0)

This doesn't work:

a = WorksheetFunction.Match(Range("A2"), Sheets("Data").Range(Cells(a, 4), Cells(a + 5, 4)), 0)

How do I set a range with variables in a .Match function?

2 Answers 2

2

you must use fully qualified (up to worksheet object) range references, otherwise they default to currently "active" sheet

  a = WorksheetFunction.Match(Range("A2"), Sheets("Data").Range(Sheets("Data").Cells(a, 4), Sheets("Data").Cells(a + 5, 4)), 0)
Sign up to request clarification or add additional context in comments.

3 Comments

however I have a variable range, so is it possible to find another way to set it with cells property, may be?
It is a different issue from the question posted, isn't it? If so then make a new post. In the meanwhile you may want to accept the answer if it solved your original question. Thank you
@GasiakA. - if you have a variable range, change the value of the variable a (or change the hard-coded 5 and 4 to be variables if they are the bits that need to change)
1

You might find it easier to use Resize() here:

a = WorksheetFunction.Match(Range("A2"), _
                            Sheets("Data").Cells(a, 4).Resize(6, 1), 0)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.