Updating the selected item from the listbox based on the last column of the row preceding

Alaa mg

Active Member
Joined
May 29, 2021
Messages
343
Office Version
  1. 2019
Hello
Everything works fine for me except for the last column in the list box. What I want when I choose a specific item from the list box and update its data is to reset the balance calculation for the selected item from the list box based on the row that precedes it for the balance column.
Let's take an example of the second row in the list box. In the first picture, the item is selected
In the second picture, the value of the fifth column of the selected element is changed, then the control button is pressed to update . In the third picture, it is marked in red. The value in the last column of the selected element has not changed. The fourth picture is right value where the value is taken from the row that precedes it and the equation is in this form 1233 + 1233 -100 = 2366
Any row that is modified when calculating in the last column must take the value from the row that precedes for the last column and add to the value of the fourth column and subtract it from the fifth column of the selected element.
But if you choose the first row, then the fourth column will be subtracted from the fifth column in case the first row is updated in the List Box.
Last thing I want implemnt the whole codes for two sheets based on selected sheet from combobox


first when select item to update
step2.JPG


second when updtating

step1.JPG


third current wrong gives result
step3.JPG



finally should be
step4.JPG

also posted here
Updating the selected item from the listbox based on the last column of the row preceding
I hope the idea is clear
thanks
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
sorry I forgot post my codes
VBA Code:
Private Sub UserForm_Initialize()
Dim a As Variant
Sheets("ss").Activate
ListBox1.ColumnWidths = "120;120;120;120;120;120;120;120;120;120;120;120;120"         'Column Widths Of Listbox
ListBox1.ColumnCount = 6 'Column Count Of Listbox
a = Sheets("ss").Range("A1:F" & Sheets("ss").Cells(Rows.Count, 1).End(xlUp).Row).Value
For i = LBound(a, 1) + 1 To UBound(a, 1)

Next i
ListBox1.List = a


'Start Userform Centered inside Excel Screen (for dual monitors)
  Me.StartUpPosition = 0
  Me.Left = Application.Left + (0.5 * Application.Width) - (0.5 * Me.Width)
  Me.Top = Application.Top + (0.5 * Application.Height) - (0.5 * Me.Height)

End Sub


Private Sub CommandButton1_Click()
Dim sonsat As Long

If ListBox1.ListIndex = -1 Then
MsgBox "Choose an item", vbExclamation, ""
Exit Sub
End If
lastrow = Sheets("SS").Cells(Rows.Count, 1).End(xlUp).Row
Sheets("SS").Range("A1:A" & lastrow).Find(What:=ListBox1.Text, LookIn:=xlValues, LookAt:=xlWhole).Activate
sonsat = ActiveCell.Row
Cells(sonsat, 1) = TextBox1.Text
Cells(sonsat, 2) = TextBox2.Text
Cells(sonsat, 3) = TextBox3.Text
Cells(sonsat, 4) = TextBox4.Text
Cells(sonsat, 5) = TextBox5.Text
'Cells(sonsat, 6) = TextBox6.Text



MsgBox "Item has been updated", vbApplicationModal, ""
ListBox1.List = Sheets("SS").Range("A1:F" & Sheets("SS").Cells(Rows.Count, 1).End(xlUp).Row).Value
Unload Me

    UserForm1.Show
End Sub

Private Sub ListBox1_Change()
    On Error GoTo FTB '<---- When there are <11 columns
    
    TextBox1.Value = ""
    TextBox2.Value = ""
    
    TextBox3.Value = ""
    
    TextBox4.Value = ""
    TextBox5.Value = ""
    
    
    

FTB:
    On Error GoTo ES '<---- When there are <11 columns
    TextBox1 = ListBox1.Column(0)
    TextBox2.Value = ListBox1.Column(1)
    TextBox3.Value = ListBox1.Column(2)
    TextBox4.Value = ListBox1.Column(3)
    TextBox5.Value = ListBox1.Column(4)
   
   

ES:
End Sub
 
Upvote 0
for correction the last sheet when re-calculation in column BALANCE for selected item from listbox should also re-calculate next rows for the same name after selected item as I marked red in the picture .so selected item become 1233 + 1233 -100 = 2366 next
2366+0-2000=366 , 366+0-466=-100 .
4.JPG
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,748
Members
448,989
Latest member
mariah3

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