Count the status messages in a column VBA Can someone review my code and provide feedback. For context in column A will be a status message of &quot;OK&quot; or &quot;NOK&quot; and this little function just counts the number of times this appear so I can update a label in another procedure. Not too sure if this is the most efficient way of doing this because looping will create delay going row by row if the data set is very large as in the main procedure this function will be called to update the label is in a loop so depending on the size of the data it can be small or very large and will trigger this function for each row.
Function UploadStatus(ByRef WS As Worksheet, ByVal StartRow As Long, ByVal EndRow As Long, Optional ByVal strMsg As String) As String
Dim OK As Long
Dim NOK As Long
Dim i As Long
Dim uploadMsg As String
 
If StartRow = 0 And EndRow = 0 Then Exit Function
With WS
    For i = StartRow To EndRow
        If .Range(&quot;A&quot; &amp; i).value = &quot;OK&quot; Then
            OK = OK + 1
        Else
            If .Range(&quot;A&quot; &amp; i).value &lt;&gt; vbNullString Then
                NOK = NOK + 1
            End If
        End If
    Next i
End With
 
If OK &lt; 2 Then
    uploadMsg = OK &amp; &quot; OK row, &quot;
Else
    uploadMsg = OK &amp; &quot; OK rows, &quot;
End If
 
If NOK &lt; 2 Then
    uploadMsg = uploadMsg &amp; NOK &amp; &quot; NOK row&quot;
Else
    uploadMsg = uploadMsg &amp; NOK &amp; &quot; NOK rows&quot;
End If
If strMsg &lt;&gt; vbNullString Then
    UploadStatus = strMsg &amp; &quot; &quot; &amp; uploadMsg
Else
    UploadStatus = uploadMsg
End If
End Function
Don't reinvent the wheel. Your homemade function will never be as fast as the built in functions.
Function UploadStatus(ByRef WS As Worksheet, ByVal StartRow As Long, ByVal EndRow As Long, Optional ByVal strMsg As String) As String
  Dim OK As Long
  Dim NOK As Long
  Dim uploadMsg As String
 
  If StartRow = 0 And EndRow = 0 Then 
    Exit Function
  End If
  
  OK = WorksheetFunction.CountIf(&quot;A&quot; &amp; StartRow &amp; &quot;:A&quot; &amp; EndRow, &quot;OK&quot;)
  NOK = WorksheetFunction.CountIf(&quot;A&quot; &amp; StartRow &amp; &quot;:A&quot; &amp; EndRow, &quot;NOK&quot;)

  uploadMsg = ResultStringer(OK &amp; &quot;OK&quot;)
  uploadMsg = uploadMsg &amp; ResultStringer(NOK &amp; &quot;NOK&quot;)
 
  If strMsg &lt;&gt; vbNullString Then
    UploadStatus = strMsg &amp; &quot; &quot; &amp; uploadMsg
  Else
    UploadStatus = uploadMsg
  End If

End Function

Private Function ResultStringer(ByVal Count As Long, ByVal ID as String) as String

  If Count &gt; 1 Then
    ResultStringer = Count &amp; ID &amp; &quot; rows, &quot;
  Else
    ResultStringer = Count &amp; ID &amp; &quot; row, &quot;
  End If

End Function