If no values are found then show Msgbox

ipbr21054

Well-known Member
Joined
Nov 16, 2010
Messages
5,832
Office Version
  1. 2007
Platform
  1. Windows
I have this code in use.

If there are no values to populate the Listbox i wish to see a Msgbox but im not sure what code to use or where to place it

VBA Code:
    Dim fndRng As Range
    Dim firstAddress As String
    
With Me.ListBox1
    .ColumnCount = 7
    .ColumnWidths = "220;200;110;170;130;60;100"
End With

With Sheets("POSTAGE").Range("G:G")
    Set fndRng = .Find(What:="RECEIVED NO SIG", LookIn:=xlValues, LookAt:=xlWhole, _
        SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    If Not fndRng Is Nothing Then
        firstAddress = fndRng.Address
        Do
            ' check the date
            If Date - DateValue(fndRng.Offset(, -6)) < 80 Then
                ' add to listbox
                With Me.ListBox1
                    .AddItem fndRng.Offset(, -5).Value                      'CUSTOMER
                    .List(.ListCount - 1, 1) = fndRng.Offset(, -4).Value    'ITEM
                    .List(.ListCount - 1, 2) = fndRng.Offset(, -6).Value    'DATE
                    .List(.ListCount - 1, 3) = fndRng.Offset(, -2).Value    'TRACKING NUMBER
                    .List(.ListCount - 1, 4) = fndRng.Offset(, 5).Value     'CLAIM
                    .List(.ListCount - 1, 6) = fndRng.Value                 'RECEIVED NO SIG
                    
                End With
            End If
            Set fndRng = .FindNext(fndRng)
        Loop While Not fndRng Is Nothing And fndRng.Address <> firstAddress
    End If
End With
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
This will count as each item is added to the listbox and then an If statement at the end will determine if it will show the message box or not.

VBA Code:
Dim fndRng As Range
Dim firstAddress As String
Dim ItemCount as Integer

ItemCount = 0

With Me.ListBox1
    .ColumnCount = 7
    .ColumnWidths = "220;200;110;170;130;60;100"
End With

With Sheets("POSTAGE").Range("G:G")
    Set fndRng = .Find(What:="RECEIVED NO SIG", LookIn:=xlValues, LookAt:=xlWhole, _
        SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    If Not fndRng Is Nothing Then
        firstAddress = fndRng.Address
        Do
            ' check the date
            If Date - DateValue(fndRng.Offset(, -6)) < 80 Then
                ' add to listbox
                With Me.ListBox1
                    .AddItem fndRng.Offset(, -5).Value                      'CUSTOMER
                    .List(.ListCount - 1, 1) = fndRng.Offset(, -4).Value    'ITEM
                    .List(.ListCount - 1, 2) = fndRng.Offset(, -6).Value    'DATE
                    .List(.ListCount - 1, 3) = fndRng.Offset(, -2).Value    'TRACKING NUMBER
                    .List(.ListCount - 1, 4) = fndRng.Offset(, 5).Value     'CLAIM
                    .List(.ListCount - 1, 6) = fndRng.Value                 'RECEIVED NO SIG

                    ItemCount = ItemCount + 1 ' Will add 1 to each time an item is added to the listbox

                End With
            End If
            Set fndRng = .FindNext(fndRng)
        Loop While Not fndRng Is Nothing And fndRng.Address <> firstAddress
    End If
End With

If Itemcount = 0 Then
MsgBox "Nothing added to listbox"
End If
 
Upvote 0
Thanks but that didnt quite work for me.

Where there are no values i see the msgbox.
I close the Msgbox but then the userform wants to open.
I put a line of code to close it but then when i go to the click the button to run it again the form wont open.

I think best maybe i wait for user that helped me & maybe put the text to advise no values actually in the listbox
Thanks
 
Upvote 0
Add me.hide for the userform.

VBA Code:
If Itemcount = 0 Then
MsgBox "Nothing added to listbox"
Me.Hide
End If

If the count is more than 1 it will show otherewise it will not.
 
Upvote 0
I understand my my button to open the userform is on a worksheet.

This code runs after you try to open it from worksheet
 
Upvote 0
Perhaps this
VBA Code:
Private Sub UserForm_Initialize()

    Dim fndRng As Range
    Dim firstAddress As String
    Dim cnt As Long
    
With Me.ListBox1
    .ColumnCount = 7
    .ColumnWidths = "220;200;110;140;70;70;100"
End With

With Sheets("POSTAGE").Range("G:G")
    Set fndRng = .Find(What:="RECEIVED NO SIG", LookIn:=xlValues, LookAt:=xlWhole, _
        SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
    If Not fndRng Is Nothing Then
        firstAddress = fndRng.Address
        Do
            ' check the date
            If Date - DateValue(fndRng.Offset(, -6)) < 90 Then
                cnt = cnt + 1
                ' add to listbox
                With Me.ListBox1
                    .AddItem fndRng.Offset(, -5).Value                      'CUSTOMER
                    .List(.ListCount - 1, 1) = fndRng.Offset(, -4).Value    'ITEM
                    .List(.ListCount - 1, 2) = fndRng.Offset(, -6).Value    'DATE
                    .List(.ListCount - 1, 3) = fndRng.Offset(, -2).Value    'TRACKING NUMBER
                    .List(.ListCount - 1, 4) = fndRng.Offset(, 5).Value     'CLAIM
                    .List(.ListCount - 1, 6) = fndRng.Value                 'RECEIVED NO SIG
                End With
            End If
            Set fndRng = .FindNext(fndRng)
        Loop While Not fndRng Is Nothing And fndRng.Address <> firstAddress
    End If
End With

If cnt = 0 Then
    MsgBox "There are " & cnt & " records to display in the list box"
    End
End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,224,812
Messages
6,181,094
Members
453,021
Latest member
Justyna P

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