+ Reply to Thread
Results 1 to 5 of 5

UserForm : Want to link Listbox1 with Listbox2 and delete data with one button

Hybrid View

HaroonSid UserForm : Want to link... 06-01-2014, 08:55 AM
HaHoBe Re: UserForm : Want to link... 06-01-2014, 10:50 AM
HaroonSid Re: UserForm : Want to link... 06-01-2014, 11:02 AM
HaHoBe Re: UserForm : Want to link... 06-01-2014, 11:10 AM
HaroonSid Re: UserForm : Want to link... 06-01-2014, 11:18 AM
  1. #1
    Forum Contributor HaroonSid's Avatar
    Join Date
    02-28-2014
    Location
    india
    MS-Off Ver
    Excel 2013
    Posts
    2,095

    UserForm : Want to link Listbox1 with Listbox2 and delete data with one button

    hello
    i m using listbox1 and listbox2 to delete data form two different NameRange and different sheets using two command button

    i want, when i tick a box of first listbox1 than auto tick same value of listbox2 and delete the data with using one command Button not two,

    thanx
    Attached Files Attached Files

  2. #2
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: UserForm : Want to link Listbox1 with Listbox2 and delete data with one button

    Hi, haroon2015,

    I don´t see the necessity for 2 Listboxes with identical names as both lists could be handled in one ListBox. Due to the specifications you made the lists need to be identical as you refer to the position in the list and not to the name of the person (which would be more robust and allow different lists both on number of persons as of order of these). And to go into the future: no,I won´t deliver code for this sceanrio as you didn´t ask for it in the opening post.

    Code behind the UserForm:
    Option Explicit
    Dim blnQuit As Boolean
    
    Private Sub cmdDelete_Click()
      Dim lngIndex As Long
      Dim lngArray As Long
      Dim alDelete() As Long
      
      With Me.ListBox1
        For lngIndex = 1 To .ListCount
          If .Selected(lngIndex - 1) = True Then
            lngArray = lngArray + 1
            ReDim Preserve alDelete(lngArray)
            alDelete(lngArray) = lngIndex
          End If
        Next lngIndex
      End With
      
      If lngArray > 0 Then
        For lngIndex = UBound(alDelete) To 1 Step -1
          Range("DEL_UNQ_TO").Offset(alDelete(lngIndex), 0).Cells.Delete
          Range("DEL_UNQ9").Offset(alDelete(lngIndex), 0).Cells.Delete
        Next lngIndex
      End If
      
      Call UserForm_Activate
    End Sub
    
    Private Sub ListBox1_Change()
      Dim lngIndex As Long
      If blnQuit Then Exit Sub
      With Me.ListBox1
        blnQuit = True
        For lngIndex = 0 To .ListCount - 1
          If .Selected(lngIndex) Then
            Me.ListBox2.Selected(lngIndex) = True
          Else
            Me.ListBox2.Selected(lngIndex) = False
          End If
        Next lngIndex
        blnQuit = False
      End With
    End Sub
    
    Private Sub ListBox2_Change()
      Dim lngIndex As Long
      If blnQuit Then Exit Sub
      With Me.ListBox2
        blnQuit = True
        For lngIndex = 0 To .ListCount - 1
          If .Selected(lngIndex) Then
            Me.ListBox1.Selected(lngIndex) = True
          Else
            Me.ListBox1.Selected(lngIndex) = False
          End If
        Next lngIndex
        blnQuit = False
      End With
    End Sub
    
    Private Sub UserForm_Activate()
      Dim lngIndex As Long
      
      With Me.ListBox1
        .Clear
        lngIndex = 1
        Do While Not IsEmpty(Range("DEL_UNQ_TO").Offset(lngIndex, 0))
          .AddItem (Range("DEL_UNQ_TO").Offset(lngIndex, 0).Value)
          lngIndex = lngIndex + 1
        Loop
      End With
      
      With Me.ListBox2
        .Clear
        lngIndex = 1
        Do While Not IsEmpty(Range("DEL_UNQ9").Offset(lngIndex, 0))
          .AddItem (Range("DEL_UNQ9").Offset(lngIndex, 0).Value)
          lngIndex = lngIndex + 1
        Loop
      End With
    End Sub
    Ciao,
    Holger
    Use Code-Tags for showing your code: [code] Your Code here [/code]
    Please mark your question Solved if there has been offered a solution that works fine for you

  3. #3
    Forum Contributor HaroonSid's Avatar
    Join Date
    02-28-2014
    Location
    india
    MS-Off Ver
    Excel 2013
    Posts
    2,095

    Re: UserForm : Want to link Listbox1 with Listbox2 and delete data with one button

    Hello, HaHoBe

    i don't want to delete entire row, because there is data in other columns

    i used ClearContents in my previous code

    Use Code-Tags for showing your code :
    Please mark your question Solved if there has been offered a solution that works fine for you
    If You like solutions provided by anyone, feel free to add reputation using STAR *

  4. #4
    Forum Guru HaHoBe's Avatar
    Join Date
    02-19-2005
    Location
    Hamburg, Germany
    MS-Off Ver
    work: 365 on Win11 (365 on Win11 (desktop), 365 on Win11 (notebook)
    Posts
    8,207

    Re: UserForm : Want to link Listbox1 with Listbox2 and delete data with one button

    Gi, haroon2015,

    nobody is deleting entire rows unless you changed the code I supplied - when using Clearcontents maybe clear the contents of the second entry on the list and report back about what happens to the length of your list when you use that command...

    Ciao,
    Holger

  5. #5
    Forum Contributor HaroonSid's Avatar
    Join Date
    02-28-2014
    Location
    india
    MS-Off Ver
    Excel 2013
    Posts
    2,095

    Re: UserForm : Want to link Listbox1 with Listbox2 and delete data with one button

    Yes U r Right, HaHoBe,

    i was thinking wrong , code is working perfect, thanx alot HaHoBe

    will add u reputation

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. copy selection from listbox1 to listbox2 in userform
    By Decar in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-23-2014, 07:26 AM
  2. [SOLVED] Listbox1 items to move in Listbox2.
    By siobeh in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-14-2014, 03:18 PM
  3. [SOLVED] Userform submit button to load listbox1 and listbox2 selection to same cell
    By wishn4fishn in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-28-2013, 03:06 PM
  4. [SOLVED] Transfer data from Userform1.listbox1 to userform2.listbox2
    By Vbadept in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-04-2013, 08:28 AM
  5. [SOLVED] Listbox1 to Listbox2?
    By Moretakitty in forum Excel Programming / VBA / Macros
    Replies: 17
    Last Post: 01-20-2005, 07:06 PM

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