簡體   English   中英

VBA EXCEL范圍語法

[英]VBA EXCEL Range syntax

我不懂范圍的語法。

為什么這樣做:

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

但這不起作用:

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

我為什么需要使用

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

A1:A是什么意思? 為什么我不能用

Range("A" & lastRow), 0

您的語法沒有任何問題,您的代碼應該可以正常工作。
使用MatchVlookup和其他查找函數等工作表函數的問題是,如果找不到要搜索的值,則會引發錯誤。

在您的情況下,您嘗試在一個單元格中搜索多個值。
所以讓我們說你的lastrow是9.你的代碼將從Cell(2,1)循環到Cell(9,1)檢查它是否在Range("A" & lastrow)Range("A9")

如果從Cell(2,1)Cell(9,1)的值與Range("A9")值相同,則不會出現錯誤。

現在,如果你使用Range("A1:A" & lastrow) ,它肯定會起作用,因為你試圖將該范圍的每個元素與自身匹配,並且肯定會找到匹配。

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

使用Range("A9")Range("A1:A9")無關緊要。
重要的是你在沒有找到匹配的情況下處理錯誤。
一種方法是使用On Error Resume NextOn Error Goto 0如下所示:

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

另一種方法是在WorksheetFunction.Match上使用Application.Match ,如下所示:

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工作方式相同,但返回#N/A時不會出錯。 因此,您可以在Variant變量中分配它的值,並在代碼中稍后使用它而不會出現任何問題。 更好的是,使用IsError測試來檢查是否找不到如評論行中所示的值。

在上面的兩種情況中,我使用了Variant類型num變量。
主要原因是如果沒有找到匹配,它將處理任何其他值。

至於范圍語法,不要混淆,這很簡單。
請參閱以下示例。

  1. 單細胞 - 全部參考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 

    不要與使用單元格索引相混淆。 就像你從左到右,從上到下對所有細胞進行編號。 在此輸入圖像描述

     Cells(16385) ' refer to A2 
  2. 連續單元格的范圍 - 全部參考A1:A10

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

    上面使用相同的語法Range(Cell1,[Cell2])其中第一個,省略了optional參數[Cell2] 因此,下面也有效:

     Range("A1:A5","A6:A10") Range("A1", "A8:A10") Range("A1:A2", "A10") 
  3. 非連續單元 - 全部指A1,A3,A5,A7,A9

     Range("A1,A3,A5,A7,A9") ' Classic 

如果沒有關於錯誤的任何具體細節,我認為Match不會返回您期望的值,而是返回#N / A錯誤。 Match具有語法

= match(lookup_value,lookup_range,match_type)

lookup_range通常由幾個單元格組成,可以是包含多個行的列,也可以是包含多個列的行。

在公式中,lookup_range中只有一個單元格。 讓我們說Lastrow是10.循環的前三次運行產生公式

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

這是一個有效的公式,但在大多數情況下,結果不是匹配而是錯誤。 而你可能想要的是

=Match(A2,A1:A10,0)

再看一下你的代碼,將它拼接在一起,找出你需要A1:A作為公式中的字符串常量的原因:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM