避免將屬性或方法的名稱重新用作變數

將屬性或方法的保留名稱重新用作你自己的過程和變數的名稱通常不被視為最佳實踐

錯誤的表單 - 雖然以下(嚴格來說)是合法的,但是工作程式碼重新使用 Find 方法以及地址屬性可能會導致名稱歧義的問題/衝突,並且通常很容易混淆。

Option Explicit

Sub find()
    Dim row As Long, column As Long
    Dim find As String, address As Range
    
    find = "something"
    
    With ThisWorkbook.Worksheets("Sheet1").Cells
        Set address = .SpecialCells(xlCellTypeLastCell)
        row = .find(what:=find, after:=address).row        '< note .row not capitalized
        column = .find(what:=find, after:=address).column  '< note .column not capitalized
        
        Debug.Print "The first 'something' is in " & .Cells(row, column).address(0, 0)
    End With
End Sub

良好的形式 - 將所有保留字重新命名為原始的近似但獨特的近似值,避免了任何潛在的命名衝突。

Option Explicit

Sub myFind()
    Dim rw As Long, col As Long
    Dim wht As String, lastCell As Range
    
    wht = "something"
    
    With ThisWorkbook.Worksheets("Sheet1").Cells
        Set lastCell = .SpecialCells(xlCellTypeLastCell)
        rw = .Find(What:=wht, After:=lastCell).Row         '◄ note .Find and .Row
        col = .Find(What:=wht, After:=lastCell).Column     '◄ .Find and .Column
        
        Debug.Print "The first 'something' is in " & .Cells(rw, col).Address(0, 0)
    End With
End Sub

雖然有時你可能想要根據自己的規格有意重寫標準方法或屬性,但這些情況很少見。在大多數情況下,請遠離為自己的構造重用保留名稱。