check if value is in lisbox

User avatar
sal21
PlatinumLounger
Posts: 4354
Joined: 26 Apr 2010, 17:36

check if value is in lisbox

Post by sal21 »

I need to check if in list2 is just present PASSWORD, if not add item PASSWORD.

naturally in my example not add item, just PASSWORD is in list2
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: check if value is in lisbox

Post by HansV »

Use code like this:

Code: Select all

    Dim i As Long
    Dim f As Boolean
    For i = 0 To Me.List2.ListCount - 1
        If Me.List2.List(i) = Me.List1 Then
            f = True
            Exit For
        End If
    Next i
    If f = False Then
        Me.List2.AddItem Me.List1
    End If
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4354
Joined: 26 Apr 2010, 17:36

Re: check if value is in lisbox

Post by sal21 »

HansV wrote:
22 Jan 2021, 15:29
Use code like this:

Code: Select all

    Dim i As Long
    Dim f As Boolean
    For i = 0 To Me.List2.ListCount - 1
        If Me.List2.List(i) = Me.List1 Then
            f = True
            Exit For
        End If
    Next i
    If f = False Then
        Me.List2.AddItem Me.List1
    End If
OPS...
the code work only if list2 is filled, but if not is filled?

User avatar
HansV
Administrator
Posts: 78457
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: check if value is in lisbox

Post by HansV »

What happens? Do you get an error message?
Best wishes,
Hans