View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
L. Howard L. Howard is offline
external usenet poster
 
Posts: 852
Default Search an array of sheets for an array of numbers & return count of numbers

Range("A2:A10") is a list of numbers.

I want to search each sheet for each number in the list and return occurrence of each number to column B, next to the number.

As posted, code throws 'Subscript out of range' error on the line:
With nNumArr(i)

I had intended to also list the sheet names in column C and write that sheet list to the varSheets array, but have not got that far. Tried some similar things like the numbers list but that failed also, so I just wrote them in the array in the code as you see them.

The numbers list and the sheet list will be much larger in a working code.

Thanks,
Howard

Sub WSnNumCount()

Dim nNumArr() As Variant
Dim nNumCt As Long
Dim varSheets As Variant
Dim i As Long, ii As Long, j As Long
Dim c As Range

nNumArr = Range("A2:A10")

varSheets = Array("Sheet2", "Sheet3", "Sheet4", "Sheet5", "Sheet6")

For i = LBound(nNumArr) To UBound(nNumArr)

With nNumArr(i)

For ii = LBound(varSheets) To UBound(varSheets)

With Sheets(varSheets(ii))
Set c = .UsedRange.Find(What:=nNumArr(i), LookIn:=xlValues)

If Not c Is Nothing Then
j = j + 1
End If

End With

Next ' ii

End With

Range("B" & Rows.Count).End(xlUp)(2) = j

Next 'i
j = 0
End Sub