hi experts
I need mod this code or alternitive. so I attach some pictures for five cases. the code populates data based on column (4) in listbox. now I would make it more flexible to sort based on date for item is written in textbox1 .
first this is data in sheet "PURCHASE"
and this show when run the userform
case 1 I want sort data based on select last date after write item in textbox1 based on column 4 in listbox
case 2 I want sort data based on select old date after write item in textbox1 based on column 4 in listbox
case 3 I want sort data based on select reset after write item in textbox1 based on column 4 in listbox should show whether last or old date together
case 4 I want sort data based on select month and combobox for specific month after write item in textbox1 based on column 4 in listbox
note: I've found to be cancel optionbutton MONTH and just depends on combobox1 to select month
case 5 I want sort data based on write the date (from,to) in textbox2,3 after write item in textbox1 based on column 4 in listbox
and this is the whole code
any help would truly appreciate.
I need mod this code or alternitive. so I attach some pictures for five cases. the code populates data based on column (4) in listbox. now I would make it more flexible to sort based on date for item is written in textbox1 .
first this is data in sheet "PURCHASE"
FILTER.xlsm | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | |||
1 | DATE | CLIENT NO | INVOICE NO | ID-CC | BB-R | TT-Y | OR-GG | QTY | PRICE | TOTAL | ||
2 | 1/5/2021 | CC2-TRB-1 | INV-TRGG-1 | IDTR-100 | FFDOOD-1 | PACK-2 | TTR | 60.00 | $120.00 | $7,200.00 | ||
3 | 1/6/2021 | CC2-TRB-1 | INV-TRGG-1 | IDTR-101 | FFDOOD-2 | PACK-3 | BBR | 10.00 | $130.00 | $1,300.00 | ||
4 | 1/7/2021 | CC2-TRB-1 | INV-TRGG-1 | IDTR-102 | FFDOOD-3 | PACK-4 | LLR | 5.00 | $100.00 | $500.00 | ||
5 | 1/8/2021 | CC2-TRB-1 | INV-TRGG-1 | IDTR-103 | FFDOOD-4 | PACK-5 | MMR | 10.00 | $289.00 | $2,890.00 | ||
6 | 1/9/2021 | CC2-TRB-2 | INV-TRGG-2 | IDTR-104 | FFDOOD-5 | PACK-6 | ASDR | 5.00 | $140.00 | $700.00 | ||
7 | 1/10/2021 | CC2-TRB-2 | INV-TRGG-2 | IDTR-100 | FFDOOD-1 | PACK-2 | TTR | 30.00 | $100.00 | $3,000.00 | ||
8 | 1/11/2021 | CC2-TRB-2 | INV-TRGG-2 | IDTR-101 | FFDOOD-2 | PACK-3 | BBR | 50.00 | $120.00 | $6,000.00 | ||
9 | 1/12/2021 | CC2-TRB-2 | INV-TRGG-2 | IDTR-107 | FFDOOD-6 | PACK-7 | FFG | 12.00 | $200.00 | $2,400.00 | ||
10 | 1/13/2021 | CC2-TRB-2 | INV-TRGG-2 | IDTR-108 | FFDOOD-7 | PACK-8 | GGF | 13.00 | $100.00 | $1,300.00 | ||
PURCHASE |
Cell Formulas | ||
---|---|---|
Range | Formula | |
J2:J10 | J2 | =H2*I2 |
and this show when run the userform
case 1 I want sort data based on select last date after write item in textbox1 based on column 4 in listbox
case 2 I want sort data based on select old date after write item in textbox1 based on column 4 in listbox
case 3 I want sort data based on select reset after write item in textbox1 based on column 4 in listbox should show whether last or old date together
case 4 I want sort data based on select month and combobox for specific month after write item in textbox1 based on column 4 in listbox
note: I've found to be cancel optionbutton MONTH and just depends on combobox1 to select month
case 5 I want sort data based on write the date (from,to) in textbox2,3 after write item in textbox1 based on column 4 in listbox
and this is the whole code
VBA Code:
Option Explicit
Dim a As Variant
Private Sub TextBox1_Change()
Call FilterData
End Sub
Private Sub TextBox2_Change()
Call FilterData
End Sub
Private Sub TextBox3_Change()
Call FilterData
End Sub
Sub FilterData()
Dim txt1 As String, txt2 As String, txt3 As String
Dim I As Long, j As Long, k As Long
ListBox1.Clear
ReDim b(1 To UBound(a, 1), 1 To UBound(a, 2))
For I = 1 To UBound(a)
If TextBox1 = "" Then txt1 = a(I, 4) Else txt1 = TextBox1
If LCase(a(I, 4)) Like LCase(txt1) & "*" Then
k = k + 1
For j = 1 To 10
If j = 1 Then 'Change the 1 in this line for the column number where you have the date
b(k, j) = Format(a(I, j), "dd/mm/yyyy")
Else
b(k, j) = a(I, j)
End If
Next
End If
Next
If k > 0 Then ListBox1.List = b
End Sub
Private Sub UserForm_Initialize()
Dim lindex&
Dim rngDB As Range, rng As Range
Dim I, myFormat(1) As String
Dim sWidth As String
Dim vR() As Variant
Dim n As Integer
Dim myMax As Single
Set rngDB = Range("A2:J20")
For Each rng In rngDB
n = n + 1
ReDim Preserve vR(1 To n)
vR(n) = rng.EntireColumn.Width
Next rng
myMax = WorksheetFunction.Max(vR)
For I = 1 To n
vR(I) = myMax
Next I
With Sheets("purchase").Cells(1).CurrentRegion
myFormat(0) = .Cells(2, 8).NumberFormatLocal
myFormat(1) = .Cells(2, 9).NumberFormatLocal
Set rng = .Offset(1).Resize(.Rows.Count - 1)
a = .Cells(1).CurrentRegion.Value
End With
sWidth = Join(vR, ";")
Debug.Print sWidth
With ListBox1
.ColumnCount = 10
.ColumnWidths = sWidth '<~~ 63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63;63
.List = rng.Value
.BorderStyle = fmBorderStyleSingle
For lindex = 0 To .ListCount - 1
.List(lindex, 0) = (Format((.List(lindex, 0)), "dd/mm/yyyy")) ' BL = dates
.List(lindex, 7) = Format$(.List(lindex, 7), myFormat(0))
.List(lindex, 8) = Format$(.List(lindex, 8), myFormat(1))
.List(lindex, 9) = Format$(.List(lindex, 9), myFormat(1))
Next
End With
End Sub