+ Reply to Thread
Results 1 to 8 of 8

Filter specific Text from column and copy to Target sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    239

    Filter specific Text from column and copy to Target sheet

    Hello,

    I'm using below code to copy the data from "Raw Data" to "ProjectSummary" tab.

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim x, ColArr, i As Long, ws As Worksheet
    ColArr = Array("P", "Q", "U", "O", "N", "R")
    Set ws = Sheets("Raw Data")
    If Not Intersect(Target, Range("B1")) Is Nothing Then
        Application.EnableEvents = False
        x = Application.Match(Target, ws.Range("C:C"), 0)
        If Not IsError(x) Then
            Range("B2").Resize(5) = Application.WorksheetFunction.Transpose(Array(ws.Range("D" & x), ws.Range("H" & x), ws.Range("A" & x), ws.Range("X" & x), ws.Range("I" & x)))
            For i = LBound(ColArr) To UBound(ColArr)
                If Not IsError(ws.Range(ColArr(i) & x)) Then Range("E" & i + 1) = ws.Range(ColArr(i) & x)
            Next i
            Range("A8").CurrentRegion.Delete
            With ws
                With .Cells(1).CurrentRegion
                    .AutoFilter 3, Target
                    Union(.Columns("J"), .Columns("L"), .Columns("S"), .Columns("V"), .Columns("Z"), .Columns("AP"), .Columns("AQ:AR")).Copy Range("A8")
                    .AutoFilter
                End With
            End With
        End If
        Application.EnableEvents = True
    End If
    End Sub
    My further requirement is that, rather than copying all the rows to "ProjectSummary", I want macro to look for a text "Positive" in column AO (Raw Data tab) and only copy the "Positive" row data to "Project Summary" tab. Sample attached for reference.

    Please suggest with changes in the above code.

    Thanks !
    Attached Files Attached Files

  2. #2
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: Filter specific Text from column and copy to Target sheet

    Just add another filter below existing...See red snippet
     .AutoFilter 3, Target
     .AutoFilter 41, "Positive"
    Good Luck...
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the [★ Add Reputation] to left of post window...
    Also....Add a comment if you like!!!!
    And remember...Mark Thread as Solved...
    Excel Forum Rocks!!!

  3. #3
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    239

    Re: Filter specific Text from column and copy to Target sheet

    Well ! i've made some changes to the code which I posted in my first thread

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim x, ColArr, i As Long, ws As Worksheet
    ColArr = Array("P", "Q", "U", "O", "N", "R")
    Set ws = Sheets("owssvr")
    If Not Intersect(Target, Range("B1")) Is Nothing Then
        Application.EnableEvents = False
        x = Application.Match(Target, ws.Range("C:C"), 0)
        If Not IsError(x) Then
            Range("B2").Resize(5) = Application.WorksheetFunction.Transpose(Array(ws.Range("D" & x), ws.Range("H" & x), ws.Range("A" & x), ws.Range("X" & x), ws.Range("I" & x)))
            For i = LBound(ColArr) To UBound(ColArr)
                If Not IsError(ws.Range(ColArr(i) & x)) Then Range("E" & i + 1) = ws.Range(ColArr(i) & x)
            Next i
            Range("A8").CurrentRegion.Delete
            With ws
                With .Cells(1).CurrentRegion
                ws.ListObjects("Table_owssvr").Range.AutoFilter Field:=3, Criteria1:=Target
                    '.AutoFilter 3, Target
                    Union(.Columns("J"), .Columns("L"), .Columns("S"), .Columns("V"), .Columns("Z"), .Columns("AP"), .Columns("AQ:AR")).Copy Range("A8")
                    ws.ListObjects("Table_owssvr").Range.AutoFilter
                End With
            End With
        End If
        Application.EnableEvents = True
    End If
    End Sub
    where can i apply the filter to copy "positive" rows ?

    Thanks !

  4. #4
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: Filter specific Text from column and copy to Target sheet

    The sample code in above post does not include listobjects...
    I cannot give you this answer as I do not have the file...
    Is column AO part of the table...
    Then this...
    ws.ListObjects("Table_owssvr").Range.AutoFilter Field:=41, Criteria1:="Positive"

  5. #5
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    239

    Re: Filter specific Text from column and copy to Target sheet

    Well ! I added one more filter as :

    ws.ListObjects("Table_owssvr").Range.AutoFilter Field:=41, Criteria1:="Positive"
    This allowed to copy and paste the Positive row to the target sheet.

    But if there is only "Negative" rows in the "owssvr" tab, I should get a msg box as "No Data found"

    Thanks !

  6. #6
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: Filter specific Text from column and copy to Target sheet

    Don't have your file to test so adjust this snippet for listobject...
    I am guessing though with your setup, this should be your full code....
    Private Sub Worksheet_Change(ByVal Target As Range)
    Dim x, ColArr, i As Long, ws As Worksheet
    ColArr = Array("P", "Q", "U", "O", "N", "R")
    Set ws = Sheets("Raw Data")
    If Not Intersect(Target, Range("B1")) Is Nothing Then
        Application.EnableEvents = False
        x = Application.Match(Target, ws.Range("C:C"), 0)
        If Not IsError(x) Then
            Range("B2").Resize(5) = Application.WorksheetFunction.Transpose(Array(ws.Range("D" & x), ws.Range("H" & x), ws.Range("A" & x), ws.Range("X" & x), ws.Range("I" & x)))
            For i = LBound(ColArr) To UBound(ColArr)
                If Not IsError(ws.Range(ColArr(i) & x)) Then Range("E" & i + 1) = ws.Range(ColArr(i) & x)
            Next i
            Range("A8").CurrentRegion.Delete
            With ws.ListObjects("Table_owssvr").Range
                .AutoFilter 3, Target
                .AutoFilter 41, "Positive"
                If .Columns(3).SpecialCells(xlCellTypeVisible).Cells.Count - 1 Then
                    Union(.Columns("J"), .Columns("L"), .Columns("S"), .Columns("V"), .Columns("Z"), .Columns("AP"), .Columns("AQ:AR")).Copy Range("A8")
                Else
                    MsgBox "NO RECORDS FOUND"
                End If
                .AutoFilter
            End With
        End If
        Application.EnableEvents = True
    End If
    End Sub
    Last edited by Sintek; 04-02-2020 at 03:42 AM.

  7. #7
    Forum Contributor
    Join Date
    02-04-2015
    Location
    India
    MS-Off Ver
    365 (2202)
    Posts
    239

    Re: Filter specific Text from column and copy to Target sheet

    Hi Sintek,

    I made some changes to the code provided by you and was able to fix the issue.

    Thanks ! for your support as always.

    Cheers.

  8. #8
    Forum Guru Sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,517

    Re: Filter specific Text from column and copy to Target sheet

    Pleasure...glad I could help...

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Copy specific column data from one sheet to another sheet using Certain filter Criteria
    By xlhelp7 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-02-2017, 12:09 AM
  2. Filter column by text and copy to another sheet
    By Grens in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-11-2016, 10:05 AM
  3. [SOLVED] Copy rows with specific text in specific column into specific sheet
    By Valemaar in forum Excel Programming / VBA / Macros
    Replies: 18
    Last Post: 08-22-2014, 03:23 PM
  4. Replies: 2
    Last Post: 01-13-2013, 06:50 AM
  5. [SOLVED] for each sheet with first 3 char matching variable, copy column C to Target sheet unless..
    By Spyderz in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-11-2012, 08:17 AM
  6. Replies: 3
    Last Post: 02-16-2011, 08:22 PM
  7. Filter, copy specific columns, and paste to another sheet.
    By Xrull in forum Excel Programming / VBA / Macros
    Replies: 19
    Last Post: 09-29-2009, 06:33 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1