我是VBA的新手,並且遇到了一些問題,特別是在語法方面。我試圖做一個函數,計算有多少重複的單元格,我有一個通過範圍內(現在,我假設應該只通過1列的範圍)。在函數中操作類型Range的參數(VBA - excel)
我有以下幾點:
Public Function countRepeated(ByVal pRange As range) As Integer
Dim numberOfRows As Integer
Dim numberOfColumns As Integer
Dim repeated As Integer
Dim localRange As range
Set localRange = pRange
numberOfRows = localRange.Rows.Count
numberOfColumns = localRange.columns.Count
If (numberOfColumns > 1) Then
temp = MsgBox("Insira intervalos com apenas 1 coluna.", vbExclamation, "Erro")
countRepeated = -1
Exit Function
End If
repeated = 0
For i = 1 To numberOfRows
temporary = localRange.Cells(i, 1).Value
For j = i + 1 To numberOfRows
If (temporary = localRange.Cells(j, 1).Value And temporary <> "") Then
repeated = repeated + 1
localRange.Cells(j, 1).Value = ""
'after the previous instruction, i get thet #VALUE! error
'i also try set localRange.Cells(j,1).Value = ""
'and localRange.Cells(j, 1).Value = 0
End If
Next j
Next i
countRepeated = repeated
End Function
但我得到一個#VALUE!
錯誤之後,我試圖改變從範圍內的值。最初,我試圖通過傳遞'ByVal'來修改參數本身(pRange),但我得到了同樣的錯誤。
感謝查爾斯,這正是問題。 –