1

I don't understand syntax for range.

Why does this work:

For i = 1 To 10
    Range("A" & i & ":D" & i).Copy
Next

But this doesn't work:

For i = 2 To lastRow
    num = WorksheetFunction.Match(Cells(i, 1), Range("A" & lastRow), 0)
 Next       

Why do I need to use

For i = 2 To lastRow
    'num = WorksheetFunction.Match(Cells(i, 1), Range("A1:A" & lastRow), 0)
Next

What A1:A mean? Why can't I use

Range("A" & lastRow), 0
8
  • You're specifying the start and end cells for a range - "A1:A" isn't what you're using, but "A1:A3" to express a range - a single cell range could just be "A1:A1", etc. Commented May 22, 2015 at 20:20
  • OK, that makes sense for second line, but the syntax for first seems odd. Commented May 22, 2015 at 20:31
  • I went ahead and posted an answer that tried to explain this in a bit more detail. Hopefully that will help :) Commented May 22, 2015 at 20:33
  • It would read easier as Range("A" & i ":" & "D" & i) or Range("A" & i & ":" & "D" & i) but the compiler wants that certain format, so I just have to refer to this thread instead of thinking!. Thanks! Commented May 22, 2015 at 20:40
  • 1
    The MATCH function is expecting an array of cells in its lookup_array parameter. =MATCH(A1, A1, 0) works on a worksheet due the error control overhead assuming 'close enough'. It isn't working in VBA because the bar for correct syntax is a bit higher. Use num = WorksheetFunction.Match(Cells(i, 1), Array(Range("A" & lastRow)), 0) to tell it explicitly that you are passing an array of one cell into the lookup_array parameter. Commented May 22, 2015 at 22:53

2 Answers 2

3

There is nothing wrong with your syntax and your code should've work just fine.
The problem with using worksheet function like Match, Vlookup and other look up functions is that if the value being searched is not found, it throws up an error.

In your case, you are trying to search multiple values in just one cell.
So let us say your lastrow is 9. You're code will loop from Cell(2,1) to Cell(9,1) checking if it is within Range("A" & lastrow) or Range("A9").

If your values from Cell(2,1) through Cell(9,1) is the same as your value in Range("A9"), you won't get an error.

Now, if you use Range("A1:A" & lastrow), it will surely work cause you are trying to match every element of that said range to itself and surely a match will be found.

WorksheetFunction.Match(Cells(2,1), Range("A1:A9")) 'will return 2
WorksheetFunction.Match(Cells(3,1), Range("A1:A9")) 'will return 3
'
'
'And so on if all elements are unique

It doesn't matter if you use Range("A9") or Range("A1:A9").
What matters is that you handle the error in case you did not find a match.
One way is to use On Error Resume Next and On Error Goto 0 like this:

Sub ject()
    Dim num As Variant
    Dim i As Long, lastrow As Long: lastrow = 9

    For i = 2 To lastrow
        On Error Resume Next
        num = WorksheetFunction.Match(Cells(i, 1), Range("A" & lastrow), 0)
        If Err.Number <> 0 Then num = "Not Found"
        On Error GoTo 0
        Debug.Print num
    Next
End Sub

Another way is to use Application.Match over WorksheetFunction.Match like this:

Sub ject()
    Dim num As Variant
    Dim i As Long, lastrow As Long: lastrow = 9

    For i = 2 To lastrow
        num = Application.Match(Cells(i, 1), Range("A" & lastrow), 0)
        Debug.Print num
        'If Not IsError(num) Then Debug.Print num Else Debug.Print "Not Found"
    Next
End Sub

Application.Match works the same way but it doesn't error out when it returns #N/A. So you can assign it's value in a Variant variable and use it later in the code without any problem. Better yet, use IsError test to check if a value is not found as seen above in the commented lines.

In both cases above, I used a Variant type num variable.
Main reason is for it to handle any other value if in case no match is found.

As for the Range Syntax, don't be confused, it is fairly simple.
Refer to below examples.

  1. Single Cell - All refer to A1

    Cells(1,1) ' Using Cell property where you indicate row and column
    Cells(1) ' Using cell property but using just the cell index
    Range("A1") ' Omits the optional [Cell2] argument
    

    Don't be confused with using cell index. It is like you are numbering all cells from left to right, top to bottom. enter image description here

    Cells(16385) ' refer to A2
    
  2. Range of contiguous cell - All refer to A1:A10

    Range("A1:A10") ' Classic
    Range("A1", "A10") ' or below
    Range(Cells(1, 1), Cells(10, 1))
    

    Above uses the same syntax Range(Cell1,[Cell2]) wherein the first one, omits the optional argument [Cell2]. And because of that, below also works:

    Range("A1:A5","A6:A10")
    Range("A1", "A8:A10")
    Range("A1:A2", "A10")
    
  3. Non-Contiguous cells - All refer to A1, A3, A5, A7, A9

    Range("A1,A3,A5,A7,A9") ' Classic
    
Sign up to request clarification or add additional context in comments.

3 Comments

I obviously stand corrected. I didn't realize my Excel VBA was quite that rusty. Errant answer deleted.
Thanks! Range("A1", "A" & lastRow), 0) works to look in range (A1 to A9) with lastrow=9. MUCH easier to read than Range("A1:A" & lastRow), 0)
L42, very thorough answer, much more than I was expecting. Hopefully this will help others.
1

Without any specific details about the error, I assume that Match does not return the value you expect, but rather an #N/A error. Match has the syntax

=match(lookup_value, lookup_range, match_type)

The lookup_range typically consists of a range of several cells, either a column with several rows or a row with several columns.

In your formula, you have only one cell in the lookup_range. Let's say Lastrow is 10. The first three runs of the loop produce the formula

=Match(A2,A10,0)
=Match(A3,A10,0)
=Match(A4,A10,0)

It is a valid formula but in most cases the result won't be a match but an error. Whereas what you probably want is

=Match(A2,A1:A10,0)

Looking again at your code, stitch it together and find why you need A1:A as a string constant in your formula:

For i = 2 To lastRow
    num = WorksheetFunction.Match(Cells(i, 1), Range("A1:A" & lastRow), 0)
Next

1 Comment

num = WorksheetFunction.Match(Cells(i, 1), Range("A" & lastRow), 0) gives Run-time error 1004. Unable to get the match property of the Worksheet function class. You are correct that I was looking for A1:A10, just didn't understand the syntax

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.