+ Reply to Thread
Results 1 to 10 of 10

VBA copy and paste based on cell value

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-20-2014
    Location
    Ireland
    MS-Off Ver
    Excel 2013
    Posts
    108

    VBA copy and paste based on cell value

    Hi All,

    I have a list of employee names, beside each name is the shift they are working. i.e DS or NS (day shift or night shift)

    What I want to do is have a code look in the list and pull every row that has DS in it, and paste it into another sheet, but not to leave any spaces between rows as they are in the employee list.

    I hope this can be easily done. I'm ok with vba, getting better every day!

    Thanks in advance for any replies

  2. #2
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA copy and paste based on cell value

    I assummed employee names were in column A and the shift was in column B:

    Sub RunMe()
    Dim ws1 As Worksheet:   Set ws1 = Sheets("Sheet1") 'primary sheet
    Dim ws2 As Worksheet:   Set ws2 = Sheets("Sheet2") 'output sheet
    Dim str As String
    
    str = "DS" 'you can change this if needed
    
    With ws1
        .AutoFilterMode = False
        .Range("B1:B" & .Range("B" & Rows.Count).End(xlUp).Row).AutoFilter Field:=1, Criteria1:=str
        .AutoFilter.Range.Offset(1, 0).SpecialCells(xlCellTypeVisible).EntireRow.Copy Destination:=ws2.Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
        .AutoFilterMode = False
    End With
        
    End Sub

  3. #3
    Forum Contributor
    Join Date
    02-20-2014
    Location
    Ireland
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: VBA copy and paste based on cell value

    Hi, Thanks for the quick reply

    I have been working on a code

    Sub Shift()
    
    Dim lr As Long, lr2 As Long, r As Long
    lr = Sheets("Histogram").Cells(Rows.Count, "A").End(xlUp).Row
    lr2 = Sheets("DS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
    lr3 = Sheets("NS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
    For r = lr To 2 Step -1
        If Range("E" & r).Value = "DS" Then
            Range("B:G").Copy Destination:=Sheets("DS Sheet").Range("A" & lr2 + 1)
            lr2 = Sheets("DS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
        End If
        If Range("E" & r).Value = "NS" Then
            Range("B:G").Copy Destination:=Sheets("NS Sheet").Range("A" & lr3 + 1)
            lr3 = Sheets("NS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
        End If
        Range("A1").Select
    Next r
    End Sub
    However this keeps returning an error saying "we can't paste because the copy and paste area is not the same size", I have made sure they are exact replicas in size.

    I realised I dont want to copy the whole row, jus column "B:G"

    Any Ideas?

  4. #4
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA copy and paste based on cell value

    Change

     Range("B:G").Copy Destination:=Sheets("DS Sheet").Range("A" & lr2 + 1)
    INTO

     Range("B" & r).Resize(, 6).Copy Sheets("DS Sheet").Range("A" & lr2 + 1)

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA copy and paste based on cell value

    I have made sure they are exact replicas in size.
    But the code doesn't know that. The code thinks you are taking the entire column B:G and trying to paste it to a non-entire column. It would work if you told it to paste to A1 but not A50 (for example).

    Probably would want to change to something more like this:

    If Range("E" & r).Value = "DS" Then
        Range("B" & lr, "G" & lr).Copy Destination:=Sheets("DS Sheet").Range("A" & Rows.Count).End(xlUp).Offset(1, 0)
    End If

  6. #6
    Forum Contributor
    Join Date
    02-20-2014
    Location
    Ireland
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: VBA copy and paste based on cell value

    Thanks for the reply.

    That works ok, but my code now seems to copy all cells, regardless of the "NS" or "DS" and is pasting them all into "DS Sheet", and relabelling them all as "DS"

    Here is my code as it is now

    Sub Shift()
    
    Dim lr As Long, lr2 As Long, r As Long
    lr = Sheets("Histogram").Cells(Rows.Count, "A").End(xlUp).Row
    lr2 = Sheets("DS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
    lr3 = Sheets("NS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
    For r = lr To 2 Step -1
        If Range("E" & r).Value = "DS" Then
            Range("B" & r).Resize(, 6).Copy Sheets("DS Sheet").Range("A" & lr2 + 1)
            lr2 = Sheets("DS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
        End If
        If Range("E" & r).Value = "NS" Then
            Range("B" & r).Resize(, 6).Copy Sheets("DS Sheet").Range("A" & lr3 + 1)
            lr3 = Sheets("NS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
        End If
        Range("A1").Select
    Next r
    End Sub

  7. #7
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA copy and paste based on cell value

    Sub Shift()
    
    Dim lr As Long, lr2 As Long, r As Long
    lr = Sheets("Histogram").Cells(Rows.Count, "A").End(xlUp).Row
    lr2 = Sheets("DS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
    lr3 = Sheets("NS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
    For r = lr To 2 Step -1
        If Range("E" & r).Value = "DS" Then
           lr2 = Sheets("DS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
            Range("B" & r).Resize(, 6).Copy Sheets("DS Sheet").Range("A" & lr2 + 1)
           
        ElseIf Range("E" & r).Value = "NS" Then
            lr3 = Sheets("NS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
            Range("B" & r).Resize(, 6).Copy Sheets("DS Sheet").Range("A" & lr3 + 1)
    
        End If
    
    Next r
    End Sub
    
     Sub Shiftmine()
    
    Dim lr As Long, r As Long
    lr = Sheets("Histogram").Cells(Rows.Count, "A").End(xlUp).Row
    For r = lr To 2 Step -1
        If Range("E" & r).Value = "DS" Then
            Range("B" & r).Resize(, 6).Copy Sheets("DS Sheet").Sheets("DS Sheet").Cells(Rows.Count, "A").End(xlUp).Offset(1)
        ElseIf Range("E" & r).Value = "NS" Then
            Range("B" & r).Resize(, 6).Copy Sheets("DS Sheet").Sheets("NS Sheet").Cells(Rows.Count, "A").End(xlUp).Offset(1)
        End If
    Next r
    End Sub
    Last edited by AB33; 09-09-2014 at 12:54 PM.

  8. #8
    Forum Contributor
    Join Date
    02-20-2014
    Location
    Ireland
    MS-Off Ver
    Excel 2013
    Posts
    108

    Re: VBA copy and paste based on cell value

    Apologies, my last reply was incorrect, Its just not pasting the "NS" Values to the "NS Sheet"

    Thanks for your code, but this too returns an error "Object not recognized".

    I have attached an example workbook so you can see whats happening.

    Thanks again for your time
    Attached Files Attached Files

  9. #9
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA copy and paste based on cell value

    You are you looping back ward? The first cell to be copied would be the last row in sheet Histogram.
    Why are the other sheets layout look weird?

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: VBA copy and paste based on cell value

    You should remove these lines from the body of the code. If you do not, the code will keep copying on the same row again and again.

    lr2 = Sheets("DS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
    lr3 = Sheets("NS Sheet").Cells(Rows.Count, "A").End(xlUp).Row
    Option Explicit
    Sub Shift()
    
    Dim lr As Long, r As Long
     With Sheets("Histogram")
        lr = .Cells(.Rows.Count, "A").End(xlUp).Row
        For r = lr To 2 Step -1
            If .Range("E" & r).Value = "DS" Then
                .Range("B" & r).Resize(, 6).Copy Sheets("DS Sheet").Cells(Rows.Count, "A").End(xlUp).Offset(1)
            ElseIf .Range("E" & r).Value = "NS" Then
                .Range("B" & r).Resize(, 6).Copy Sheets("NS Sheet").Cells(Rows.Count, "A").End(xlUp).Offset(1)
            End If
        Next r
     End With
    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. Copy and Paste based on a cell value
    By lzee61971 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-17-2014, 09:59 PM
  2. Copy-Paste based on cell value
    By Konexcelmath in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-30-2013, 03:32 AM
  3. [SOLVED] Copy/Paste Based on Value of Cell
    By l8tnite in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-25-2012, 06:17 PM
  4. Copy Paste based on Cell Value
    By Bwbisel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 10-14-2011, 09:06 AM
  5. Copy and paste row based on cell value
    By contra76 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 10-20-2010, 03:23 AM

Tags for this Thread

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