I want to preface this by saying I have no idea whatsoever why my code is doing what it is doing. I'm really hoping one of the VBA gurus on here can help. Also, this is my first post so I did my best to follow the rules, but if I did anything wrong please point it out.
I have a sub that iterates through a column of data and creates an array. It calls a function that checks to see if the particular value is already in the array. If it is not, then the array is re-dimensioned, the value is inserted, and the process begins again, continuing until the end of the list is reached. I end up with an array totaling 41 values, but 4 of them have been duplicated twice, so there are only 37 unique values in the array.
I can't for the life of me figure out what sets these values apart or why they are being duplicated. The total list is 700+ values long so I figured I should see other values duplicated, but I'm not.
Here is the code for the sub that creates the array:
Sub ProductNumberArray(strWrkShtName As String, strFindColumn As String, blAsGrp As Boolean, iStart As Integer)
Dim i As Integer
Dim lastRow As Integer
Dim iFindColumn As Integer
Dim checkString As String
With wbCurrent.Worksheets(strWrkShtName)
iFindColumn = .UsedRange.Find(strFindColumn, .Range("A1"), xlValues, xlWhole, xlByColumns).Column
lastRow = .Cells(Rows.Count, iFindColumn).End(xlUp).row
For i = iStart To lastRow
checkString = .Cells(i, iFindColumn).Value
If IsInArray(checkString, arrProductNumber) = False Then
If blAsGrp = False Then
ReDim Preserve arrProductNumber(0 To j)
arrProductNumber(j) = checkString
j = j + 1
Else
ReDim Preserve arrProductNumber(1, 0 To j)
arrProductNumber(0, j) = .Cells(i, iFindColumn - 1).Value
arrProductNumber(1, j) = checkString
j = j + 1
End If
End If
Next i
End With
End Sub
And here is the code that checks to see if the checkString value is in the array:
Function IsInArray(stringToBeFound As String, arr As Variant) As Boolean
Dim bDimen As Byte, i As Long
On Error Resume Next
If IsError(UBound(arr, 2)) Then bDimen = 1 Else bDimen = 2
On Error GoTo 0
Select Case bDimen
Case 1
On Error Resume Next
IsInArray = Application.Match(stringToBeFound, arr, 0)
On Error GoTo 0
Case 2
For i = 1 To UBound(arr, 2)
On Error Resume Next
IsInArray = Application.Match(stringToBeFound, Application.Index(arr, , i), 0)
On Error GoTo 0
If IsInArray = True Then Exit For
Next
End Select
End Function
Any help at all will be most welcome. I've been able to find answers to all my questions previously (or at least debug and see an obvious problem) but this one has stumped me. I hope someone can figure out what is going on.
[EDIT] Here is the code where the sub is being called:
Sub UpdatePSI()
Set wbCurrent = Application.ActiveWorkbook
Set wsCurrent = wbCurrent.ActiveSheet
frmWorkbookSelect.Show
If blFrmClose = True Then 'if the user closes the selection form, the sub is exited
blFrmClose = False
Exit Sub
End If
Set wsSelect = wbSelect.Sheets(1)
Call ProductNumberArray("Forecast", "Item", True, 3)
wbCurrent, wsCurrent, and blFrmClose are defined in the general declarations.
Scripting Dictionaryinstead, as it has an.Existsmethod you can use to easily create a distinct list. It can then later be turned into anArrayScripting Dictionariesdon't allow me to do that. @SJR If the value is not found then the function returnsFalseand the value is added. If the value is found then the function returnsTrueand theIfstatement is skipped.KeyandValueparameters; or you could store both parts as theValuein an array.