+ Reply to Thread
Results 1 to 8 of 8

Use VBA code to copy and paste a specific selection

Hybrid View

  1. #1
    Registered User
    Join Date
    07-30-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    32

    Use VBA code to copy and paste a specific selection

    Hi everyone,

    I am trying to find out how to write a vba code that will copy individual rows that have ID codes (in column D) that do not match the master list of ID codes on sheet "ID Codes" column A. Then I need to paste the copied rows into a new sheet called "print sheet" in the next availbe row. Any ideas as to how I would even start this???

    Thanks!

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Use VBA code to copy and paste a specific selection

    Sub LooptyLoop()
    
        Dim lRow1 As Long
        Dim lRow2 As Long
        Dim l As Long
        Dim ll As Long
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim ws3 As Worksheet
        
        Set ws1 = Sheets("Search Codes")
        Set ws2 = Sheets("ID Codes")
        Set ws3 = Sheets.Add(, Sheets(Sheets.Count))
        lRow1 = ws1.Range("D65536").End(xlUp).Row
        lRow2 = ws2.Range("A65536").End(xlUp).Row
        
        For l = 2 To lRow1
            For ll = 2 To lRow2
                If ws1.Range("D" & l).Value = ws2.Range("A" & ll).Value Then
                    ws1.Range("D" & l).EntireRow.Copy Destination:=ws3.Range("A65536").End(xlUp).Offset(1, 0)
                End If
            Next ll
        Next l
    
    End Sub
    Untested.

    e/ I forgot to mention. You'll have to change Search Codes here:
    Set ws1 = Sheets("Search Codes")
    To the name of the worksheet you're searching from.
    Last edited by Solus Rankin; 08-08-2013 at 02:36 PM.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Registered User
    Join Date
    07-30-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Use VBA code to copy and paste a specific selection

    IT works well the only problem is pastes the rows into a new sheet instead of my "Print Sheet", also it only copied the same row from the bottom of my list instead of just the row from the top. Any ideas??

  4. #4
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Use VBA code to copy and paste a specific selection

    If Print Sheet already exists (I thought you wanted a new sheet) then change this:
    Set ws3 = Sheets.Add(, Sheets(Sheets.Count))
    to this:
    Set ws3 = Sheets.("Print Sheet")
    I'm not sure which list you're speaking of or what is in the bottom/top row. Is there an example workbook you could attach with a before and after? This would aid in coming up with a complete solution. To attach a workbook, press the 'Go Advanced' button below, and then the paperclip icon in the tool bar.

  5. #5
    Registered User
    Join Date
    07-30-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Use VBA code to copy and paste a specific selection

    I have attatched the workbook

    Concat.xlsm

  6. #6
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Use VBA code to copy and paste a specific selection

    I noticed the ID Codes on the Notes tab are on there multiple times, therefore they will get copied multiple times.

    Try:
    Sub LooptyLoop()
    
        Dim lRow1 As Long
        Dim lRow2 As Long
        Dim l As Long
        Dim ll As Long
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim ws3 As Worksheet
        
        Set ws1 = Sheets("Notes")
        Set ws2 = Sheets("ID Codes")
        Set ws3 = Sheets("Print Sheet")
        lRow1 = ws1.Range("D65536").End(xlUp).Row
        lRow2 = ws2.Range("A65536").End(xlUp).Row
        
        For l = 2 To lRow1
            For ll = 2 To lRow2
                If ws1.Range("D" & l).Value = ws2.Range("A" & ll).Value Then
                    ws2.Range("A" & ll).EntireRow.Copy Destination:=ws3.Range("A65536").End(xlUp).Offset(1, 0)
                End If
            Next ll
        Next l
    
    End Sub

  7. #7
    Registered User
    Join Date
    07-30-2013
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    32

    Re: Use VBA code to copy and paste a specific selection

    I updated what Im trying to accomplish

    Concat.xlsm

  8. #8
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Use VBA code to copy and paste a specific selection

    Private Sub CommandButton1_Click()
    
    'IF the ID Code in Cloumn D in Notes is blank ("") or does not match the list of ID codes in sheet"ID CODES"
    'then copy and past the entire row of the ID code into the "Print sheet" in the next available row
    
        Dim lRow1 As Long
        Dim lRow2 As Long
        Dim l As Long
        Dim ll As Long
        Dim strTest As String
        Dim ws1 As Worksheet
        Dim ws2 As Worksheet
        Dim ws3 As Worksheet
        
        Set ws1 = Sheets("Notes")
        Set ws2 = Sheets("ID Codes")
        Set ws3 = Sheets("Print Sheet")
        lRow1 = ws1.Range("D65536").End(xlUp).Row
        lRow2 = ws2.Range("A65536").End(xlUp).Row
        
        For l = 2 To lRow1
            strTest = ws1.Range("D" & l).Value
            For ll = 2 To lRow2
                If ws2.Range("A" & ll).Value = strTest Then
                    ws1.Range("A" & l).EntireRow.Copy Destination:=ws3.Range("D65536").End(xlUp).Offset(1, 0)
                End If
            Next ll
        Next l
    
    End Sub

+ 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. Replies: 0
    Last Post: 07-16-2013, 05:22 AM
  2. VBA Code to copy and paste a row depending on selection made in a combobox
    By ssu in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 05-20-2013, 05:37 AM
  3. Macro to copy specific line from text file and paste into specific cell in excel
    By keeneye in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-05-2013, 10:35 AM
  4. Selection Change code greys out paste option
    By bradlehoux in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-17-2011, 01:27 PM
  5. Error on code to copy and paste user selection.
    By leaning in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-03-2011, 10:52 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