+ Reply to Thread
Results 1 to 18 of 18

VBA to filter a column and past but then build onto that worksheet

  1. #1
    Registered User
    Join Date
    05-15-2013
    Location
    USA
    MS-Off Ver
    MS365 V. 2202
    Posts
    18

    VBA to filter a column and past but then build onto that worksheet

    Hello,
    I have 4 worksheets that all have the same format but different data. Column Z displays only conflict or no conflicts

    Im trying to edit this code below to go to each worksheet ( worksheet 1, 2, 3, 4) and copy only the conflicts in Column Z and then paste (merge) them into worksheet CONFLICTS.
    So look in column Z in worksheet 1, filter for just conflicts and copy then to a worksheet CONFLICTS
    Do the same in worksheet 2 but add them to what was just copied from worksheet 1 AND ADD TO worksheet CONFLICTS
    Do the same in worksheet 3 but add them to what was just copied from worksheet 1 and 2, AND ADD TO worksheet CONFLICTS
    Do the same in worksheet 4 but add them to what was just copied from worksheet 1 ,2,3 AND ADD TO worksheet CONFLICTS

    in the end i need the conflicts worksheet to have all the conflicts from worksheets 1,2,3,4 together---

    I HOPE THAT MAKES SENSE


    Sub CopyONLYConflicts()

    End Sub
    Function GetWorksheet(shtName As String) As Worksheet
    On Error Resume Next
    Set GetWorksheet = Worksheets(shtName)
    End Function




    Application.ScreenUpdating = False
    Dim x As Range
    Dim rng As Range
    Dim last As Long
    Dim sht As String

    'specify sheet name in which the data is stored
    sht = "Unit Activation Template"

    'change filter column in the following code
    last = Sheets(sht).Cells(Rows.Count, "F").End(xlUp).Row
    Set rng = Sheets(sht).Range("A1:R" & last)

    Sheets(sht).Range("F1:F" & last).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("AA1"), Unique:=True

    For Each x In Range([AA2], Cells(Rows.Count, "AA").End(xlUp))

    If Not GetWorksheet(x.Text) Is Nothing Then
    Sheets(x.Text).Delete
    End If

    With rng
    .AutoFilter
    .AutoFilter Field:=6, Criteria1:=x.Value
    .SpecialCells(xlCellTypeVisible).Copy

    Sheets.Add(After:=Sheets(Sheets.Count)).Name = x.Value
    ActiveSheet.Paste
    End With
    Next x

    ' Turn off filter
    Sheets(sht).AutoFilterMode = False

    With Application
    .CutCopyMode = False
    .ScreenUpdating = True
    End With

    End Function
    Quote ReplyReport Edit

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

    Re: VBA to filter a column and past but then build onto that worksheet

    So you are wanting to loop all sheets...Filter Column 26 for "Conflicts" and copy those rows to next row in Sheets("Conflicts")
    This assumes Conflicts Sheet exists and and data starts in A1 of each sheet...
    Please Login or Register  to view this content.
    If not...upload a sample file representing actual data setup...
    Last edited by sintek; 10-29-2022 at 01:55 PM.
    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 star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  3. #3
    Registered User
    Join Date
    05-15-2013
    Location
    USA
    MS-Off Ver
    MS365 V. 2202
    Posts
    18

    Re: VBA to filter a column and past but then build onto that worksheet

    Thank you for helping me!
    From Worksheets 1 through 4 I need to copy only conflicts in column z and paste into the CONFLICTS worksheet.

    If possible, I would only like to copy Columns A through R and Z from each worksheet (skip columns S,T,U,V,W,X)

    The workbook will have a blank CONFLICTS tab with prepopulated field names.

    As displayed in the CONFLICTS worksheet you will see the name (Column A) shows only those Conflicts in Wokrsheets 1,2,3,4

    Hopefully this helps
    .
    Thank you very much!
    Attached Files Attached Files

  4. #4
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Cool Hello, try this !


    According to your attachment an Excel basics VBA demonstration for starters to paste only to the CONFLICTS worksheet module :

    PHP Code: 
    Sub Demo1()
            Const 
    "Conflict"
            
    [A1].CurrentRegion.Offset(1).Clear
            R
    & = 2
        
    For S& = 1 To Index 1
            With Sheets
    (S).[A1].CurrentRegion.Columns
                
    .Item(26).AutoFilter 1C, , , False
                 N
    & = Application.Subtotal(103, .Item(1)) - 1
                 
    If N Then .Item("A:R").Offset(1).Copy Cells(R1): N
                
    .AutoFilter
            End With
        Next
            
    If 2 Then Range("Z2:Z" 1) = C
    End Sub 
    ► Do you like it ? ► So thanks to click on bottom left star icon « Add Reputation » !
    Last edited by Marc L; 10-29-2022 at 02:05 PM. Reason: little tweak ...

  5. #5
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: VBA to filter a column and past but then build onto that worksheet

    copy Columns A through R and Z from each worksheet (skip columns S,T,U,V,W,X)
    Your result does not show this...What about Column Y?
    This will copy A-R & Z to Conflicts sheet...
    In above code-Post 2...Replace red snippet with the below...
    Please Login or Register  to view this content.
    Last edited by sintek; 10-29-2022 at 01:55 PM.

  6. #6
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Question Re: VBA to filter a column and past but then build onto that worksheet


    Hi sintek,

    I don't believe your mod may work as expected, did you ever test it ?

  7. #7
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: VBA to filter a column and past but then build onto that worksheet

    Hi MarcL...No did not...will do quickly

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

    Re: VBA to filter a column and past but then build onto that worksheet

    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Exclamation Re: VBA to filter a column and past but then build onto that worksheet


    sintek, your code copies the source column Z to the destination column S instead of column Z, the reason of my post #6 ...
    Last edited by Marc L; 10-30-2022 at 06:03 AM.

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: VBA to filter a column and past but then build onto that worksheet

    @ MarcL

    I know the code does that...I am assuming that OP wants only A-R & Z copied...
    If possible, I would only like to copy Columns A through R and Z from each worksheet
    Am also assuming that OP sample file is not as per his requirement...Other wise it is pointless only copying specific columns as the pasted to file houses 0's in these non copied Columns...So once again, as per so many other posts of ours prior, which are still unanswered...I will wait to see what true requirement is and then easily adapt..

  11. #11
    Forum Expert
    Join Date
    11-24-2013
    Location
    Paris, France
    MS-Off Ver
    Excel 2003 / 2010
    Posts
    9,831

    Arrow Re: VBA to filter a column and past but then build onto that worksheet


    As I better understand now your point of view 'cause sometimes I do the same,
    so weird when the attachment does not exactly match the explanation ...

  12. #12
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: VBA to filter a column and past but then build onto that worksheet

    So just in case OP requirement is as you understand...Slight code mod...
    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    05-15-2013
    Location
    USA
    MS-Off Ver
    MS365 V. 2202
    Posts
    18

    Re: VBA to filter a column and past but then build onto that worksheet

    Sintek, this works!

    What would change in the code if i changed the names of the worksheets (because the workbook now has other worksheets with different data and i just need the conflicts from these 4)
    Worksheet 1 - Unit Activation
    Worksheet 2- Unit Deactivation
    Worksheet 3 - Unit Realignment
    Worksheet 4 - Unit Reorganization

    Thanks again for the help everyone
    Last edited by abrig005; 10-31-2022 at 01:49 PM.

  14. #14
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: VBA to filter a column and past but then build onto that worksheet

    Sintek, this works!
    Which code are you referring too as MarcL and I were unsure as to your exact requirement...
    Perhaps it would be best if you upload a sample file showing before and after result...Incorporating these new sheets you named above...

  15. #15
    Registered User
    Join Date
    05-15-2013
    Location
    USA
    MS-Off Ver
    MS365 V. 2202
    Posts
    18

    Re: VBA to filter a column and past but then build onto that worksheet

    Sub J3v16()
    Dim ws As Worksheet
    For Each ws In Sheets
    If ws.Name <> "CONFLICTS" Then
    With ws.Cells(1).CurrentRegion
    .AutoFilter 26, "Conflict"
    If .Columns(26).SpecialCells(12).Cells.Count - 1 > 0 Then
    .Columns("A:R").Offset(1).Copy Sheets("Conflicts").Range("A" & Rows.Count).End(3)(2)
    End If
    .AutoFilter
    End With
    End If
    Next ws
    Sheets("Conflicts").Cells(1).CurrentRegion.Columns(26).SpecialCells(4) = "Conflict"

    The image shows the various worksheets currently in my workbook as an example.

    Attachment 803078
    End Sub

  16. #16
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: VBA to filter a column and past but then build onto that worksheet

    Your attachment is invalid...If I understand then this works...
    Please Login or Register  to view this content.

  17. #17
    Registered User
    Join Date
    05-15-2013
    Location
    USA
    MS-Off Ver
    MS365 V. 2202
    Posts
    18

    Re: VBA to filter a column and past but then build onto that worksheet

    thank you!

  18. #18
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,239

    Re: VBA to filter a column and past but then build onto that worksheet

    Glad to have contributed...Tx for rep +...Please mark thread as solved...

+ 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] Using =Filter to build a list
    By Ciggy36 in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-19-2021, 09:53 PM
  2. how to past data (blank rows)into filter column
    By saquibansari in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 09-02-2019, 11:34 AM
  3. [SOLVED] VBA macros to auto filter copy and past the data by column
    By hecgroups in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-01-2019, 05:37 AM
  4. [SOLVED] How to build a filter Using VBA or a simple formula
    By Diego Santos in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-06-2015, 04:55 AM
  5. How to build a filter that defines what to remove, not what to retain:
    By Chad Bateman in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 07-26-2014, 01:40 PM
  6. find random "like" data in a column and past the sum in a different worksheet.
    By rsamedic in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 11-17-2013, 09:18 PM
  7. [SOLVED] Can't scroll down past row 260, or past column Y in Excel 2003....
    By damianberry in forum Excel General
    Replies: 2
    Last Post: 12-11-2012, 01:00 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