+ Reply to Thread
Results 1 to 30 of 30

Need a macro for filtering and pasting data

  1. #1
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Need a macro for filtering and pasting data

    Hi,

    I need a macro that filters sheet 'Routing' by column E that starts with digit 6. It then filters column F by those that either start with a space. (or ones that don't start with number - your choice) It then copies the results and pastes it in sheet 'Outside processing' starting from cell B3. I also need the pasted result to have double lined border around it.

    I've attached the sheet as reference. I've manually entered the results but you may delete the results in Outside Processing sheet (except column A) and see if it replicates it. Thank you!

    testing 2.xlsx

  2. #2
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Need a macro for filtering and pasting data

    Hi there,

    Please see attached file with macro.

    To filter column 'E' numbers by the first digit (6), we convert the numbers first to 'Text' (convert back to 'General' at the end).
    To filter column 'F' if the first digit is a number, I used a 'helper' formula in column 'K'. If you have other data there, move to the right of the last used column. This is deleted again at the end.

    Not sure what you mean with double-lined borders. They are normally just available at the bottom of the cell...

    There are probably more elegant solutions, but the code below should work.


    Please Login or Register  to view this content.
    Attached Files Attached Files
    If your Question is answered; please mark it SOLVED. If you are happy with a member's solution, say 'Thanks' and click the 'Star' to Add Reputation.

  3. #3
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    If you look at routing sheet in the excel I uploaded you will see how there is a special border around the pasted range. I want that border when macro automatically pastes it.

  4. #4
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    Actually, I solved the border thing but another issue I found is if there are any values in column K of sheeting routing, it gets deleted and all the columns to its right it shift 1 column to their left replacing column k after the macro is executed. Could you ensure this doesn't happen? Thanks!

  5. #5
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Need a macro for filtering and pasting data

    @skhande2, try the following vba code, it is only set up to work with your sample data, but it should give you the jist. You would also need to apply the grid lines if you want them, but it sounds like you already have a way to handle that:

    Please Login or Register  to view this content.

  6. #6
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Need a macro for filtering and pasting data

    Quote Originally Posted by skhande2 View Post
    Actually, I solved the border thing but another issue I found is if there are any values in column K of sheeting routing, it gets deleted and all the columns to its right it shift 1 column to their left replacing column k after the macro is executed. Could you ensure this doesn't happen? Thanks!
    Replace:
    Columns("K:K").Select
    Selection.Delete Shift:=xlToLeft


    With:
    Columns("K:K").Clear

  7. #7
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    Thanks for the help but its still messing up column K. (tried both of your solutions..) I've uploaded the file with the actual data. In hindsight, maybe I should have done it to begin with. Thanks!
    Practice.xlsx

  8. #8
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Need a macro for filtering and pasting data

    The updated code incorporates some of the code from "johnnyL". Thanks for a simpler solution without helper column!

    Yes, uploading a sample reflecting the actual date is making it a lot easier to find a solution.
    I also noted about using column K as a helper column in my
    I used a 'helper' formula in column 'K'. If you have other data there, move to the right of the last used column
    .

    Still not sure about the double boarder thing, but I think you worked that part out yourself.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    My computer won't allow me to execute implanted macro for security reasons. Could you paste the macro here? Thanks!

  10. #10
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Need a macro for filtering and pasting data

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    Could you ensure numbers in column F in sheet 'Outside Processing' are converted back into numbers from text? Much Appreciated!

  12. #12
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    Also, this is my solution to double borders. I applied it to other macros and it works just fine but for some reason, to this macro, it won't end by the last row :

    Please Login or Register  to view this content.
    Rest of the macro is same, I just added the border part towards the end.

  13. #13
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Need a macro for filtering and pasting data

    Here you go:

    Please Login or Register  to view this content.

  14. #14
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Need a macro for filtering and pasting data

    This will change column F back to numbers, make the 'double line' dynamic based on how many rows of test you have.
    Also added as a separate routine, called from the code, to reset any previous 'double-lines' you had in the output sheet before the double line boarder is applied.


    Please Login or Register  to view this content.
    Last edited by ORoos; 06-28-2023 at 07:32 AM. Reason: Updated Sub for ResetBoarders

  15. #15
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    Thanks guys! So both of your macros error if there are no numbers to copy and paste.

    -JohnnyL, your macro copy paste the header from routing into row 3 of Outside processing and removes its header.
    -ORoos, your macro errors (which stops other macros after it from executing that I've added to it) and corrupts the values in column E at the table in the bottom of Routing.

    ORoos, as you edit your macro, please edit the version of it I created below. Thanks!

    You can test this on the sheet I pasted earlier by removing the rows that get copy and pasted but I've uploaded a file without those for your ease. Mucho Appreciato!

    Please Login or Register  to view this content.
    Welp.xlsx

  16. #16
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Need a macro for filtering and pasting data

    @skhande2, I have come up with the following code that addresses the errors that you mentioned:

    Please Login or Register  to view this content.
    Last edited by johnnyL; 06-29-2023 at 02:02 PM.

  17. #17
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    I'm not sure what you mean. I've attached pictures that match up on both sheets columns E,F & G. When I executed your new macro, instead of running 0 results, it returns results it wasn't supposed to. (see the bottom sheet in both pics)
    Attachment 834768
    Attachment 834769

    From the original post, the criteria was, "I need a macro that filters sheet 'Routing' by column E that starts with digit 6. It then filters column F by those that either start with a space. (or ones that don't start with number - your choice)"

    As you can see in the pics, there are no cells in columns E that start with 6

  18. #18
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Need a macro for filtering and pasting data

    I just edited my previous post.

  19. #19
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Need a macro for filtering and pasting data

    I also just added an autofilter check to make sure we start with all autofilters removed.

  20. #20
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    That works . Thanks!

  21. #21
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Need a macro for filtering and pasting data

    Glad we could help.

  22. #22
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    So..I'm having another issue. Not sure if you will experience it on your end but your macro works flawlessly when only 1 excel sheet is open. But when I've multiple excel sheets open, I get a dialog box, "There is already data here. Do you want to replace it?" Then, I notice nothing is copied over to Outside processing but its emptied out and multiple columns of routing (F,G and H) are wiped out.

  23. #23
    Valued Forum Contributor
    Join Date
    11-27-2011
    Location
    usa
    MS-Off Ver
    Excel 2007, Excel 365
    Posts
    495

    Re: Need a macro for filtering and pasting data

    I only see one potential issue in the code I submitted:

    Change the second line of code from:
    Please Login or Register  to view this content.
    to:
    Please Login or Register  to view this content.

    Try that and see if it resolves the issue. If it doesn't can you further explain the issue you are having? You mentioned 'excel sheet', did you mean ' excel workbooks'?

  24. #24
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    I meant workbooks, yes. Its not happening right now, when it happens again, I will test your correction.

  25. #25
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Need a macro for filtering and pasting data

    Adding my version back into the mix.
    The updated macro will exit when no data gas been found to be copied.
    The text2column bits will only convert and change back to data at the top, no longer the full column.

    Let us know if that works.




    Please Login or Register  to view this content.

  26. #26
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    When I run this macro ORoos in a sheet where nothing gets copied, it converts numbers in column E in sheet 'Routing' into text. Could you have it convert back at the end? Thanks

  27. #27
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Need a macro for filtering and pasting data

    Try the code below and let us know if that works for you:


    Please Login or Register  to view this content.

  28. #28
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    That worked. Thanks! Hey, could you guys also please take a look at this post? Thanks!
    https://www.excelforum.com/excel-pro...ml#post5845536

    Appreciated!
    Last edited by skhande2; 07-05-2023 at 05:56 PM.

  29. #29
    Registered User
    Join Date
    11-01-2020
    Location
    London, uk
    MS-Off Ver
    365
    Posts
    87

    Re: Need a macro for filtering and pasting data

    Actually, its erroring again when I'm combining it with the bulk of other macros, could you write this macro in a way so it doesn't call another routine? I think calling it is making it error. So just 1 Sub and end sub.

  30. #30
    Valued Forum Contributor
    Join Date
    03-24-2020
    Location
    Thailand
    MS-Off Ver
    Office 2016
    Posts
    897

    Re: Need a macro for filtering and pasting data

    Try:

    Please Login or Register  to view this content.

+ 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] Filtering data then copy pasting to a new workbook
    By bkramer1 in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 02-24-2022, 12:15 PM
  2. Filtering data sets and pasting in new workbook
    By myself4u58 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-20-2020, 04:09 AM
  3. Macro For Filtering And Pasting Data
    By excellearner121 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-07-2013, 11:49 PM
  4. Macro for filtering raw data,copy/ pasting and sorting based on conditions
    By amazingjeffery in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-17-2013, 02:45 AM
  5. Filtering, Copying and Pasting Data from one Workbook to Another...
    By mtt23 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-09-2012, 12:42 AM
  6. Filtering, Coping and pasting perticular range of data.
    By protocol in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-18-2011, 10:00 AM
  7. Filtering data and pasting into another workbook (v2.0)
    By longbow007 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 05-26-2010, 01:07 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