Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

search dates into textboxes on userform with hard code

0

Hi

well , this  code is not mine . you can see writing code of smart and difficult way .

in reality  I need to add another condition by two dates in textbox5,6 

if I write the name in textbox1 will populate data and calculation in listbox and when write two dates  in textbox5,6 then will populate data and calculation  within two dates based on name in textbox1 , if I just write dates in textbox5,6 without write name in textbox1 then will populate data and calculation in list box as the original code does it when clear textbox1 but within two dates , if just the write name in textbox1 without filling dates in textbox5,6 will populate the whole data and calculation in listbox for the name as the original code does it  .

I know  this  code is not easy  to understand line by line , but I hope finding help from the experts.

thanks.

Answer
Discuss

Discussion

Hi Maklil

Thsi code uses the old Evaluate function and I could not get it to work with date. I have thought of another solution but cannot post it today. Will do so tomorrow hopefully.
John_Ru (rep: 6142) Feb 25, '24 at 8:55 am
thanks John for  inform me .
MAKLIL (rep: 34) Feb 25, '24 at 2:08 pm
Add to Discussion

Answers

0
Selected Answer

Maklil

Your code used the old Evaluate function and I could not get it to work with dates (annoyingly!)

Instead I created this "universal" filter code (in the Userform1 code sheet of the attached modified file), commented for your understanding:.

Revision #1 26 February 2024- revised file attached. Includes revised code (below) to remove decimal places in Listbox results and remove negative signs from Credit values. Test removed from the TextBox1 code so that Listbox is revised if an input is deleted (also TextBox1 input is now case-insensitive).

Revision #2 27 February 2024- The code is now changed to add a variable RunTotl so that column 8 of Listbox1 now shows a "RUNNING TOTAL" of the filtered data i.e. how the debits and credits accumulate down the filtered data. (Previously it showed a "NETT MOVEMENT" for that row, which was not what the user wanted). Changes are in bold below:

Private Sub FilterLB()
    Dim i As Long, x As Long
    Dim AddFlag As Boolean, RunTotl As Double

    On Error Resume Next
    ' reset results
    With Me.ListBox1
        .Clear
        .ColumnCount = 8
        .ColumnWidths = "80;90;90;80;70;70;70;90"
        .AddItem
        .List(0, 0) = "DATE"
        .List(0, 1) = "INVOICE NO"
        .List(0, 2) = "NAME"
        .List(0, 3) = "CLIENT NO"
        .List(0, 4) = "PAID"
        .List(0, 5) = "DEBIT"
        .List(0, 6) = "CREDIT"
        .List(0, 7) = "RUNNING TOTAL"
    End With

    'set values to 0
    TextBox2.Value = 0
    TextBox3.Value = 0
    TextBox4.Value = 0
    RunTotl = 0

    With Sheets("data")
        For i = 2 To .Cells(1).CurrentRegion.Rows.Count
            ' assume this row is added
            AddFlag = True
            '... unless it passes any of these negative tests
            If TextBox1.Text <> "" And UCase(.Cells(i, 3).Value) <> UCase(TextBox1.Text) Then AddFlag = False
            If IsDate(TextBox5.Value) Then
                If .Cells(i, 1).Value < CDate(TextBox5.Value) Then AddFlag = False
            End If
            If IsDate(TextBox6.Value) Then
                If .Cells(i, 1).Value > CDate(TextBox6.Value) Then AddFlag = False
            End If
            If AddFlag = True Then
                Me.ListBox1.AddItem
                x = Me.ListBox1.ListCount - 1
                ' add values from row
                Me.ListBox1.List(x, 0) = Format(.Cells(i, 1).Value, "dd/mm/yyyy")
                Me.ListBox1.List(x, 1) = .Cells(i, 2).Value
                Me.ListBox1.List(x, 2) = .Cells(i, 3).Value
                Me.ListBox1.List(x, 3) = .Cells(i, 4).Value
                Me.ListBox1.List(x, 4) = .Cells(i, 5).Value
                Me.ListBox1.List(x, 5) = Format(.Cells(i, 6).Value, "#,#;-#,#;0")
                Me.ListBox1.List(x, 6) = Format(.Cells(i, 7).Value, "#,#;-#,#;0")
                ' calculate the running total, starting with 0
                RunTotl = RunTotl + .Cells(i, 6).Value - .Cells(i, 7).Value
                Me.ListBox1.List(x, 7) = Format(RunTotl, "#,#;-#,#;0")
                Me.TextBox2.Value = TextBox2.Value + .Cells(i, 6).Value
                Me.TextBox3.Value = TextBox3.Value + .Cells(i, 7).Value
            End If
        Next i
    End With
    'format textboxes
    Me.TextBox2.Value = Format(TextBox2.Value, "#,#.00;-#,#.00;0")
    Me.TextBox3.Value = Format(TextBox3.Value, "#,#.00;-#,#.00;0")
    Me.TextBox4.Value = Format(CDec(TextBox2.Value) - CDec(TextBox3.Value), "#,#.00;-#,#.00;0")

End Sub

