VBA EXCEL Range syntax

前端 未结 2 1891
有刺的猬
有刺的猬 2020-12-21 21:12

I don\'t understand syntax for range.

Why does this work:

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

B

相关标签:
2条回答
  • 2020-12-21 21:38

    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
      
    0 讨论(0)
  • 2020-12-21 21:43

    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
    
    0 讨论(0)
提交回复
热议问题