I have two list boxes. lb1 and lb2. Items are picked from lb1 and transferred to lb2 with command bt. this command bt run this procdure below.
I wish to have the math only run one for items that exist in lb2 more then once and carry on with math for other items (none duplicate) as needed.
The NameTwice is a "switch" I put in the command bt code which does the transfer from lb1 to lb2 to "fake" the array to skip math for the item being duplicated.
This below array procedure is always reran when new (single or multi, items are added) to lb2
I wish to have the math only run one for items that exist in lb2 more then once and carry on with math for other items (none duplicate) as needed.
The NameTwice is a "switch" I put in the command bt code which does the transfer from lb1 to lb2 to "fake" the array to skip math for the item being duplicated.
This below array procedure is always reran when new (single or multi, items are added) to lb2
VBA Code:
Public Sub TestArrays()
' Return time math
'listbox2
Dim Total As Variant
Dim DatalistArray() As Variant
Dim a As Long, b As Long
Dim sh As Worksheet
Dim i As Integer
Dim NameArray() As String
Dim maxtotal As Variant
Set sh = ThisWorkbook.Sheets("Data")
' MsgBox NameTwice
With Sheet1.ListBox2
ReDim NameArray(.ListCount)
'Load the listbox2 values into the array
For i = 0 To .ListCount - 1
NameArray(i) = .List(i)
Sheet1.Range("B60").Value = i + 1 'stores counted items for total formula
Next i
'print the array values to the debug window
For i = 0 To UBound(NameArray) - 1
Debug.Print i, NameArray(i)
ReDim Preserve DatalistArray(1 To sh.Range("A" & Rows.count).End(xlUp).Row, 1 To 2) 'was 4 not 2
'load the sheet1 values into DataListArray array
For a = 1 To sh.Range("A" & Rows.count).End(xlUp).Row
For b = 1 To 2
DatalistArray(a, b) = sh.Cells(a, b)
Next b
Next a
For a = 2 To UBound(DatalistArray)
Dim LBname As String
LBname = NameArray(i)
'find if lbname exists in DataListArray(a,1) and pull its data in for math
If DatalistArray(a, 1) = LBname And NameTwice <> 2 Then 'nametwice is varible for more then one occurence of customer in listbox2
'math area
nummax = Format(Application.WorksheetFunction.Max(DatalistArray(a, 2)), "h:mm")
Total = Total + DatalistArray(a, 2)
maxtotal = Format(Application.WorksheetFunction.Max(Total), "h:mm")
Sheet1.Range("b56").Value = nummax
Sheet1.Range("b57").Value = maxtotal
Sheet1.Range("b58").Value = Sheet1.TBRalphTimeLeft.Value
Sheet1.returnTimeRalph = Format(Sheet1.Range("b59"), "h:mm AM/PM")
NameTwice = 0 'reset count varible for more then one occurence of customer in listbox2
Exit For
MsgBox "No match found !"
End If
Next a
Next i
End With
NameTwice = 0 'reset count varible for more then one occurence of customer in listbox2
End Sub