+ Reply to Thread
Results 1 to 7 of 7

copying multiple listbox values to new sheet

Hybrid View

dan2010 copying multiple listbox... 08-16-2010, 12:20 AM
Andy Pope Re: copying multiple listbox... 08-16-2010, 04:24 AM
dan2010 Re: copying multiple listbox... 08-16-2010, 04:35 AM
Andy Pope Re: copying multiple listbox... 08-16-2010, 04:46 AM
dan2010 Re: copying multiple listbox... 08-16-2010, 05:12 AM
  1. #1
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    copying multiple listbox values to new sheet

    Hi,

    i am currently having troubles writing code to copy multiple selected listbox values to a new sheet.what i am hoping to do is to let user select some items in listbox and show the selected name,risk value and cost on sheet 2.the risk value is calculated with a formula(age + length), cost (length+diameter). The code below is what i managed to find at the moment, however it will show all the values of name,risk value,cost,age,length,diameter. if i delete the values of age, length and diameter, the risk value and cost will turn to "0". So i am wondering if there is any code that can copy the info in listbox straight to sheet 2 instead of referring to sheet1.
    any help will be greatly appreciated

    Private Sub Commandbutton1_click()
    dim a As Long
    dim i As Long
    a =1
    For i = 0 To ListBox1.ListCount - 1
    If Listbox1.selected(i) = True Then
    worksheets("sheet1").Cells(i+1,1).EntireRow.Copy
    Worksheets("sheet2").Cells(a,1).pastespecial
    a = a+1
    End If
    Next i
    End sub


    sheet 1
    name risk value cost age length diameter
    a
    b
    c
    d
    e

    listbox
    name risk value cost
    a
    b
    c
    d
    e
    Last edited by dan2010; 08-16-2010 at 05:25 AM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: copying multiple listbox values to new sheet

    Does it work if you provide a parameter to the PasteSpecial command

    Worksheets("sheet2").Cells(a,1).pastespecial xlPasteValues
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: copying multiple listbox values to new sheet

    it shows run-time error'1004'
    pastespecial method of range class failed

    i have attached an example to illustrate my point.

    i am thinking whether is it possible to use the values from listbox to paste on sheet 2 if possible.
    Attached Files Attached Files

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: copying multiple listbox values to new sheet

    This will copy the entire row from sheet1 to sheet2

    Private Sub CommandButton1_Click()
    
    Dim i As Long
    Dim a As Long
    
    a = 2
    For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) = True Then
    
        Sheet1.Range(ListBox1.RowSource).Cells(1, 1).Offset(i).EntireRow.Copy
        Sheet2.Cells(a, 1).PasteSpecial xlPasteValues
        a = a + 1
    End If
    Next i
    
    End Sub
    This will copy the first 3 columns only
    Private Sub CommandButton1_Click()
    
    Dim i As Long
    Dim a As Long
    
    a = 2
    For i = 0 To ListBox1.ListCount - 1
    If ListBox1.Selected(i) = True Then
    
        Sheet1.Range(ListBox1.RowSource).Cells(1, 1).Offset(i).Resize(1, 3).Copy
        Sheet2.Cells(a, 1).PasteSpecial xlPasteValues
        a = a + 1
    End If
    Next i
    
    End Sub
    If you want the records to append rather than start in row to then include
    a = Sheet2.Cells(Sheet2.Rows.Count, 1).End(xlUp).Row
    If a = 1 Then
        ' add header
        
        a = 2
    Else
        a = a + 1
    End If

  5. #5
    Forum Contributor
    Join Date
    08-04-2010
    Location
    singapore
    MS-Off Ver
    Excel 2007
    Posts
    143

    Re: copying multiple listbox values to new sheet

    i am using excel 2007. i tried ur coding but error '1004' pop up. pastepspecial method of range class failed.i dont think "x1pastevalues" is available in my version :S

  6. #6
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,481

    Re: copying multiple listbox values to new sheet

    I doubt anybody has x1PasteValues. That should be an L not a 1 (one)

    xlPasteValues

+ Reply to Thread

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