VBA Display list in msgbox

RvdV16681

Board Regular
Joined
Mar 31, 2016
Messages
62
Hello,

I'm using a Userform to store incoming andoutgoing equipment. Users of the equipment need to register by filling in theirname. What I would like is that users can see which equipment items they haveused, up till 10 items.
The data is stored on Sheet"Blad1", column A has the equipment number, column B the names ofusers, column C equipment description and column E the date they returned theitem.
On Userform5 is a Textbox, to fill in the name,and a Commandbutton. What I would like is a code that is searching for the namethat is filled is, and a msgbox to pop up showing a list of the last 10 itemsthat are used by this person, so the return so there has to be a value in columnE confirming the item is returned.
I'm now using this code, but that only give thelast item used, and I would like to extend it to the last 10 items.
<!--[if !supportLineBreakNewLine]-->
Code:
Private Sub CommandButton1_Click()
Dim I As Long
 Dim Lastrow As Long
 Lastrow = Sheets("Blad1").Cells(Rows.Count, "A").End(xlUp).row
    For I = Lastrow To 1 Step -1
        If Sheets("Blad1").Cells(I, 2).value = TextBox1.value And Sheets("Blad1").Cells(I, 4) > "" Then
              MsgBox "Last item used by " & (Sheets("Blad1").Cells(I, 2).value) & vbNewLine & "is: " & (Sheets("Blad1").Cells(I, 1).value) & ", " & (Sheets("Blad1").Cells(I, 3).value)
            Exit Sub
         End If
        
      Next
 End Sub
Hopefully there is a possibility to change the code and get it to work for me.

Thanks for helping out in advance!!
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi RvdV16681,

Untested by try this:

Code:
Option Explicit
Private Sub CommandButton1_Click()

    Dim I As Long
    Dim Lastrow As Long
    Dim intCounter As Integer
    Dim strMyList As String
    
    Lastrow = Sheets("Blad1").Cells(Rows.Count, "A").End(xlUp).Row
    
    For I = Lastrow To 1 Step -1
        If Sheets("Blad1").Cells(I, 2).Value = TextBox1.Value And Sheets("Blad1").Cells(I, 4) > "" Then
            intCounter = intCounter + 1
            If intCounter <= 10 Then
                If strMyList = "" Then
                    strMyList = Sheets("Blad1").Cells(I, 1).Value & ", " & Sheets("Blad1").Cells(I, 3).Value
                Else
                    strMyList = strMyList & vbNewLine & Sheets("Blad1").Cells(I, 1).Value & ", " & Sheets("Blad1").Cells(I, 3).Value
                End If
            Else
                Exit For
            End If
        End If
    Next I
    
    MsgBox "Last " & intCounter & " item(s) used by " & TextBox1.Value & vbNewLine & "is: " & strMyList

End Sub

Regards,

Robert
 
Last edited:
Upvote 0
Hello Trebor76,

I just tried the code and it works as I want it to.
Thank you for your quick reply and to help me solving it!!
 
Upvote 0

Forum statistics

Threads
1,215,035
Messages
6,122,791
Members
449,095
Latest member
m_smith_solihull

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