Results 1 to 4 of 4

How do I loop through each CELL in a named range and compare to another range for matches?

Threaded View

  1. #1
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    How do I loop through each CELL in a named range and compare to another range for matches?

    Hi,

    I have ListBox on a UserForm that pre-loads items from a list ("TheList") and then pre-loads the already selected items on that list from another list ("TheListSelected"). It works perfectly except for one little thing. I would like to relocate the TheList to another sheet but the code uses CELL and OFFSET and I don't know how to get them to read from a range on another sheet.

    I've attached the file, but the code I ned to change is pasted below in bold:

    Private Sub UserForm_Initialize()
    
      Dim cell As Range
      
        'Define the list as a named range
        With Sheets("HappySheet")
            Application.Names.Add "TheList", .Range("A5", .Range("A" & Rows.Count).End(xlUp))
        End With
      
        CheckBox1.Value = True  'Default setting
      
        With ListBox1
        
           For Each cell In Range("TheList")                 ' Loop through each cell in the list
                .AddItem cell.Value                          ' Add items to the ListBox
                If cell.Offset(, 2).Value = cell.Value Then  ' test if the item is in the Selected List
                    .Selected(.ListCount - 1) = True         ' Select the item in the Listbox
                Else
                    CheckBox1.Value = False                  ' Unheck the Select All checkbox one item is not selected
                End If
           Next cell
            
        End With
    
    End Sub
    Is this just a small tweak of code?

    Lawrence

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1