VBA Copy specific columns to new tab if cells contains specific text

thedeadzeds

Active Member
Joined
Aug 16, 2011
Messages
451
Office Version
  1. 365
Platform
  1. Windows
Hi Guys,

Is there a way to do the following:

Copy columns A,C, E,Y, and D from 'Offsited Data' tab to 'Send CSI' tab and paste as paste values only in that order. So when pasted into the Send CSI tab column A to column A, C to B, E to C, Y to D, and D to E

However, only copy the data where column Z is equal to "Yes" and where column Y is equal to "Dave", "Jim", "Steve", "Ron" and "James".

Hope this makes sense
 
Last edited:

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try something like this ..

Code:
Sub CopyData()
    Dim OD As Worksheet, CSI As Worksheet, Rng As Range, Cel As Range, rw As Long, r As Long
    
    Set OD = Sheets("Offsited Data")
    Set CSI = Sheets("Send CSI")
    Set Rng = OD.Range("A2", OD.Range("A" & Rows.Count).End(xlUp)).Offset(, 25)
    
    For Each Cel In Rng
        rw = Cel.Row
        If Cel = "Yes" Then
            Select Case Cel.Offset(, -1)
                Case "Dave", "Jim", "Steve", "Ron", "James"
                    r = CSI.Range("A" & Rows.Count).End(xlUp).Row + 1
                    With CSI
                    .Cells(r, 1) = OD.Cells(rw, "A")
                    .Cells(r, 2) = OD.Cells(rw, "C")[COLOR=#ff0000][/COLOR]
                    .Cells(r, 3) = OD.Cells(rw, "E")
                    .Cells(r, 4) = OD.Cells(rw, "Y")
                    .Cells(r, 5) = OD.Cells(rw, "D")
                    End With
            End Select
        End If
    Next Cel
End Sub

If the cells are general format then then attributing values is the same as paste values
If the format also needs copying across then add line to do that where required
Code:
                   .Cells(r, 2).NumberFormat = OD.Cells(rw, "C").NumberFormat
 
Upvote 0
how do i copy over the headers from the offsited data?
Code:
Sub CopyData()
    Dim OD As Worksheet, CSI As Worksheet, Rng As Range, Cel As Range, rw As Long, r As Long
    
    Set OD = Sheets("Offsited Data")
    Set CSI = Sheets("Send CSI")
[COLOR=#ff0000]    Set Rng = OD.Range("A2", OD.Range("A" & Rows.Count).End(xlUp)).Offset(, 25)
    With CSI
    .Cells(1, 1) = OD.Cells(1, "A")
    .Cells(1, 2) = OD.Cells(1, "C")
    .Cells(1, 3) = OD.Cells(1, "E")
    .Cells(1, 4) = OD.Cells(1, "Y")
    .Cells(1, 5) = OD.Cells(1, "D")
    End With[/COLOR]
    For Each Cel In Rng
        rw = Cel.Row
        If Cel = "Yes" Then
            Select Case Cel.Offset(, -1)
                Case "Dave", "Jim", "Steve", "Ron", "James"
                    r = CSI.Range("A" & Rows.Count).End(xlUp).Row + 1
                    With CSI
                    .Cells(r, 1) = OD.Cells(rw, "A")
                    .Cells(r, 2) = OD.Cells(rw, "C")
                    .Cells(r, 3) = OD.Cells(rw, "E")
                    .Cells(r, 4) = OD.Cells(rw, "Y")
                    .Cells(r, 5) = OD.Cells(rw, "D")
                    End With
            End Select
        End If
    Next Cel
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,761
Messages
6,174,343
Members
452,556
Latest member
Chrisolowolafe

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top