Option Explicit
Private Sub UserForm_Initialize()
Dim a, i As Long, Cr As Double, Dr As Double
With Sheets("data").Cells(1).CurrentRegion
a = .Offset(1).Resize(.Rows.Count - 1).Value
End With
ReDim Preserve a(1 To UBound(a, 1), 1 To UBound(a, 2) + 1)
For i = 1 To UBound(a, 1)
a(i, UBound(a, 2)) = a(i, 6) + a(i, 7)
Cr = Cr + a(i, 6): Dr = Dr + a(i, 7)
Next
With Me.ListBox1
.ColumnCount = UBound(a, 2)
.List = a
End With
Me.TextBox2 = Format$(Cr, "#,#.00;-#,#.00;0")
Me.TextBox3 = Format$(Dr, "#,#.00;-#,#.00;0")
Me.TextBox4 = Format$(Cr - Dr, "#,#.00;-#,#.00;0")
End Sub
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim a, x, i As Long, Cr As Double, Dr As Double
Me.ListBox1.Clear
If Me.TextBox1.Value = "" Then UserForm_Initialize: Exit Sub
With Sheets("data").Cells(1).CurrentRegion
x = Filter(.Parent.Evaluate("transpose(if(" & .Columns(3).Address & _
"=""" & Me.TextBox1 & """,row(1:" & .Rows.Count & ")))"), False, 0)
If UBound(x) > -1 Then a = Application.Index(.Value, _
Application.Transpose(x), Evaluate("column(" & .Rows(1).Address & ")"))
End With
If Not IsArray(a) Then Exit Sub
If UBound(x) = 0 Then
ReDim Preserve a(1 To UBound(a) + 1)
a(UBound(a)) = Application.Sum(Application.Index(a, Array(6, 7)))
Cr = a(6): Dr = a(7)
Me.ListBox1.Column = a
Else
ReDim Preserve a(1 To UBound(a, 1), 1 To UBound(a, 2) + 1)
For i = 1 To UBound(a, 1)
a(i, UBound(a, 2)) = a(i, 6) + a(i, 7)
Cr = Cr + a(i, 6): Dr = Dr + a(i, 7)
Next
Me.ListBox1.List = a
End If
Me.TextBox2 = Format$(Cr, "#,#.00;-#,#.00;0")
Me.TextBox3 = Format$(Dr, "#,#.00;-#,#.00;0")
Me.TextBox4 = Format$(Cr - Dr, "#,#.00;-#,#.00;0")
End Sub
Bookmarks