Hello, I am having a problem that I can't resolve and could use some help. I have attached a screenshot and my code for reference. In the screenshot, in column "P" under Reference #, I currently have a working code that will find me the max value by group based on the first two letters, but I would also like to find the max value based on the project names under the "U" column. For an example, if I were to choose "PROJECT 4" then the reference numbers corresponding to "PROJECT 4" would find me the max value for reference number by the group.
Any help is appreciated.
Any help is appreciated.
VBA Code:
Sub Create_RefNr(uf As Object)
Set WbDb = Workbooks("DataBase.xlsx")
Set WbCd = ThisWorkbook
Set ShDb = WbDb.Sheets("TbExpense")
' Application.ScreenUpdating = False
WbDb.Activate
ShDb.Activate
Range("A1").Select
If uf.cbo_RefNr = "PU" Or uf.cbo_RefNr = "RE" Or uf.cbo_RefNr = "DE" Or uf.cbo_RefNr = "TR" Then
Dim i As Long, a As Long, b As Long
Dim X As String, txt As String
Dim va As Variant, ar As Variant
Dim d As Object
Set d = CreateObject("scripting.dictionary")
va = ShDb.Range("p7", Cells(Rows.Count, "p").End(xlUp))
X = uf.cbo_RefNr.Text
On Error Resume Next
For i = 1 To UBound(va, 1)
ar = Split(va(i, 1), "-")
If UCase(ar(0)) = UCase(X) Then
d(CLng(ar(1))) = ""
End If
Next
On Error Resume Next
a = 1 'Application.Min(d.Keys)
b = Application.Max(d.keys)
uf.cbo_RefNr.Text = Left(uf.cbo_RefNr.Text & "-", 3) & Format(Right(b, 5) + 1, "00000")
' For i = a To b
' If Not d.Exists(i) Then txt = txt & ";" & i
' Next
' txt = Right(txt, Len(txt) - 1)
' Me.ListBox2.List = Split(txt, ";")
End If
' WbDb.Close
Set WbDb = Nothing
Set ShDb = Nothing
Set WbCd = Nothing
End Sub