VBA - Copying part of a row to matching column headers

NathanA

New Member
Joined
Jan 18, 2017
Messages
34
I have code to copy rows of data to another sheet if the cell in column L equals "High" or "Extreme". I'm unsure how to copy only certain columns of that row to the new spreadsheet, which has matching headers in place. I'd like to either copy columns 2, 3, 6, 12, 14, 18, and 21, or have the code copy the columns which match the column headers in the new sheet. Any guidance would be much appreciated!

Code:
Sub Copy()
     'assuming the data is in Current Risks
Set i = Sheets("Current Risks")
Set e = Sheets("Extreme&High Risk Report")
Dim d
Dim j
d = 1
j = 2


Do Until IsEmpty(i.Range("L" & j))
'copy row with high or extreme risk rating
If i.Range("L" & j) = "Extreme" Or i.Range("L" & j) = "High" Then
d = d + 1
e.Rows(d).Value = i.Rows(j).Value


End If
j = j + 1
Loop


End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
NOT tested but I think this right:
Code:
Sub Copy()
     'assuming the data is in Current Risks
    Set i = Sheets("Current Risks")
    Set e = Sheets("Extreme&High Risk Report")
    Dim d
    Dim j
    d = 1
    j = 2
    Do Until IsEmpty(i.Range("L" & j))
        'copy row with high or extreme risk rating
        If i.Range("L" & j) = "Extreme" Or i.Range("L" & j) = "High" Then
            d = d + 1
'            e.Rows(d).Value = i.Rows(j).Value
'            2, 3, 6, 12, 14, 18, and 21
            e.Cells(d, 1).Value = i.Cells(j, 2).Value
            e.Cells(d, 2).Value = i.Cells(j, 3).Value
            e.Cells(d, 3).Value = i.Cells(j, 6).Value
            e.Cells(d, 4).Value = i.Cells(j, 12).Value
            e.Cells(d, 5).Value = i.Cells(j, 14).Value
            e.Cells(d, 6).Value = i.Cells(j, 18).Value
            e.Cells(d, 7).Value = i.Cells(j, 21).Value
        End If
        j = j + 1
    Loop
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,248
Messages
6,171,027
Members
452,374
Latest member
keccles

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