View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Count numbers that start with 1

Hi Andy,

The following code ignores blank cells. If you don't ignore blank cells then
needs to be modified to ensure only getting the range with data because
otherwise UsedRange is a bit unreliable but ignoring blank cells overcomes
this. Do all of the cells between the first cell top left and the last cell
bottom right of the range to be searched have data? If so, the following code
should return the correct answer anyway.

Sub CountFirstOne()

Dim rngUsed As Range
Dim c As Range
Dim lngOne As Long
Dim lngTot As Long

'Edit "Sheet1" with your sheet name
With Sheets("Sheet1")
For Each c In .UsedRange
'Don't count blank cells
If c.Value < "" Then
lngTot = lngTot + 1
If Left(c.Value, 1) = "1" Then
lngOne = lngOne + 1
End If
End If
Next c
End With

MsgBox "Cells with the number 1 = " & lngOne _
& vbCrLf & "Total cells looked at = " & lngTot

End Sub

--
Regards,

OssieMac