how populate the last entered data in listbox when run userform

abdelfattah

Well-known Member
Joined
May 3, 2019
Messages
1,430
Office Version
  1. 2019
  2. 2010
Platform
  1. Windows
hi
I would when run the userform I would show the last entered data .
in pic 1 when enter data from the first time

1.PNG




in pic2 enter new data
\
2.PNG



pic 3 it populate the last entered data with ignore the old data
3.PNG





this is the current code to show all of data
VBA Code:
Private Sub UserForm_Initialize()
   Dim LastRow As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row    '<---- ??????
    With ListBox1
        .ColumnCount = 6    '<---- Change to 18??????
        .ColumnWidths = "70;70;100;70;70;70"    '<---- 18 x each column width
        .List = Range("A1:F" & LastRow).Value    '<---- Change the "I" to the column number used above ( S = 18)
    End With
End Sub
thanks
and so on if I enter new data it will populate new data in listbox with ignores the old data
note: if you want depending on date to do that I don't mind . it's up to you how should do it.
 

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

HI Abdel Fattah​

In your code change
VBA Code:
.List = Range("A1:F" & LastRow).Value
to
VBA Code:
.RowSource = "A1:F" & LastRow
it should be:
VBA Code:
Private Sub UserForm_Initialize()
   Dim LastRow As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    With ListBox1
        .ColumnCount = 6
        .ColumnWidths = "70;70;100;70;70;70"
        .RowSource = "A1:F" & LastRow
    End With
End Sub
 
Upvote 0
thanks but it doesn't make difference. it shows all of data .:unsure:
 
Upvote 0
Sorry, I missed ignore the old data
I think you should
have a copy of entered a new data & get on user form from there
or
Somewhere, for example in Range("K1") enter the count of newly added rows (variable "LastAddedRows") then use this code

VBA Code:
Private Sub UserForm_Initialize()
    Dim LastRow As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Dim LastAddedRows As Long
    LastAddedRows = Range("K1").Value
    With ListBox1
        .ColumnCount = 6
        .ColumnWidths = "70;70;100;70;70;70"
        .RowSource = ("A" & LastRow - LastAddedRows + 1) & ":F" & LastRow
    End With
End Sub
 
Upvote 0
thanks , but there is a problem if I have data are repetead contain the same date . it will take the last date for lastrow and ignore the rest of data contain the same date .

I would show all of data for the last date . even they are repeated . it shouldn't ignore the rest of repeated data contain the same date .
 
Upvote 0
Sorry, I was confused about what to do with dates, please show an example of what you need to see in the Listbox from the data
 
Upvote 0
see the last rows 7,8,9 theses the last entered with repeat the same date
CL.xlsx
ABCDEF
1DATECODEBRANDTYPEORIGINQUANTITY
21/1/2021AA20-W50 208LQ8EU100
31/2/2021AA120-W50 12x1LQ8EU101
41/3/2021AA220-W50 15x1LQ8EU102
51/4/2021BB120-W50 208LCASEU103
61/5/2021BB220-W50 12x1LCASEU104
71/6/2021BB320-W50 15x1LCASEU105
81/6/2021BB420-W50 15x1LENIEU106
91/6/2021BB520-W50 15x1LENIEU107
SHEET1


the result should show like this
1.PNG
 
Upvote 0
Just making clear for me, can we say show the latest date data only, no matter how many records we have, if one record with the latest date, then show one line, if 5 records have the same latest date, then show 5 lines?
 
Upvote 0
Just making clear for me, can we say show the latest date data only, no matter how many records we have, if one record with the latest date, then show one line, if 5 records have the same latest date, then show 5 lines?
exactly this is what I want based on date .
 
Upvote 0
Here you go
VBA Code:
Private Sub UserForm_Initialize()
    Dim LastRow As Long
    LastRow = Range("A" & Rows.Count).End(xlUp).Row
    Dim MaxDt, CntLatestDate As Long
    MaxDt = WorksheetFunction.Max(Range("A:A"))
    CntLatestDt = Application.WorksheetFunction.CountIf(Range("A:A"), MaxDt)
    With ListBox1
        .ColumnCount = 6
        .ColumnWidths = "70;70;100;70;70;70"
        .RowSource = ("A" & LastRow - CntLatestDt + 1) & ":F" & LastRow
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,550
Members
449,237
Latest member
Chase S

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