VBA to copy paste some filtered columns to new sheet (dynamic table)

  • Hi, I have a set of data that I will be using different criteria to filter.


    I will need to


    1) copy some columns of the filtered data without headers, and paste into another existing sheet.


    2) as the pasted sheet has other columns (with formulas), i will need the formulas to extend to all the pasted rows.


    In my attached file, there are 14 possible rows that can be copied over to the Output sheet. I am trying to copy columns A, B and H over to Output. But as you can see I also have formulas in Columns F, H and I in Output. It has to be applied whether the filtered data has 5 or 10 or 14 rows. How can i ensure that the formulas will be applied to however many rows are pasted?


    thanks very much!forum.ozgrid.com/index.php?attachment/71240/

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)



    Regards
    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    Hi, Thanks for the prompt reply!


    1. This works in copy and pasting the entire set of data, however if the data in Raw is filtered, the macro still pastes the full 14 rows. Is there a way to edit such that it only copies the filtered data?


    2. Can the vba also incorporate the function of clearing the pasted columns before running the copy-and paste process? E.g. When I run the macro a second time, the information is currently pasted in new rows below those pasted from the first run.


    thanks so much!

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    Quote


    1. This works in copy and pasting the entire set of data, however if the data in Raw is filtered, the macro still pastes the full 14 rows. Is there a way to edit such that it only copies the filtered data?


    2. Can the vba also incorporate the function of clearing the pasted columns before running the copy-and paste process? E.g. When I run the macro a second time, the information is currently pasted in new rows below those pasted from the first run.


    Point 1 is done. I wasnt clear on point 2... As I read it the first part, you want to clear previously pasted data, this is what the code below does. So you get a fresh view each time. Your example seems to contradict this, but I'm not sure if your example is from the code as it was in post number #2, or how you would like it to be :) Anyway, let me know if the code is right.


    Also, if for example, I select "Banks", there is only 1 row to copy (fine), but the formulas in columns E:J are not being cleared... so you could have 8 or 10 rows of formulas from the last time you run it. If you want to clear the formulas too then change


    Code
    Set r2 = .Range("B8:D8").Resize(.Range("B6").CurrentRegion.Rows.Count)


    to

    Code
    Set r2 = .Range("B8:J8").Resize(.Range("B6").CurrentRegion.Rows.Count)



    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    Wow thank you so much!


    It works perfectly now. I hope i am not asking too much, but can i also check if the following is possible:
    - Say I put a formula in Column E (%) =100%/((COUNTIF(B:B, ">0"). Understand that it will be applied to the whole column when macro is run.
    - What if after the macro, I made a manual change to some of the cells in Column E such that is overwrites the formula. E.g. I put in 50%, 20% etc.
    - When I re-run the macro, can the column cells change back to =100%/((COUNTIF(B:B, ">0")?


    Thank you!

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    Assuming you have kept this line

    Code
    Set r2 = .Range("B8:D8").Resize(.Range("B6").CurrentRegion.Rows.Count)


    Then the formulas are Not overwritten. So if you change the formula on some rows to fixed percentages, then they should be retained when you rerun the macro.


    Its only in cases where the number of copied / Pasted rows ("B:D") EXCEEDS the number of rows with formulas that I "filldown" the formulas based on the LAST row that contains formulas ("E:J"). Other than that, I dont touch the formulas in "E:J"


    Regards
    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    I actually used the line Set r2 = .Range("B8:J8").Resize(.Range("B6").CurrentRegion.Rows.Count) instead.


    I tried running the macro after changing the entire Column E from my formula to manual percentages, but it simply does a filldown of the number instead of the original formula.


    I believe I will need to incorporate the formula into the vba somehow to ensure it repopulates with the formulas instead.

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    In the short term, change back this line:

    Code
    Set r2 = .Range("B8:J8").Resize(.Range("B6").CurrentRegion.Rows.Count)

    to

    Code
    Set r2 = .Range("B8:D8").Resize(.Range("B6").CurrentRegion.Rows.Count)


    In the longer term you will need to include the formula as part of the logic in the VBA.


    I would suggest starting a new thread on this, because ultimately the problem you are trying to solve right here in this thread has been solved ;)


    In the new thread you should try and describe the formula you want to inject into the rows... shouldnt be that hard to do it. The formula should be generic enough to fit your business needs... for example, under what conditions would you change a cell from a formula to a fixed value. The formula should be generic enough to account for those instances. As I said, start a new thread ;)


    Regards
    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    Ok got it, thanks very much for the kind help!
    -Clari

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    Hi,


    As a follow up to my query, may I also check if it is possible to incorporate an 'if' into the vba -- such that as long as any of the filtered visible cells contain "#", then do not copy that row.


    this is because some of the input data may contain errors, in which case i do not want to copy and paste that row.


    thank you!

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    You could probably do this yourself... Typically its just a case of including extra criteria on the filter... for example, if "Amount" or "Years" or "Yield" had a "#" error in the them, then you would simply apply a filter on those fields for "Number" and "Greater Than" and then select 0 as the value. This will hide any "#" errors in that column and my vba code will only copy the visible filtered rows.


    I could that do that in VBA too (apply a filter >0), but I would need to know in advance which column contains (or is likely to contain) the # error.


    Regards
    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    I would like to apply it to the last column. I agree with your filtering method, however as my actual spreadsheet already has a number of fields that require filtering, I am trying not to complicate it further.


    Appreciate any suggestions on this!

  • Re: VBA to copy paste some filtered columns to new sheet (dynamic table)


    That shouldnt matter... any given field (column) can filter for two criteria...


    Alternatively, just copy the data to the new table (including #values or #errors) - as we are doing above anyway, and then apply the filter to the new table > 0 to hide the rows with # values


    (Or got a step further and filter the new table for the character "#" and delete those rows from the new table)


    Ger

    _______________________________________________
    There are 10 types of people in the world. Those that understand Binary and those that dont. :P


    Why are Halloween and Christmas the same? Because Oct 31 = Dec 25... ;)

    _______________________________________________

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!