Copy marked lines to a new tab

louacct3

New Member
Joined
Dec 2, 2020
Messages
3
Office Version
  1. 365
Platform
  1. Windows
I want to add a new sheet (Sheet B) to my workbook that copies over all lines on an existing sheet (Sheet A) that are marked with an X.

So for example, I have the following data on Sheet A:

ItemResponsibilityCostIs Eligible for Credit?
Paint wallsIndependent contractor$3750
Fix toiletsConstruction company$500X
Survey foundationIndependent contractor$750
Rewire interiorDo it myself$55X

What I want to happen is for all of the lines that are marked "Is Eligible for Credit?" to populate themselves on Sheet B like this:

ItemResponsibilityCost
Fix toiletsConstruction company$500
Rewire interiorDo it myself$55

Adding or removing the 'X' on Sheet A should cause Sheet B to update.

Is there a function that can grab all of these lines, or does this need to be done with a VBA script/macro? Any advice on accomplishing this feat is appreciated.
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.
I know I can do this with formulas that look for the presence of the data in that last column, and only copy the data if the X exists...but how to do it gracefully so that it doesn't leave blank lines in between?
 
Upvote 0
VBA Code:
Option Explicit

Sub CopyYes()
    Dim c As Range
    Dim j As Integer
    Dim Source As Worksheet
    Dim Target As Worksheet
    
    
    ' Change worksheet designations as needed
    Set Source = ActiveWorkbook.Worksheets("Sheet1")
    Set Target = ActiveWorkbook.Worksheets("Sheet2")
    Target.Range("A2:D1000").Delete
        
    j = 2     ' Start copying to row 1 in target sheet.
    
    
    For Each c In Source.Range("D2:D1000")   ' Do 1000 rows
        
        If c = "X" Or c = "x" Then
           Source.Rows(c.Row).Copy Target.Rows(j)
           j = j + 1
        End If
        
    Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,613
Messages
6,120,515
Members
448,968
Latest member
Ajax40

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