possible calculation in listbox for debit , credit when search based on textbox ?

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,486
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
Hello
I want calculation in listbox for BALANCE column when search the id in second column in listbox into textbox 4 then should change the calculation .
first I select the sheet name from combobox1 and will populate data in listbox and will populate the last row from sheet into textbox1,2,3
now when search for ID in second column for last part as in pic2 then should calculate like this
the first row in listbox for column (BALANCE)= 0-2200=-2200
second row in listbox for column (BALANCE)=-2200+(-4200)=-6400
third row in listbox for column (BALANCE)= -6400+7000-6200=57400
and should change the lastrow(TOT) in listbox should sum the INPUT column ,OUTPUT column and BALANCE should subtract INPUT from OUTPUT(70000-12600=57400)

first when select sheet from combobox1
1.PNG




result when search in textbox4
2.PNG


VBA Code:
Option Explicit
Option Compare Text

Private Data, Temp, Crit As String, i As Long, lr As Long, ii As Long, x As Long
Dim ws As Worksheet


Private Sub ComboBox1_Change()
'ActiveSheet.Visible = True
 If ComboBox1.Value <> "" Then
  TextBox1.Value = ""
  TextBox2.Value = ""
  TextBox3.Value = ""
  CommandButton1.Enabled = True
  
  End If
  If ComboBox1.Value = "" Then ListBox1.Clear
  If ComboBox1.Value = "" Then TextBox1.Value = "": TextBox2.Value = "": TextBox3.Value = ""
  If ComboBox1.Value <> "" Then
  TextBox1.Visible = True: TextBox2.Visible = True: TextBox3.Visible = True
  
  If ComboBox1.Value = "" Then Exit Sub

Set ws = Sheets(ComboBox1.Value)

ws.Activate

With ws
lr = .Range("A" & Rows.Count).End(xlUp).Row
TextBox1.Value = .Range("C" & lr).Value
TextBox2.Value = .Range("D" & lr).Value
TextBox3.Value = .Range("E" & lr).Value
If TextBox3.Value < 0 Then
TextBox3.ForeColor = vbRed
Else: TextBox3.ForeColor = vbBlack
End If
End With

  Call LBoxPop

  
 End If

End Sub
Private Sub TextBox1_Change()
TextBox1.Value = Format(TextBox1, "#,##0.00")
 Call LBoxPop
End Sub

Private Sub TextBox2_Change()
TextBox2.Value = Format(TextBox2, "#,##0.00")
End Sub
Private Sub TextBox3_Change()
TextBox3.Value = Format(TextBox3, "#,##0.00")
End Sub

Private Sub UserForm_Initialize()
 
 Crit = ""
  Dim i As Long
  For i = 1 To Sheets.Count
   If Sheets(i).Name <> "DATA" And Sheets(i).Name <> "MAIN" Then
   ComboBox1.AddItem Sheets(i).Name
   End If
  Next
  
  If ComboBox1.ListIndex > -1 Then
    Set ws = Sheets(ComboBox1.Value)
    Call LBoxPop
    
  End If
  If ComboBox1.Value = "" Or ComboBox1.Value <> "" Then CommandButton1.Enabled = False
  
      ListBox1.SetFocus



  
End Sub
Private Sub LBoxPop()

    Dim r          As Long, c As Long
    Dim Data()     As Variant
    Dim rng        As Range
    Dim va
     
   Debug.Print ws.Name
    Set rng = ws.Cells(1, 1).CurrentRegion
'
    Data = ws.Cells(1, 1).CurrentRegion.Value
    
    For i = 1 To UBound(Data, 1)
        Data(i, 1) = Format(Data(i, 1), "yyyy-mm-dd")
    Next
 For i = 1 To UBound(Data, 1)
        Data(i, 3) = Format(Data(i, 3), "#,##0.00")
        Data(i, 4) = Format(Data(i, 4), "#,##0.00")
        Data(i, 5) = Format(Data(i, 5), "#,##0.00")
    Next
    With UserForm1.ListBox1
        .ColumnCount = 5
        .columnWidths = "90;300;120;120;100"
        .List = Data
    End With
    
    
        For i = ListBox1.ListCount - 1 To 0 Step -1

    Debug.Print i, ListBox1.List(i, 0)
      If ListBox1.List(i, 0) <> "" Then
        ListBox1.ListIndex = i
        Exit For
      End If
    Next i
  


 
End Sub
I hope this is possible.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top