View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Howard Howard is offline
external usenet poster
 
Posts: 536
Default Inaccurate count with code

Do see why this would return inaccurate counts of the values listed in F1:F4sheet 1? There are three sheets in the workbook. I have checked for leading-trailing spaces, copied and pasted to assure spelling and upper case is same - same.

If I copy and paste Reject in say two sheet. should return 2, maybe will or maybe returns 0. Tried several seneraios to try to detect a trend but it is too random to make any assumptions.

The Msgbox and Range print out depict the same errors.

These are in F1:F4, sheet 1.
Approved
Reject
Touched
PI


Option Explicit

Sub TheCountOfFour()

Dim wks As Worksheet
Dim rng As Range, c As Range
Dim i As Variant, ii As Long
Dim j As Variant, jj As Long
Dim k As Variant, kk As Long
Dim l As Variant, ll As Long

i = Range("F1").Value
j = Range("F2").Value
k = Range("F3").Value
l = Range("F4").Value

Set rng = Range("A1:A250")

For Each wks In ActiveWorkbook.Worksheets
For Each c In rng
If c.Value = i Then ii = ii + 1
If c.Value = j Then jj = jj + 1
If c.Value = k Then kk = kk + 1
If c.Value = l Then ll = ll + 1
Next
Next wks

MsgBox "You have " & ii & " " & i & ", " & " " _
& jj & " " & j & ", " & " " _
& kk & " " & k & ", " & " " _
& ll & " " & l, vbOKOnly, "Count Four"

' Range("G1").Value = ii
' Range("G2").Value = jj
' Range("G3").Value = kk
' Range("G4").Value = ll

End Sub

Regards,
Howard