Afternoon all. I'm working on adding and undetermined number of values (customers) to a variant array using a For/Next loop and ReDim Preserve. My code below:
lRow = sht1.Cells(sht1.Rows.Count, 1).End(xlUp).Row
cCount = 0
uCount = 0
var_Events = sht1.Range("A2:BC" & lRow).Value2
For i = LBound(var_Events) To UBound(var_Events)
ReDim Preserve var_Customers(0 To cCount)
If Not CustInArray(str(var_Events(i, 2)), var_Customers) Then
var_Customers(cCount) = str(var_Events(i, 2))
cCount = cCount + 1
End If
If i Mod 100 = 0 Then
MsgBox "Line: " & i
End If
Next i
Here is the CustInArray function:`
Function CustInArray(str As String, arr As Variant) As Boolean
CustInArray = (UBound(Filter(arr, str)) > -1)
End Function`
I added the Mod/MsgBox after it crashed the first time to see where/when it was crashing with no errors. It gets to about line 6000 before excel crashes (I don't see the "Line: 6000" MsgBox).
I've checked the UBound of the var_Events, and it is 6290 which is in line with the number of lines on my WS. I also tried (UBound(var_Events) - 1), and still no luck.
I'm not 100% why it's crashing since there's no error, so that's all I can provide for now. Thanks in advance!
EDIT: I mentioned this in the comments, but thought it would be good to add here. I initially thought to use dictionaries, but this is just the first part of a longer process. Each customer is going to have an unknown number of items assigned to them, and an unknown number of classes to those items.
Redim Preservean array thousands of times is very resource consuming and probably exhausted the memory. I urge you to start using a Dictionary Object.Crasheswhat exactly does it do? Does excel shut down and go away? Is there an error? Or does it freeze and go white?