I'm new to VBA and am having some problems, especially with the syntax.
I'm trying to do a function that counts how many repeated cells I have in a passing range (for now, I assume that should pass only a range with 1 column).
I have the following:
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
But I am getting a #VALUE!
error after I try to change a value from the Range. Initially I tried to modify the parameter itself (pRange), by passing it 'ByVal', but I get the same error.