This now puts a header row in the ListBox (DATE, INVOICE NO etc.) and is called by all modules as follows (some with tests on the input- which should be added using the Enter key)::

Option Explicit

Private Sub UserForm_Initialize()

    Call FilterLB

End Sub

Private Sub TextBox1_AfterUpdate()
    ' refilter list box (also when name is removed)
    Call FilterLB

End Sub

Private Sub TextBox5_AfterUpdate()

    If TextBox5.Value = "" Then Exit Sub
    If Not IsDate(TextBox5.Value) Then
       TextBox5.BackColor = vbRed
       Exit Sub
    End If

    TextBox5.BackColor = vbWhite
     ' refilter list box
    Call FilterLB

End Sub


Private Sub TextBox6_AfterUpdate()
    If TextBox6.Value = "" Then Exit Sub
    If Not IsDate(TextBox6.Value) Then
       TextBox6.BackColor = vbRed
       Exit Sub
    End If

    TextBox6.BackColor = vbWhite
     ' refilter list box
    Call FilterLB

End Sub

You can now add/remove names and/or dates and get a filtered output- which may be blank! If you add something that is not a date (in Textbox 5 or 6), it goes red.

Hope this makes sense and fixes your problem. If so, please mark this Answer as Selected.

Discuss

Discussion

thanks  for your trying , John.
but I 'm afraid  you changed calculation way .also  you cancelled  calculation way for just  the name without  write dates  should keep it  as the original code .
just test  the name in textbox1 without write dates you will understand how calculate for debit,credit where show in balance column .
my goal when add two dates for the name  then should calculate as the original code does it when write the  name  in textbox1
your calculation way for two dates is when just fill dates without fill name in textbox1 and it's ok and this is what I want, but when write just name or name and dates together , then the way of calculation will be different .
MAKLIL (rep: 34) Feb 26, '24 at 2:48 pm
Maklil. Not sure I understand your comment above but will try to look later this evening or tomorrow. 
John_Ru (rep: 6142) Feb 26, '24 at 3:06 pm
Maklil. Please see Revision #1 26 February 2024 to my Answer and the revised code/  file. I think I've corrected my errors and made it act just like your original code.
John_Ru (rep: 6142) Feb 26, '24 at 4:00 pm
doesn't seem to change anything , sorry !
but I edited the file  in FORM , WRONG sheets I captureed some  pictures .
the third picture  when just write dates  is ok as you did  it , but the first, second sheet will be different in calculation you can compare  first picture in FORM sheet  with the picture in WRONG sheet  what I got based on your code .
so when search just  the name will subtract debit from credit for just first row and the next rows will take the balance from first row and add to DEBIT and subtract from CREDIT.every next row will take the previous amount from BALANCE column and add to next  row for debit and subtract from credit . also the same thing when write name and two dates .
when search within two dates for the specific name then the first row should subtract DEBIT from CREDIT  and next row will depend on previous amount  in BALANCE column .
MAKLIL (rep: 34) Feb 26, '24 at 4:45 pm
Maklil- from looking at thoise images, you are using the wrong file (the revised file always has a header row in the list box). Please download the file again from my Answer and try again
John_Ru (rep: 6142) Feb 26, '24 at 4:56 pm
I'm pretty sure , your attaching doesn't change any thing , despite of that  I downloaded again and still doesn't do as I posted .
by the way the images based on my file ,not based on  your attaching, but the image in WRONG sheet will be the same thing as your attaching for calculation way.
MAKLIL (rep: 34) Feb 26, '24 at 5:16 pm
Maklil. Sorry- I now see that the last Listbox column (which I called "Nett Movement") seems to be some kind of rolling balance (though I don't know how useful that is, you don't seem to account for payment statuses and TextBoxes 2 to 4 give the final balance of filtered data).

It's too late now for me to do anything but please explain CAREFULLY what you want that column to show (in the case of no name, a name only and with dates). If I get time tomorrow I'll try to fix it.
John_Ru (rep: 6142) Feb 26, '24 at 5:55 pm
the last column will show movemont balances from row to next row when add to debit and subtract from credit . every row will depend on next row after take previous amount in last column .
 (in the case of no name, a name only and with dates)
I gave you example in image2 for FORM sheet 
again when write the name and two dates (12/06/2021 to 13/06/2021)  for ELYAAS name .
the result in last column in first row will be 0-2500=-2500
next row will take previous amount from last column -2500
the result in nextrow will be =-2500-2501=-5001 and so on for next row depends on previous amount from last column .
MAKLIL (rep: 34) Feb 27, '24 at 5:08 am
Maklil. Thanks for the clarification. Please see Revision #2 27 February 2024 to my Answer plus new file calculation with dates from textboxes and running balance v0_c.xlsm
John_Ru (rep: 6142) Feb 27, '24 at 6:57 am
really appreciated for your time & assistance .
thanks , John.
MAKLIL (rep: 34) Feb 27, '24 at 11:23 am
Glad we got there in the end! Thanks for selecting my Answer, Maklil.
John_Ru (rep: 6142) Feb 27, '24 at 11:52 am
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login