+ Reply to Thread
Results 1 to 14 of 14

Filter data and copy visible cells based on criteria

Hybrid View

  1. #1
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Filter data and copy visible cells based on criteria

    Hello everyone

    Hope everyone is fine
    In my attachment I have Data sheet .. I need first to filter column D according to the value in range("I1") then the visible results would be copied to specific sheets in order (01A then 02A then 03A .. That's sheets have the letter A to the right)
    Copying the visible cells would be 29 records for each page

    For example : I have filtered the data and got 38 record ..
    so in sheets("01A") I expect to have the first 29 names
    then in sheets("02A") there would be just 9 records to be copied
    of course sheets("03A") would be empty with no names as the results are out ..

    In target sheets (01A,02A,03A) I need first before running the code to clear the contents of the ranges (C6:C1000) as a preparation to receive the new results


    the copied data would be column B only .. the target cell in target sheets would be cell C6
    Hope it is clear
    Attached Files Attached Files
    < ----- Please click the little star * next to add reputation if my post helps you
    Visit Forum : From Here

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Filter data and copy visible cells based on criteria

    Maybe :

    Sub Test()
      Dim arrRng(1 To 3) As Range, rng As Range, i As Long
    
      Application.ScreenUpdating = False
    
      For i = 1 To UBound(arrRng)
          Set arrRng(i) = Worksheets("0" & i & "A").Range("C6")
          arrRng(i).Resize(1000).ClearContents
      Next i
    
      With Sheets("Data")
        .AutoFilterMode = False
        With .Range("B1").CurrentRegion
          .AutoFilter field:=3, Criteria1:=Parent.Range("I1").Value
          .Columns(1).Offset(1).SpecialCells(xlCellTypeVisible).Copy arrRng(1)
        End With
        .AutoFilterMode = False
      End With
    
      For i = 1 To (UBound(arrRng) - 1)
          With arrRng(i).Offset(29).Resize(1000)
            If Not IsEmpty(.Cells(1)) Then
               .Copy arrRng(i + 1)
               .ClearContents
            Else
               Exit For
            End If
          End With
      Next i
    
      Application.ScreenUpdating = True
    End Sub

  3. #3
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Filter data and copy visible cells based on criteria

    Thanks a lot for this great and swift help my best friend
    Now everything is ok ..
    I need to add lines at first of the code that show all the sheets (01A - 01B - 02A - 02B - 03A - 03B)
    then at the end of the code to hide the sheets that doesn't receive data

    As an example if I filtered 29 records only ..the code would copy data to 01A sheet only (((So 01A and 01B sheets should be unhidden))) while the other four sheets to be hidden
    Hope it is clear

  4. #4
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Filter data and copy visible cells based on criteria

    May I have alert message if the number of results exceeded the number of sheets which will receive data
    I mean in my attachment for example I have three sheets 01A - 02A - 03A so these sheets would receive only (29*3) = 87
    If the number of filtered results (records ) exceed 87 ,I need to receive alert message so as to create new sheets for new data
    Thanks a lot ..(Bear me in this thread ..as I have many points)

  5. #5
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Filter data and copy visible cells based on criteria

    Quote Originally Posted by YasserKhalil View Post
    Thanks a lot for this great and swift help my best friend
    In fact, the new requests are coming in swift mode too

    I need to add lines at first of the code that show all the sheets (01A - 01B - 02A - 02B - 03A - 03B)
    then at the end of the code to hide the sheets that doesn't receive data
    It is easier the opposite way, first the code hiding all sheets (as the same time with clearcontents job), and then do the copy paste job, and finally showing the sheets which have data on them.

    May I have alert message if the number of results exceeded the number of sheets which will receive data
    ....
    Thanks a lot ..(Bear me in this thread ..as I have many points)
    Done.
    BTW, you....... are a bear ?

    Sub Test()
      Dim arrRng(1 To 3) As Range, rng As Range, i As Long, total As Long
    
      Application.ScreenUpdating = False
    
      For i = 1 To UBound(arrRng)
          Set arrRng(i) = Worksheets("0" & i & "A").Range("C6")
          arrRng(i).Resize(1000).ClearContents
          arrRng(i).Parent.Visible = xlSheetVeryHidden
          Sheets(Replace(arrRng(i).Parent.Name, "A", "B")).Visible = xlSheetVeryHidden
      Next i
    
      With Sheets("Data")
        .AutoFilterMode = False
        With .Range("B1").CurrentRegion
          .AutoFilter field:=3, Criteria1:=.Parent.Range("I1").Value
          With .Columns(1).Offset(1).SpecialCells(xlCellTypeVisible)
            total = .Count
            .Copy arrRng(1)
          End With
        End With
        .AutoFilterMode = False
      End With
    
      If Not IsEmpty(arrRng(1)) Then
         arrRng(1).Parent.Visible = xlSheetVisible
         Sheets(Replace(arrRng(1).Parent.Name, "A", "B")).Visible = xlSheetVisible
    
         For i = 1 To (UBound(arrRng) - 1)
             With arrRng(i).Offset(29).Resize(1000)
               If Not IsEmpty(.Cells(1)) Then
                  .Copy arrRng(i + 1)
                  .ClearContents
                  arrRng(i + 1).Parent.Visible = xlSheetVisible
                  Sheets(Replace(arrRng(i + 1).Parent.Name, "A", "B")).Visible = xlSheetVisible
               Else
                  Exit For
               End If
             End With
         Next i
         
         If total > 29 * UBound(arrRng) Then MsgBox "Not enough sheets to divide data properly"
      End If
    
      Application.ScreenUpdating = True
    End Sub

  6. #6
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Filter data and copy visible cells based on criteria

    Mr. Karedog
    everything is perfect now

    Now I need to copy different columns to target sheets .. in the same way
    for example :: In Sheets("Data") ,Columns(V:X) have to copied in the same way to the target sheets ... to D6 - E6 - F6
    As well In Sheets("Data"),Columns("P:U") have to be copied in the same way to the target sheets ... to G6 - H6 - I6 - J6 - K6 - L6

    There are other columns in Sheets("Data") to be copied to sheets(01B - 02B -03B ..)
    for example
    Column K in sheets("Data") to be copied to sheets(01B - 02B -03B ..) in AE6

    Thanks a lot for your wonderful assistance

  7. #7
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Filter data and copy visible cells based on criteria

    Another point ..
    I tested the code and it doesn't work well unless I select sheets("Data")
    In fact I need to run the code even if I selected any other sheet
    Thank you very much for you great help

    OK I edited Parent. part with Sheets("Data"). (this point is ok)
    Last edited by YasserKhalil; 11-10-2015 at 07:09 PM.

  8. #8
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Filter data and copy visible cells based on criteria

    Thanks a lot for this wonderful help ..
    By the way how can I say (Bear with me ..) I mean to be patient with me in this thread as I have many ideas and new points
    Test the code on my original file and I will be back if there is any problem
    Regards

  9. #9
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Filter data and copy visible cells based on criteria

    Quote Originally Posted by YasserKhalil View Post
    Thanks a lot for this wonderful help ..
    By the way how can I say (Bear with me ..) I mean ...
    You are welcome.
    Yes, I know
    It has been a while since I am being involved in some of your threads. I just made a joke to make it not too boring.
    The bear, I mean, like the grizzly bear, polar bear, ....

    -----

    One way is to save the filtered rows of AutoFilter to a variable (in my code below it is called filteredRows), and then you can use this var as many time as needed, together with intersect method, to get intersection area of expected columns.

    Sub Test()
      Dim arrRngA(1 To 3) As Range, arrRngB(1 To 3) As Range, filteredRows As Range
      Dim i As Long, total As Long
    
      Application.ScreenUpdating = False
    
      For i = 1 To UBound(arrRngA)
          Set arrRngA(i) = Worksheets("0" & i & "A").Range("C6")
          Set arrRngB(i) = Worksheets("0" & i & "B").Range("AE6")
          arrRngA(i).Resize(1000, 10).ClearContents  '.Resize(1000, 10) = 1000 rows x 10 columns (C:L)
          arrRngB(i).Resize(1000).ClearContents
          arrRngA(i).Parent.Visible = xlSheetVeryHidden
          arrRngB(i).Parent.Visible = xlSheetVeryHidden
      Next i
    
      With Sheets("Data")
        .AutoFilterMode = False
         With .Range("B1").CurrentRegion
           .AutoFilter field:=3, Criteria1:=.Parent.Range("I1").Value
            With .Columns(1).Offset(1).SpecialCells(xlCellTypeVisible)
              total = .Count
              Set filteredRows = .EntireRow
            End With
         End With
        .AutoFilterMode = False
      
        If total >= 2 Then
           arrRngA(1).Parent.Visible = xlSheetVisible
           Intersect(filteredRows, Sheet1.Columns("B")).Copy arrRngA(1)
           Intersect(filteredRows, Sheet1.Columns("V:X")).Copy arrRngA(1).Offset(0, 1)
           Intersect(filteredRows, Sheet1.Columns("P:U")).Copy arrRngA(1).Offset(0, 4)
    
           arrRngB(1).Parent.Visible = xlSheetVisible
           Intersect(filteredRows, Sheet1.Columns("K")).Copy arrRngB(1)
        Else
           Exit Sub
        End If
      End With
    
      For i = 1 To (UBound(arrRngA) - 1)
          With arrRngA(i).Offset(29).Resize(1000, 10)
            If Not IsEmpty(.Cells(1)) Then
               .Copy arrRngA(i + 1)
               .ClearContents
               arrRngA(i + 1).Parent.Visible = xlSheetVisible
            Else
               Exit For
            End If
          End With
          With arrRngB(i).Offset(29).Resize(1000)
            .Copy arrRngB(i + 1)
            .ClearContents
            arrRngB(i + 1).Parent.Visible = xlSheetVisible
          End With
      Next i
    
      If total > 29 * UBound(arrRngA) Then MsgBox "Not enough sheets to divide data properly"
      Application.ScreenUpdating = True
    End Sub
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Filter data and copy visible cells based on criteria

    Mr. Karedog
    sorry for disturbing you ...
    I will explain in detail again so as to have a complete vision
    In sheets("Data") I have data in A1:X & LR
    I need to transfer data (filtered data only not all ...visible data) to sheets(01A - 01B - 02A - 02B etc)

    some columns would be in sheets A ad other columns would be in sheets B ..
    In detail:
    --------
    Sheets("Data") columns ------- Target Sheet ----------- Target cell
    **************************************************
    A - B -------------------------Sheets A ------------- B6 - C6

    V - W - X --------------------Sheets A ------------- D6 - E6 -F6

    P - Q - R - S - T - U --------Sheets A -------------- G6 - H6 - I6 - J6 - K6 - L6

    F - G - H ---------------- Sheets A ---------------- M6 - N6 - O6

    I ----------------------- Sheets A ----------------- X6

    K ----------------------Sheets B ----------------- AE6

    J ----------------------Sheets B ----------------- AF6

    L ----------------------Sheets B ----------------- AG6

    M ----------------------Sheets B ----------------- AH6

    This is the complete vision ...

    As for sheets A & B that wouldn't receive data to be hidden ...I have 9 couples (01A - 01B - 02A - 02B .............09A-09B)
    Regards

  11. #11
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Filter data and copy visible cells based on criteria

    As your post in #9, the layout has been changed, i.e. :
    - Data now start from column A (previously column B), so what field to be AutoFiltered now ?
    - Previously the criteria is taken from cell I1, now column I is used as data area, where is the criteria cell now ?

    But the principal is exactly the same with the code in #10, you just need to adjust some area.
    If you have any trouble, please upload the workbook of final layout.

  12. #12
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Filter data and copy visible cells based on criteria

    Great ... Great ... Great
    Your code is wonderful ...Thank you very very much for all this great help

    As for joking , I like it .. And i got your joke in fact and i accept it (you are my best friend ..! Did you forget that?) So if we are friends , we are of the same kind
    Thanks a lot for awesome coding ..
    Regards

  13. #13
    Forum Expert
    Join Date
    04-23-2009
    Location
    Matrouh, Egypt
    MS-Off Ver
    Excel 2013
    Posts
    6,892

    Re: Filter data and copy visible cells based on criteria

    Thanks a lot
    I'm editing you fantastic and great code to suit my file
    That's wonderful ...
    Let me half an hour and if I faced any problem I will tell you at once

  14. #14
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Filter data and copy visible cells based on criteria

    You are welcome.
    And about we are of the same kind, I didn't see it coming


    Regards

+ 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. copy, paste visible cells after Auto filter- VBA
    By meus in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-31-2015, 01:59 AM
  2. filter data based on criteria and copy to another sheet
    By anbarasi_r in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-23-2015, 09:56 PM
  3. Advanced Filter Copy visible cells only
    By carsto in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-22-2015, 03:28 AM
  4. [SOLVED] Copy Filter Data and paste it on another workbook with special cells(Only Visible Cells)
    By HaroonSid in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 04-13-2014, 07:59 AM
  5. Macro to filter data and copy the data's from multiple columns based on the criteria
    By millatshawn in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-22-2014, 08:14 AM
  6. Need VBA help to filter and copy data based on specific criteria
    By astrial in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 06-13-2013, 10:48 AM
  7. Copy to Visible cells after filter
    By vandanavai in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-23-2009, 01:29 PM

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