VBA - Copying part of a row to matching column headers

NathanA

New Member
Joined
Jan 18, 2017
Messages
33
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
 

Warship

Well-known Member
Joined
Jul 17, 2007
Messages
1,051
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
 

Forum statistics

Threads
1,082,139
Messages
5,363,362
Members
400,730
Latest member
cookie123

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top