VBa Copy Selected Columns & Paste To New Workbook

ir121973

Active Member
Joined
Feb 9, 2008
Messages
371
Hi, I wonder whether someone could help me please.

I'm using the code below to find a cell value and then copy and paste into "Sheet 1".

Code:
Dim Src As Worksheet, Dst As Worksheet
Dim LastRow As Long, r As Range
Dim CopyRange As Range
'Change these to the correct sheet names
Set Src = Sheets("Amalgamation of Search")
Set Dst = Sheets("Sheet1")
LastRow = Src.Cells(Cells.Rows.Count, "C").End(xlUp).Row
For Each r In Src.Range("C2:C" & LastRow)
     If r.Value = "SC" Then
     If CopyRange Is Nothing Then
                Set CopyRange = r.EntireRow
        Else
                Set CopyRange = Union(CopyRange, r.EntireRow)
        End If
     End If
Next r
If Not CopyRange Is Nothing Then
CopyRange.Copy Dst.Range("A1")
End If
End Sub

The problem I'm having is that I'd like to amend this, so that rather than having the need to have a "Sheet1" in my source sheet, I'd like to open a new workbook and paste the data into there.

In addition I'd like to only copy columns A, C and D.

I've spent over a day on this now, and although I've tried various combinations of code I still can't get this to work.

I just wondered whether someone may be able to look at this please and offer some guidance on how I may be able to achieve this.

Many thanks and kind regards

Chris
 
Last edited:

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Try:
Code:
Sub CopyCols()
    Application.ScreenUpdating = False
    Dim Src As Worksheet
    Dim LastRow As Long
    Set Src = ThisWorkbook.Sheets("Amalgamation of Search")
    LastRow = Src.Cells(Cells.Rows.Count, "C").End(xlUp).Row
    Src.Range("C1:C" & LastRow).AutoFilter Field:=1, Criteria1:="SC"
    Src.Range("A2:A" & LastRow).SpecialCells(xlCellTypeVisible).Copy Workbooks.Add.Sheets("Sheet1").Range("A1")
    Src.Range("C2:C" & LastRow).SpecialCells(xlCellTypeVisible).Copy Sheets("Sheet1").Range("B1")
    Src.Range("D2:D" & LastRow).SpecialCells(xlCellTypeVisible).Copy Sheets("Sheet1").Range("C1")
    Src.AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub
After you run the macro, you can save the new workbook with a name of your choosing.
 
Last edited:
Upvote 0
Some clarification is needed. If a value in C2 to end of data in column C = "SC" then copy only that row's column's A, C, and D? Is copy needed or just getting the value is needed? Where would the data go? Into a new workbook's Sheet1's first empty row in column A, to it's columns A, C, and D?

There are 2 methods besides your iteration method that would work. The only problem with your iteration is speed. Other than that, it would work fine. Method 2 would be a FindAll() routine (FindAll VBA Function). Method 3 would be a Filter method as Mumps just showed.
 
Last edited:
Upvote 0
Hi @mumps, that's fantastic, it work great.

Sincere thanks for all your help.

Kind Regards

Chris
 
Upvote 0
Hi @Kenneth Hobson,

Many thanks for taking the time to reply to my post, but as @mumps has provided a working solution, please don't spend any time on this.

Regards

Chris
 
Upvote 0

Forum statistics

Threads
1,216,583
Messages
6,131,557
Members
449,655
Latest member
Anil K Sonawane

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