Copy specific columns vba

owen4512

Board Regular
Joined
Dec 10, 2014
Messages
71
Hi all,

The below code works however it requires some adapting to meet my needs. Currently the below will copy data on sheet3 in range A:N and paste this to sheet5 range B9. The difficulty i'm having is that i don't want to copy all the data from A:N but instead i want to copy data in B,C,E,H,I and paste into sheet5 B9

How can i adapt the below? Any help would be appreciated :)

VBA Code:
Sub Create_Report()

    Dim i, last_row_Data As Integer
    Dim Records As Long

    Records = Sheet1.Range("N15").Value
    last_row_Data = Application.WorksheetFunction.CountA(Sheet2.Range("A:A")) + 1 'Finds last row on 'All-Data' tab
    
    For i = 2 To last_row_Data
        If Sheet2.Range("M" & i).Value = "Outstanding" Then
            Records = Records + 1
            Sheet2.Activate
            Sheet2.Range(Cells(i, 1), Cells(i, 14)).Copy
            Sheet5.Activate
            Sheet5.Range("B9").PasteSpecial xlPasteValuesAndNumberFormats
        End If
     Next i
    
End Sub
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,727
Office Version
  1. 365
Platform
  1. Windows
Here is one way.

Replace this line:
VBA Code:
           Sheet2.Range(Cells(i, 1), Cells(i, 14)).Copy
with this:
VBA Code:
           rng = "B" & i & ", C" & i & ", E" & i & ", H" & i & ", I" & i
           Sheet2.Range(rng).Copy
 
Solution

owen4512

Board Regular
Joined
Dec 10, 2014
Messages
71
Here is one way.

Replace this line:
VBA Code:
           Sheet2.Range(Cells(i, 1), Cells(i, 14)).Copy
with this:
VBA Code:
           rng = "B" & i & ", C" & i & ", E" & i & ", H" & i & ", I" & i
           Sheet2.Range(rng).Copy
Thank you, Joe - That's done the trick!
 

Joe4

MrExcel MVP, Junior Admin
Joined
Aug 1, 2002
Messages
57,727
Office Version
  1. 365
Platform
  1. Windows
You are welcome.
Glad I was able to help!
 

Watch MrExcel Video

Forum statistics

Threads
1,133,622
Messages
5,659,926
Members
418,536
Latest member
Tezzies

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
Top