+ Reply to Thread
Results 1 to 8 of 8

copy selected listbox items and paste to a specific sheet

  1. #1
    Registered User
    Join Date
    11-08-2016
    Location
    SCOTLAND
    MS-Off Ver
    2007
    Posts
    8

    copy selected listbox items and paste to a specific sheet

    hi All,

    I managed to find a bit code for a userform im working on, i have 2 listboxes, Multiextended, i can transfer from listbox1 to listbox 2 ok, checkbox to select all, move items back n forward, but the code I have to copy listbox2 to a sheet called Cert_Input finding the first blank cell in E seems to take forever, any help?

    Private Sub CommandButton1_Click()
    Dim Litem As Long, LbRows As Long, LbCols As Long
    Dim bu As Boolean
    Dim Lbloop As Long, Lbcopy As Long

    LbRows = ListBox2.ListCount - 1

    For Litem = 0 To LbRows
    If ListBox2.Selected(Litem) = True Then
    bu = True
    Exit For
    End If
    Next

    If bu = True Then
    With Sheets("Cert_Input").Cells(Rows.Count, 5).End(xlUp).Offset(1, 0)

    For Litem = 0 To LbRows
    If ListBox2.Selected(Litem) = True Then 'Row selected
    'Increment variable for row transfer range
    Lbcopy = Lbcopy + 1
    For Lbloop = 0 To LbCols
    'Transfer selected row to relevant row of transfer range
    .Cells(Lbcopy, Lbloop + 1) = ListBox2.List(Litem, Lbloop)

    Next Lbloop
    End If
    Next

    End With

    Else
    MsgBox "Nothing chosen", vbCritical
    End If
    MsgBox "The Selected Data Are Copied.", vbInformation

    End With
    End Sub

  2. #2
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: copy selected listbox items and paste to a specific sheet

    See if this helps.

    Please Login or Register  to view this content.
    David
    (*) Reputation points appreciated.

  3. #3
    Registered User
    Join Date
    11-08-2016
    Location
    SCOTLAND
    MS-Off Ver
    2007
    Posts
    8
    Quote Originally Posted by Tinbendr View Post
    See if this helps.

    Please Login or Register  to view this content.
    Thanks, ill give it a try first thing in the morning, let you know the outcome. Brains frazzled.

  4. #4
    Registered User
    Join Date
    11-08-2016
    Location
    SCOTLAND
    MS-Off Ver
    2007
    Posts
    8

    Re: copy selected listbox items and paste to a specific sheet

    Hi
    It comes up, a Run time error 1004, highlights this row, not gonna get a more indepth look at it till this afternoon,

    .Cells(Lbcopy, Lbloop + 1) = ListBox2.List(Litem, Lbloop)

    Thanks

  5. #5
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: copy selected listbox items and paste to a specific sheet

    I left off this.

    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    11-08-2016
    Location
    SCOTLAND
    MS-Off Ver
    2007
    Posts
    8

    Re: copy selected listbox items and paste to a specific sheet

    Hi Tinbendr, Thanks, but kinda still takes its time, maybe My computer slow, but seems to run through each item, sometimes the list might have 5 items, then computer goes through the processing stage 5 times, but the list could have 100 items, goes through it for each part also.

  7. #7
    Registered User
    Join Date
    11-08-2016
    Location
    SCOTLAND
    MS-Off Ver
    2007
    Posts
    8

    Re: copy selected listbox items and paste to a specific sheet

    Finally solved it, much faster, thanks for your help, code as follows:
    Private Sub CommandButton1_Click()
    Dim i As Long
    Dim ary

    ReDim ary(0 To 0)
    With Me.ListBox2
    For i = 0 To .ListCount - 1
    If .Selected(i) Then
    ReDim Preserve ary(1 To UBound(ary) + 1)
    ary(UBound(ary)) = .List(i)
    End If
    Next
    End With

    Sheets("Cert_Input").Cells(Rows.Count, 5).End(xlUp).Offset(1, 0).Resize(UBound(ary)).Value _
    = Application.Transpose(ary)
    End Sub



    Private Sub UserForm1_Initialize()
    Dim i As Long

    For i = 65 To 85
    Me.ListBox2.AddItem Chr(i)
    Next
    End Sub

  8. #8
    Forum Expert Tinbendr's Avatar
    Join Date
    06-26-2012
    Location
    USA
    MS-Off Ver
    Office 2010
    Posts
    2,125

    Re: copy selected listbox items and paste to a specific sheet

    Glad you got it sorted.

+ 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. How to Copy and paste selected row from a listbox to a sheet
    By Hilfesucher in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-27-2015, 10:18 AM
  2. Selected Listbox Items to Sheet
    By CobraLAD in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 04-10-2014, 04:54 AM
  3. Copy Selected items from multicolumn, multiselect listbox to another listbox
    By Willigb in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 04-19-2013, 11:27 AM
  4. [SOLVED] Trying to copy selected items from sheet to listbox then selected back to sheet
    By BigWes1960 in forum Excel - New Users/Basics
    Replies: 4
    Last Post: 03-14-2013, 03:27 PM
  5. Transfer random listbox items to new listbox and then loop through selected items
    By narrowgate88 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-31-2013, 05:58 PM
  6. [[2003] From specific sheet range into listbox items?
    By jobro in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-24-2008, 05:20 AM
  7. How to copy selected listbox item to sheet
    By michdan in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-14-2005, 08:05 AM

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